+act #3671 Implementing support for MDC values defined by the application. Fixes #3671.

* Added LogEvent subclasses with new field for transporting the MDC custom values.
* Slf4jLogger now takes MDC values from new LogEvent field, puts all in MDC before appending the log, and removes all after.
* New trait DiagnosticLoggingAdapter was introduced, which extends LoggingAdapter and adds MDC support with methods to get, set and clear MDC values.
* New factory method added to Logging for getting loggers with MDC support.
* BusLogging was changed to create new LogEvents including the MDC values.
* Actors can mixin with DiagnosticActorLogging which defines a diagnostic logger "log", has a hook to override for defining MDC values per message, and overrides aroundReceive for setting and clearing MDC around receive execution.
* Proper documentation was added for Scala and Java under the Logging/Slf4j section.
This commit is contained in:
Gaston M. Tonietti 2013-10-18 01:10:10 -03:00
parent 05f402c236
commit 9e9d5541b4
12 changed files with 645 additions and 18 deletions

View file

@ -28,6 +28,54 @@ object LoggingDocSpec {
}
//#my-actor
import akka.event.Logging
class MdcActor extends Actor {
val log = Logging(this)
def receive = {
case _ {
//#mdc
val mdc = Map("requestId" -> 1234, "visitorId" -> 5678)
log.mdc(mdc)
// Log something
log.info("Starting new request")
log.clearMDC()
//#mdc
}
}
}
//#mdc-actor
import Logging.MDC
case class Req(work: String, visitorId: Int)
class MdcActorMixin extends Actor with akka.actor.DiagnosticActorLogging {
var reqId = 0
override def mdc(currentMessage: Any): MDC = {
reqId += 1
val always = Map("requestId" -> reqId)
val perMessage = currentMessage match {
case r: Req => Map("visitorId" -> r.visitorId)
case _ => Map()
}
always ++ perMessage
}
def receive: Receive = {
case r: Req => {
log.info(s"Starting new request: ${r.work}")
}
}
}
//#mdc-actor
//#my-event-listener
import akka.event.Logging.InitializeLogger
import akka.event.Logging.LoggerInitialized
@ -69,13 +117,23 @@ object LoggingDocSpec {
class LoggingDocSpec extends AkkaSpec {
import LoggingDocSpec.MyActor
import LoggingDocSpec.{MyActor, MdcActor, MdcActorMixin, Req}
"use a logging actor" in {
val myActor = system.actorOf(Props[MyActor])
myActor ! "test"
}
"use a MDC logging actor" in {
val mdcActor = system.actorOf(Props[MdcActor])
mdcActor ! "some request"
}
"use a MDC logging actor by mixin" in {
val mdcActor = system.actorOf(Props[MdcActorMixin])
mdcActor ! Req("some request", 5678)
}
"allow registration to dead letters" in {
new AnyRef {
//#deadletters