|
| 1 | +package loggerf.logger.logback |
| 2 | + |
| 3 | +import cats.effect.{IOLocal, unsafe} |
| 4 | +import ch.qos.logback.classic.LoggerContext |
| 5 | +import logback_scala_interop.JLoggerFMdcAdapter |
| 6 | +import org.slf4j.{LoggerFactory, MDC} |
| 7 | + |
| 8 | +import java.util.{Map => JMap, Set => JSet} |
| 9 | +import scala.jdk.CollectionConverters._ |
| 10 | +import scala.util.control.NonFatal |
| 11 | + |
| 12 | +/** @author Kevin Lee |
| 13 | + * @since 2023-07-07 |
| 14 | + */ |
| 15 | +class Ce3MdcAdapterWithIoRuntime(private val ioRuntime: unsafe.IORuntime) extends JLoggerFMdcAdapter { |
| 16 | + |
| 17 | + private[this] val localContext: IOLocal[Map[String, String]] = |
| 18 | + IOLocal[Map[String, String]](Map.empty[String, String]) |
| 19 | + .unsafeRunSync()(ioRuntime) |
| 20 | + |
| 21 | + override def put(key: String, `val`: String): Unit = { |
| 22 | + val unsafeThreadLocal = localContext.unsafeThreadLocal() |
| 23 | + unsafeThreadLocal.set(unsafeThreadLocal.get + (key -> `val`)) |
| 24 | + } |
| 25 | + |
| 26 | + @SuppressWarnings(Array("org.wartremover.warts.Null", "org.wartremover.warts.StringPlusAny")) |
| 27 | + override def get(key: String): String = |
| 28 | + localContext.unsafeThreadLocal().get.getOrElse(key, null) // scalafix:ok DisableSyntax.null |
| 29 | + |
| 30 | + override def remove(key: String): Unit = { |
| 31 | + val unsafeThreadLocal = localContext.unsafeThreadLocal() |
| 32 | + unsafeThreadLocal.set(unsafeThreadLocal.get - key) |
| 33 | + } |
| 34 | + |
| 35 | + override def clear(): Unit = localContext.unsafeThreadLocal().set(Map.empty[String, String]) |
| 36 | + |
| 37 | + override def getCopyOfContextMap: JMap[String, String] = getPropertyMap0 |
| 38 | + |
| 39 | + override def setContextMap0(contextMap: JMap[String, String]): Unit = |
| 40 | + localContext.unsafeThreadLocal().set(contextMap.asScala.toMap) |
| 41 | + |
| 42 | + private def getPropertyMap0: JMap[String, String] = localContext.unsafeThreadLocal().get.asJava |
| 43 | + |
| 44 | + override def getPropertyMap: JMap[String, String] = getPropertyMap0 |
| 45 | + |
| 46 | + override def getKeys: JSet[String] = localContext.unsafeThreadLocal().get.keySet.asJava |
| 47 | + |
| 48 | +} |
| 49 | +object Ce3MdcAdapterWithIoRuntime { |
| 50 | + |
| 51 | + @SuppressWarnings(Array("org.wartremover.warts.Null")) |
| 52 | + private def initialize0(ioRuntime: unsafe.IORuntime): Ce3MdcAdapterWithIoRuntime = { |
| 53 | + val field = classOf[MDC].getDeclaredField("mdcAdapter") |
| 54 | + field.setAccessible(true) |
| 55 | + val adapter = new Ce3MdcAdapterWithIoRuntime(ioRuntime) |
| 56 | + field.set(null, adapter) // scalafix:ok DisableSyntax.null |
| 57 | + field.setAccessible(false) |
| 58 | + adapter |
| 59 | + } |
| 60 | + |
| 61 | + @SuppressWarnings(Array("org.wartremover.warts.AsInstanceOf", "scalafix:DisableSyntax.asInstanceOf")) |
| 62 | + def initialize()(implicit ioRuntime: unsafe.IORuntime): Ce3MdcAdapterWithIoRuntime = { |
| 63 | + val loggerContext = |
| 64 | + LoggerFactory.getILoggerFactory.asInstanceOf[LoggerContext] |
| 65 | + initializeWithLoggerContext(loggerContext) |
| 66 | + } |
| 67 | + |
| 68 | + def initializeWithLoggerContext( |
| 69 | + loggerContext: LoggerContext |
| 70 | + )(implicit ioRuntime: unsafe.IORuntime): Ce3MdcAdapterWithIoRuntime = { |
| 71 | + val adapter = initialize0(ioRuntime) |
| 72 | + try { |
| 73 | + val field = classOf[LoggerContext].getDeclaredField("mdcAdapter") |
| 74 | + field.setAccessible(true) |
| 75 | + field.set(loggerContext, adapter) |
| 76 | + field.setAccessible(false) |
| 77 | + adapter |
| 78 | + } catch { |
| 79 | + case NonFatal(_) => adapter |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments