Presented By:
Prateek Gupta(Software Consultant)
Introduction to
MDC Logging in Scala
Lack of etiquette and manners is a huge turn off.
KnolX Etiquettes
Punctuality
Respect Knolx session timings, you
are requested not to join sessions
after a 5 minutes threshold post
the session start time.
Feedback
Make sure to submit a constructive
feedback for all sessions as it is
very helpful for the presenter.
Avoid Disturbance
Avoid leaving your window
unmuted after asking a question.
Mute
Please keep your window on mute.
Agenda
02 What is MDC ?
03 Dependency and Configuration
04 MDC Across a Single Thread
05 MDC Across Multiple Threads
06 Demo
01 Problem with multi-threaded Logging
Problem with multi-threaded Logging
● Imagine you have a multi-user web application. It gets a HTTP request, calls a few functions and serves
a response.
● But this might not be enough for useful debugging.
● So we need a way to put the log messages into context, like this:
● With this we can search and filter our logs for the request we're interested in.
What is MDC ?
● The Mapped Diagnostic Context, in short MDC, allows to put context to all log statements for a
single thread.
● We can also propagate MDC across multiple threads or futures.
● For that purpose, we have to create our own ExecutionContext where we can copy the MDC to any
new thread that is being used.
● This provide us multi-thread MDC logging.
Dependency and Configuration
● Add the following dependency in your build.sbt.
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.2.3"
● In logback.xml, add the following pattern.
<pattern>%date (%level) [%thread] [%X{contextID}]: %message%n</pattern>
MDC Across a Single Thread
● It is very easy to provide context to all the log statements across a single thread.
MDC.put(“contextId”, UUID.randomUUID().toString)
● This puts a context value as identified by the key in the current thread’s context map.
● With this, everything that is in the current thread will use the same MDC.
MDC Across Multiple Threads
● MDC works fine if the thread doesn’t change.
● So if we use a new Thread or a Future, we are losing our precious MDC!
● To get the MDC across that new thread, we'll need to provide our own ExecutionContext.
MdcExecutionContext
Demo
Thank You !

Introduction to MDC Logging in Scala.pdf

  • 1.
    Presented By: Prateek Gupta(SoftwareConsultant) Introduction to MDC Logging in Scala
  • 2.
    Lack of etiquetteand manners is a huge turn off. KnolX Etiquettes Punctuality Respect Knolx session timings, you are requested not to join sessions after a 5 minutes threshold post the session start time. Feedback Make sure to submit a constructive feedback for all sessions as it is very helpful for the presenter. Avoid Disturbance Avoid leaving your window unmuted after asking a question. Mute Please keep your window on mute.
  • 3.
    Agenda 02 What isMDC ? 03 Dependency and Configuration 04 MDC Across a Single Thread 05 MDC Across Multiple Threads 06 Demo 01 Problem with multi-threaded Logging
  • 4.
    Problem with multi-threadedLogging ● Imagine you have a multi-user web application. It gets a HTTP request, calls a few functions and serves a response. ● But this might not be enough for useful debugging.
  • 5.
    ● So weneed a way to put the log messages into context, like this: ● With this we can search and filter our logs for the request we're interested in.
  • 6.
    What is MDC? ● The Mapped Diagnostic Context, in short MDC, allows to put context to all log statements for a single thread. ● We can also propagate MDC across multiple threads or futures. ● For that purpose, we have to create our own ExecutionContext where we can copy the MDC to any new thread that is being used. ● This provide us multi-thread MDC logging.
  • 7.
    Dependency and Configuration ●Add the following dependency in your build.sbt. libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.2.3" ● In logback.xml, add the following pattern. <pattern>%date (%level) [%thread] [%X{contextID}]: %message%n</pattern>
  • 8.
    MDC Across aSingle Thread ● It is very easy to provide context to all the log statements across a single thread. MDC.put(“contextId”, UUID.randomUUID().toString) ● This puts a context value as identified by the key in the current thread’s context map. ● With this, everything that is in the current thread will use the same MDC.
  • 9.
    MDC Across MultipleThreads ● MDC works fine if the thread doesn’t change. ● So if we use a new Thread or a Future, we are losing our precious MDC! ● To get the MDC across that new thread, we'll need to provide our own ExecutionContext.
  • 10.
  • 11.
  • 12.