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