diff options
author | Stewart Stewart <stewart@driver.xyz> | 2017-09-25 18:51:53 -0700 |
---|---|---|
committer | Stewart Stewart <stewart@driver.xyz> | 2017-09-25 18:52:20 -0700 |
commit | 8d975a4d3681d0ba41bd8272f572a78de62e7f89 (patch) | |
tree | 760ed57257308d9eeb78106911f86fcc76c59308 /src/main/scala/xyz/driver/core/logging/MdcExecutionContext.scala | |
parent | 508d59ea5e2865a5bfc6d380527b1ab91e0fddd5 (diff) | |
download | driver-core-8d975a4d3681d0ba41bd8272f572a78de62e7f89.tar.gz driver-core-8d975a4d3681d0ba41bd8272f572a78de62e7f89.tar.bz2 driver-core-8d975a4d3681d0ba41bd8272f572a78de62e7f89.zip |
Add MdcExecutionContext to logging pacagkev1.1.1
Diffstat (limited to 'src/main/scala/xyz/driver/core/logging/MdcExecutionContext.scala')
-rw-r--r-- | src/main/scala/xyz/driver/core/logging/MdcExecutionContext.scala | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/core/logging/MdcExecutionContext.scala b/src/main/scala/xyz/driver/core/logging/MdcExecutionContext.scala new file mode 100644 index 0000000..9f8db3e --- /dev/null +++ b/src/main/scala/xyz/driver/core/logging/MdcExecutionContext.scala @@ -0,0 +1,32 @@ +/** 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 + // scalastyle:off + if (callerMdc != null) MDC.setContextMap(callerMdc) + 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) +} |