diff --git a/build.sbt b/build.sbt index a6af7ed4..813b1b2a 100644 --- a/build.sbt +++ b/build.sbt @@ -18,7 +18,7 @@ lazy val commonSettings = commonSmlBuildSettings ++ ossPublishSettings ++ Seq( val scalaTest = "org.scalatest" %% "scalatest" % "3.2.19" % Test val slf4j = "org.slf4j" % "slf4j-api" % "2.0.17" -val logback = "ch.qos.logback" % "logback-classic" % "1.5.16" +val logback = "ch.qos.logback" % "logback-classic" % "1.5.17" // used during CI to verify that the documentation compiles val compileDocumentation: TaskKey[Unit] = taskKey[Unit]("Compiles documentation throwing away its output") diff --git a/mdc-logback/src/main/scala/ox/logback/InheritableMDC.scala b/mdc-logback/src/main/scala/ox/logback/InheritableMDC.scala index dc2d1bd5..0c7056eb 100644 --- a/mdc-logback/src/main/scala/ox/logback/InheritableMDC.scala +++ b/mdc-logback/src/main/scala/ox/logback/InheritableMDC.scala @@ -21,7 +21,7 @@ object InheritableMDC: private val logger: Logger = LoggerFactory.getLogger(getClass.getName) private[logback] val currentContext: ForkLocal[Option[MDCAdapter]] = ForkLocal(None) - /** Initialise inheritable MDCs. Must be called as early in the app's code as possible. */ + /** Initialize inheritable MDCs. Must be called as early in the app's code as possible. */ lazy val init: Unit = // Obtaining the current provider, to replace it later with our implementation returning the correct MDCAdapter. val getProviderMethod = classOf[LoggerFactory].getDeclaredMethod("getProvider") @@ -41,6 +41,12 @@ object InheritableMDC: providerField.setAccessible(true) providerField.set(null, new OverrideMDCAdapterDelegateProvider(currentProvider, scopedValuedMDCAdapter)) + // in Logback 1.5.17+ the MDC class is initialized earlier, which means that the MDC adapter is stored earlier + // hence, overwriting it here as well + val mdcStaticField = classOf[MDC].getDeclaredField("MDC_ADAPTER") + mdcStaticField.setAccessible(true) + mdcStaticField.set(null, scopedValuedMDCAdapter) + logger.info(s"Scoped-value based MDC initialized") case currentProvider => logger.warn(s"A non-Logback SLF4J provider ($currentProvider) is being used, unable to initialize scoped-value based MDC")