我正在配置我的 Akka 应用程序以使用此处指定的 SLF4J 记录器:
I'm configuring my Akka application to use the SLF4J logger as specified here:
doc.akka.io/docs/akka/2.3.4/scala/logging.html
在幕后,我依靠 Logback 进行日志记录.我正在开发一个用于日志记录的通用模块,用户可以在他们的演员系统中使用它.主要是,我正在创建一个他们可以混合的特征.
Underneath the hood, I'm depending on Logback to do the logging. I'm developing a common module for logging purposes that users can use in their actor systems. Mainly, I'm creating a trait they can mixin.
我有一个特点:
我有这样的东西:
trait ActorLogger { val log: DiagnosticLoggingAdapter = Logging(this); }我有一些额外的逻辑可以将 MDC 值添加到 DiagnosticLoggingAdapter 的 MDC.现在的问题是:如果用户想要混合到他们的非演员类中,我会完全公开一个不同的记录器.所以我可能有这样的事情:
I have some extra logic which will add MDC values to the DiagnosticLoggingAdapter's MDC. The problem is now this: I expose a different logger entirely if users want to mixin to their non-actor classes. So I might have something like this:
trait ClassLogger { val log = LoggerFactory getLogger getClass.getName }我希望将 MDC 值转移到这个记录器.例如,如果我将 MDC 值放入我的 DiagnosticAdapterLogger,我应该能够从 org.slf4j.MDC 获取这些值
I want the MDC values to carry over to this logger. So for example, if I put MDC values into my DiagnosticAdapterLogger, I should expect to be able to get those values from org.slf4j.MDC
如何以干净的方式实现这一目标?
How can this be achieved in a clean way?
谢谢!
推荐答案如果你在 actor 系统之外的所有代码都是单线程的(即你没有产生任何额外的期货或线程),那么有一个比那个更简单的解决方案@jasop 引用.
If all your code outside the actor system is single-threaded (i.e. you don't spawn any additional futures or threads), there's a simpler solution than the one @jasop references.
我有这个 mixin 负责填充 MDC 的内部和外部 actor:
I have this mixin that takes care of populating the MDC both inside and outside actors:
import akka.actor.DiagnosticActorLogging import akka.contrib.pattern.ReceivePipeline import org.slf4j.MDC import scala.collection.JavaConverters.mapAsJavaMapConverter trait MdcActorLogging extends DiagnosticActorLogging { this: ReceivePipeline => /** * This is for logging in Akka actors. */ override def mdc(message: Any): akka.event.Logging.MDC = { message match { case MyMessage(requestId) => Map("requestId" -> requestId) case _ => Map() } } /** * This makes the MDC accessible for logging outside of Akka actors by wrapping the actor's * `receive` method. * Implements the [[doc.akka.io/docs/akka/2.4/contrib/receive-pipeline.html ReceivePipeline]] * pattern. */ pipelineOuter { case e @ MyMessage(requestId) => val origContext = MDC.getCopyOfContextMap val mdcWithPath = Map("requestId" -> requestId, // inside actors this is already provided, but outside we have to add this manually "akkaSource" -> self.path.toString) MDC.setContextMap(mdcWithPath.asJava) ReceivePipeline.Inner(evt) // invoke actual actor logic .andAfter { if (origContext != null) MDC.setContextMap(origContext) else MDC.clear() } case e => ReceivePipeline.Inner(e) // pass through } }非演员代码可以使用任何记录器,例如混入 com.typesafe.scalalogging.LazyLogging 特性.
The non-actor code can use any logger, e.g. mix in the com.typesafe.scalalogging.LazyLogging trait.
更多推荐
使用 SLF4J MDC 进行 Scala Akka 日志记录
发布评论