Scala Akka Logging with SLF4J MDC

末鹿安然 提交于 2019-11-27 16:47:06

问题


I'm configuring my Akka application to use the SLF4J logger as specified here:

http://doc.akka.io/docs/akka/2.3.4/scala/logging.html

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.

I have a trait that does this:

I have something as such:

trait ActorLogger {

    val log: DiagnosticLoggingAdapter = Logging(this);

}

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
}

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?

Thanks!


回答1:


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.

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 [[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
  }
}

The non-actor code can use any logger, e.g. mix in the com.typesafe.scalalogging.LazyLogging trait.



来源:https://stackoverflow.com/questions/25135043/scala-akka-logging-with-slf4j-mdc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!