Merge pull request #16649 from meln1k/wip-16630-add-actor-system-name-to-Slf4jLogger-context-meln1k

+slf #16630 Add the actor system name to the Slf4jLogger context
This commit is contained in:
Konrad Malawski 2015-01-16 10:34:09 +01:00
commit 9dae14e1a5
5 changed files with 34 additions and 3 deletions

View file

@ -305,7 +305,7 @@ the first case and ``LoggerFactory.getLogger(String s)`` in the second).
final LoggingAdapter log = Logging.getLogger(system.eventStream(), "my.string");
Logging Thread and Akka Source in MDC
Logging Thread, Akka Source and Actor System in MDC
-------------------------------------
Since the logging is done asynchronously the thread in which the logging was performed is captured in
@ -335,6 +335,15 @@ information is available in the MDC with attribute name ``akkaSource``::
</encoder>
</appender>
Finally, the actor system in which the logging was performed
is available in the MDC with attribute name ``sourceActorSystem``::
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{ISO8601} %-5level %logger{36} %X{sourceActorSystem} - %msg%n</pattern>
</encoder>
</appender>
For more details on what this attribute contains—also for non-actors—please see
`How to Log`_.

View file

@ -341,7 +341,7 @@ the first case and ``LoggerFactory.getLogger(s: String)`` in the second).
val log = Logging(system.eventStream, "my.nice.string")
Logging Thread and Akka Source in MDC
Logging Thread, Akka Source and Actor System in MDC
-------------------------------------
Since the logging is done asynchronously the thread in which the logging was performed is captured in
@ -371,6 +371,15 @@ information is available in the MDC with attribute name ``akkaSource``::
</encoder>
</appender>
Finally, the actor system in which the logging was performed
is available in the MDC with attribute name ``sourceActorSystem``::
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{ISO8601} %-5level %logger{36} %X{sourceActorSystem} - %msg%n</pattern>
</encoder>
</appender>
For more details on what this attribute contains—also for non-actors—please see
`How to Log`_.

View file

@ -56,6 +56,7 @@ object Logger {
class Slf4jLogger extends Actor with SLF4JLogging {
val mdcThreadAttributeName = "sourceThread"
val mdcActorSystemAttributeName = "sourceActorSystem"
val mdcAkkaSourceAttributeName = "akkaSource"
val mdcAkkaTimestamp = "akkaTimestamp"
@ -88,11 +89,13 @@ class Slf4jLogger extends Actor with SLF4JLogging {
MDC.put(mdcAkkaSourceAttributeName, logSource)
MDC.put(mdcThreadAttributeName, logEvent.thread.getName)
MDC.put(mdcAkkaTimestamp, formatTimestamp(logEvent.timestamp))
MDC.put(mdcActorSystemAttributeName, actorSystemName)
logEvent.mdc foreach { case (k, v) MDC.put(k, String.valueOf(v)) }
try logStatement finally {
MDC.remove(mdcAkkaSourceAttributeName)
MDC.remove(mdcThreadAttributeName)
MDC.remove(mdcAkkaTimestamp)
MDC.remove(mdcActorSystemAttributeName)
logEvent.mdc.keys.foreach(k MDC.remove(k))
}
}
@ -104,6 +107,8 @@ class Slf4jLogger extends Actor with SLF4JLogging {
*/
protected def formatTimestamp(timestamp: Long): String =
Helpers.currentTimeMillisToUTCString(timestamp)
private val actorSystemName = context.system.name
}
/**

View file

@ -7,7 +7,7 @@
</appender>
<appender name="TEST" class="akka.event.slf4j.Slf4jLoggerSpec$TestAppender">
<encoder>
<pattern>%date{ISO8601} level=[%level] logger=[%logger] akkaSource=[%X{akkaSource}] sourceThread=[%X{sourceThread}] mdc=[ticket-#%X{ticketNumber}: %X{ticketDesc}] - msg=[%msg]%n----%n</pattern>
<pattern>%date{ISO8601} level=[%level] logger=[%logger] akkaSource=[%X{akkaSource}] sourceActorSystem=[%X{sourceActorSystem}] sourceThread=[%X{sourceThread}] mdc=[ticket-#%X{ticketNumber}: %X{ticketDesc}] - msg=[%msg]%n----%n</pattern>
</encoder>
</appender>
<logger name="akka.event.slf4j.Slf4jLoggingFilterSpec$DebugLevelProducer"

View file

@ -156,6 +156,14 @@ class Slf4jLoggerSpec extends AkkaSpec(Slf4jLoggerSpec.config) with BeforeAndAft
s should include("akkaSource=[Slf4jLoggerSpec$MyLogSource]")
s should include("logger=[akka.event.slf4j.Slf4jLoggerSpec$MyLogSource]")
}
"include actorSystem name in sourceActorSystem" in {
val log = Logging(system.eventStream, classOf[MyLogSource])
log.info("test")
awaitCond(outputString.contains("----"), 5 seconds)
val s = outputString
s should include("sourceActorSystem=[Slf4jLoggerSpec]")
}
}
}