aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStewart Stewart <stewinsalot@gmail.com>2017-09-25 18:26:32 -0700
committerGitHub <noreply@github.com>2017-09-25 18:26:32 -0700
commit26a5eb401c48c08812f1292664848595e7f74806 (patch)
tree760ed57257308d9eeb78106911f86fcc76c59308
parent676ddd423470e58dda711a9363027aad98b74a59 (diff)
parent8d975a4d3681d0ba41bd8272f572a78de62e7f89 (diff)
downloaddriver-core-26a5eb401c48c08812f1292664848595e7f74806.tar.gz
driver-core-26a5eb401c48c08812f1292664848595e7f74806.tar.bz2
driver-core-26a5eb401c48c08812f1292664848595e7f74806.zip
Merge pull request #66 from drivergroup/jstjohn/add_slick_mdcv1.2.0
Pull in slickmdc
-rw-r--r--src/main/scala/xyz/driver/core/database/MdcAsyncExecutor.scala53
-rw-r--r--src/main/scala/xyz/driver/core/logging.scala63
-rw-r--r--src/main/scala/xyz/driver/core/logging/DriverLayout.scala58
-rw-r--r--src/main/scala/xyz/driver/core/logging/MdcExecutionContext.scala32
-rw-r--r--src/main/scala/xyz/driver/core/logging/package.scala7
5 files changed, 150 insertions, 63 deletions
diff --git a/src/main/scala/xyz/driver/core/database/MdcAsyncExecutor.scala b/src/main/scala/xyz/driver/core/database/MdcAsyncExecutor.scala
new file mode 100644
index 0000000..5939efb
--- /dev/null
+++ b/src/main/scala/xyz/driver/core/database/MdcAsyncExecutor.scala
@@ -0,0 +1,53 @@
+/** 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
+package database
+
+import java.util.concurrent._
+import java.util.concurrent.atomic.AtomicInteger
+
+import scala.concurrent._
+import com.typesafe.scalalogging.StrictLogging
+import slick.util.AsyncExecutor
+
+import logging.MdcExecutionContext
+
+/** Taken from the original Slick AsyncExecutor and simplified
+ * @see https://github.com/slick/slick/blob/3.1/slick/src/main/scala/slick/util/AsyncExecutor.scala
+ */
+object MdcAsyncExecutor extends StrictLogging {
+
+ /** Create an AsyncExecutor with a fixed-size thread pool.
+ *
+ * @param name The name for the thread pool.
+ * @param numThreads The number of threads in the pool.
+ */
+ def apply(name: String, numThreads: Int): AsyncExecutor = {
+ new AsyncExecutor {
+ val tf = new DaemonThreadFactory(name + "-")
+
+ lazy val executionContext = {
+ new MdcExecutionContext(ExecutionContext.fromExecutor(Executors.newFixedThreadPool(numThreads, tf)))
+ }
+
+ def close(): Unit = {}
+ }
+ }
+
+ def default(name: String = "AsyncExecutor.default"): AsyncExecutor = apply(name, 20)
+
+ private class DaemonThreadFactory(namePrefix: String) extends ThreadFactory {
+ private[this] val group =
+ Option(System.getSecurityManager).fold(Thread.currentThread.getThreadGroup)(_.getThreadGroup)
+ private[this] val threadNumber = new AtomicInteger(1)
+
+ def newThread(r: Runnable): Thread = {
+ val t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement, 0)
+ if (!t.isDaemon) t.setDaemon(true)
+ if (t.getPriority != Thread.NORM_PRIORITY) t.setPriority(Thread.NORM_PRIORITY)
+ t
+ }
+ }
+}
diff --git a/src/main/scala/xyz/driver/core/logging.scala b/src/main/scala/xyz/driver/core/logging.scala
deleted file mode 100644
index 9a5805d..0000000
--- a/src/main/scala/xyz/driver/core/logging.scala
+++ /dev/null
@@ -1,63 +0,0 @@
-package xyz.driver.core
-
-import java.text.SimpleDateFormat
-import java.util.Date
-
-import ch.qos.logback.classic.spi.ILoggingEvent
-import ch.qos.logback.core.LayoutBase
-import org.apache.commons.lang3.StringUtils
-import org.slf4j.helpers.NOPLogger
-
-object logging {
-
- val NoLogger = com.typesafe.scalalogging.Logger(NOPLogger.NOP_LOGGER)
-
- class DriverLayout extends LayoutBase[ILoggingEvent] {
- import scala.collection.JavaConverters._
-
- private val FieldSeparator = "="
- private val DateFormatString = "MM/dd/yyyy HH:mm:ss"
- private val newline = System.getProperty("line.separator")
- private val IgnoredClassesInStack = Set("org.apache.catalina", "org.apache.coyote", "sun.reflect", "javax.servlet")
-
- override def doLayout(loggingEvent: ILoggingEvent): String = {
-
- val date = new SimpleDateFormat(DateFormatString).format(new Date(loggingEvent.getTimeStamp))
- val level = StringUtils.rightPad(loggingEvent.getLevel.toString, 5)
-
- val message = new StringBuilder(s"$date [$level] - ${loggingEvent.getMessage}$newline")
-
- logContext(message, loggingEvent)
-
- Option(loggingEvent.getCallerData) foreach { stacktrace =>
- val stacktraceLength = stacktrace.length
-
- if (stacktraceLength > 0) {
- val location = stacktrace.head
-
- val _ = message
- .append(s"Location: ${location.getClassName}.${location.getMethodName}:${location.getLineNumber}$newline")
- .append(s"Exception: ${location.toString}$newline")
-
- if (stacktraceLength > 1) {
- message.append(stacktrace.tail.filterNot { e =>
- IgnoredClassesInStack.forall(ignored => !e.getClassName.startsWith(ignored))
- } map {
- _.toString
- } mkString newline)
- }
- }
- }
-
- message.toString
- }
-
- private def logContext(message: StringBuilder, loggingEvent: ILoggingEvent) = {
- Option(loggingEvent.getMDCPropertyMap).map(_.asScala).filter(_.nonEmpty).foreach { context =>
- message.append(
- context map { case (key, value) => s"$key$FieldSeparator$value" } mkString ("Context: ", " ", newline)
- )
- }
- }
- }
-}
diff --git a/src/main/scala/xyz/driver/core/logging/DriverLayout.scala b/src/main/scala/xyz/driver/core/logging/DriverLayout.scala
new file mode 100644
index 0000000..5e6c7df
--- /dev/null
+++ b/src/main/scala/xyz/driver/core/logging/DriverLayout.scala
@@ -0,0 +1,58 @@
+package xyz.driver.core
+package logging
+
+import java.text.SimpleDateFormat
+import java.util.Date
+
+import ch.qos.logback.classic.spi.ILoggingEvent
+import ch.qos.logback.core.LayoutBase
+import org.apache.commons.lang3.StringUtils
+
+class DriverLayout extends LayoutBase[ILoggingEvent] {
+ import scala.collection.JavaConverters._
+
+ private val FieldSeparator = "="
+ private val DateFormatString = "MM/dd/yyyy HH:mm:ss"
+ private val newline = System.getProperty("line.separator")
+ private val IgnoredClassesInStack = Set("org.apache.catalina", "org.apache.coyote", "sun.reflect", "javax.servlet")
+
+ override def doLayout(loggingEvent: ILoggingEvent): String = {
+
+ val date = new SimpleDateFormat(DateFormatString).format(new Date(loggingEvent.getTimeStamp))
+ val level = StringUtils.rightPad(loggingEvent.getLevel.toString, 5)
+
+ val message = new StringBuilder(s"$date [$level] - ${loggingEvent.getMessage}$newline")
+
+ logContext(message, loggingEvent)
+
+ Option(loggingEvent.getCallerData) foreach { stacktrace =>
+ val stacktraceLength = stacktrace.length
+
+ if (stacktraceLength > 0) {
+ val location = stacktrace.head
+
+ val _ = message
+ .append(s"Location: ${location.getClassName}.${location.getMethodName}:${location.getLineNumber}$newline")
+ .append(s"Exception: ${location.toString}$newline")
+
+ if (stacktraceLength > 1) {
+ message.append(stacktrace.tail.filterNot { e =>
+ IgnoredClassesInStack.forall(ignored => !e.getClassName.startsWith(ignored))
+ } map {
+ _.toString
+ } mkString newline)
+ }
+ }
+ }
+
+ message.toString
+ }
+
+ private def logContext(message: StringBuilder, loggingEvent: ILoggingEvent) = {
+ Option(loggingEvent.getMDCPropertyMap).map(_.asScala).filter(_.nonEmpty).foreach { context =>
+ message.append(
+ context map { case (key, value) => s"$key$FieldSeparator$value" } mkString ("Context: ", " ", newline)
+ )
+ }
+ }
+}
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)
+}
diff --git a/src/main/scala/xyz/driver/core/logging/package.scala b/src/main/scala/xyz/driver/core/logging/package.scala
new file mode 100644
index 0000000..2b6fc11
--- /dev/null
+++ b/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)
+}