我正在配置我的Akka应用程序,以使用这里指定的SLF4J记录器:
http://doc.akka.io/docs/akka/2.3.4/scala/logging.html在底层,我依靠Logback来进行日志记录。我正在开发一个用于日志记录的通用模块,用户可以在他们的参与者系统中使用它。主要是,我正在创建一个它们可以混合的trait。
我有一个trait是这样做的:
我有这样的东西:
trait ActorLogger {
val log: DiagnosticLoggingAdapter = Logging(this);
}
我有一些额外的逻辑,将MDC值添加到DiagnosticLoggingAdapter的MDC。现在的问题是:如果用户想要混合到他们的非参与者类中,我将完全公开一个不同的记录器。所以我可以这样写:
trait ClassLogger {
val log = LoggerFactory getLogger getClass.getName
}
我希望MDC值传递到这个记录器。因此,例如,如果我将MDC值放入我的DiagnosticAdapterLogger中,我应该能够从org.slf4j.MDC
获得这些值如何以一种干净的方式实现这一目标?
谢谢!
如果actor系统之外的所有代码都是单线程的(即不产生任何额外的future或线程),那么有一个比@jasop引用的更简单的解决方案。
我有这个mixin,它负责填充MDC内部和外部actor:
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 [[http://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
}
}
非参与者代码可以使用任何记录器,例如mix in com.typesafe.scalalogging.LazyLogging
trait。