ActorLogging
中的DiagnosticActorLogging
和Akka
特征之间的区别是什么?您应该何时优先于另一个?快速查看docs并不能提供很多指导。
答案 0 :(得分:1)
MDC允许您为记录的邮件添加其他上下文。例如,对于日志记录模式%5p %X{user-id} %m%n
,在mdc map中插入user-id
将替换模板字段%X{user-id}
中的值
DiagnosticActorLogging
为您提供了对mdc
的轻松访问,并负责为actor处理的每条消息设置和清除mdc。请查看aroundReceive
特征中的DiagnosticActorLogging
方法,以便更好地理解
override protected[akka] def aroundReceive(receive: Actor.Receive, msg: Any): Unit = try {
log.mdc(mdc(msg))
super.aroundReceive(receive, msg)
} finally {
log.clearMDC()
}
答案 1 :(得分:0)
最简单的解决方法是检查DiagnosticActorLogging来源:
/**
* Scala API: Mix in DiagnosticActorLogging into your Actor to easily obtain a reference to a logger with MDC support,
* which is available under the name "log".
* In the example bellow "the one who knocks" will be available under the key "iam" for using it in the logback pattern.
*
* {{{
* class MyActor extends Actor with DiagnosticActorLogging {
*
* override def mdc(currentMessage: Any): MDC = {
* Map("iam", "the one who knocks")
* }
*
* def receive = {
* case "pigdog" => log.info("We've got yet another pigdog on our hands")
* }
* }
* }}}
*/
trait DiagnosticActorLogging extends Actor {
import akka.event.Logging._
val log = akka.event.Logging(this)
def mdc(currentMessage: Any): MDC = emptyMDC
override protected[akka] def aroundReceive(receive: Actor.Receive, msg: Any): Unit = try {
log.mdc(mdc(msg))
super.aroundReceive(receive, msg)
} finally {
log.clearMDC()
}
}
基本上,它允许隐式地将一些共享的日志记录数据关联到actor中的每个日志记录调用,而不会明确地添加它们。根据您的应用程序,它可能对主机名,请求ID,客户端ID,jar版本,构建日期,端点等等有用。