diff options
Diffstat (limited to 'jvm/src/main/scala/xyz/driver/core/logging')
-rw-r--r-- | jvm/src/main/scala/xyz/driver/core/logging/MdcExecutionContext.scala | 31 | ||||
-rw-r--r-- | jvm/src/main/scala/xyz/driver/core/logging/package.scala | 7 |
2 files changed, 38 insertions, 0 deletions
diff --git a/jvm/src/main/scala/xyz/driver/core/logging/MdcExecutionContext.scala b/jvm/src/main/scala/xyz/driver/core/logging/MdcExecutionContext.scala new file mode 100644 index 0000000..df21b48 --- /dev/null +++ b/jvm/src/main/scala/xyz/driver/core/logging/MdcExecutionContext.scala @@ -0,0 +1,31 @@ +/** Code ported from "de.geekonaut" %% "slickmdc" % "1.0.0" + * License: @see https://github.com/AVGP/slickmdc/blob/master/LICENSE + * Blog post: @see http://50linesofco.de/post/2016-07-01-slick-and-slf4j-mdc-logging-in-scala.html + */ +package xyz.driver.core.logging + +import org.slf4j.MDC +import scala.concurrent.ExecutionContext + +/** + * Execution context proxy for propagating SLF4J diagnostic context from caller thread to execution thread. + */ +class MdcExecutionContext(executionContext: ExecutionContext) extends ExecutionContext { + override def execute(runnable: Runnable): Unit = { + val callerMdc = MDC.getCopyOfContextMap + executionContext.execute(new Runnable { + def run(): Unit = { + // copy caller thread diagnostic context to execution thread + Option(callerMdc).foreach(MDC.setContextMap) + try { + runnable.run() + } finally { + // the thread might be reused, so we clean up for the next use + MDC.clear() + } + } + }) + } + + override def reportFailure(cause: Throwable): Unit = executionContext.reportFailure(cause) +} diff --git a/jvm/src/main/scala/xyz/driver/core/logging/package.scala b/jvm/src/main/scala/xyz/driver/core/logging/package.scala new file mode 100644 index 0000000..2b6fc11 --- /dev/null +++ b/jvm/src/main/scala/xyz/driver/core/logging/package.scala @@ -0,0 +1,7 @@ +package xyz.driver.core + +import org.slf4j.helpers.NOPLogger + +package object logging { + val NoLogger = com.typesafe.scalalogging.Logger(NOPLogger.NOP_LOGGER) +} |