From e49ea9b5e4f24d168ee69e4de7af1a38631e2598 Mon Sep 17 00:00:00 2001 From: Nikita Melkozerov Date: Mon, 12 Jan 2015 21:03:42 +0500 Subject: [PATCH] +slf #16630 Add the actor system name to the Slf4jLogger context --- akka-docs/rst/java/logging.rst | 11 ++++++++++- akka-docs/rst/scala/logging.rst | 11 ++++++++++- .../src/main/scala/akka/event/slf4j/Slf4jLogger.scala | 5 +++++ akka-slf4j/src/test/resources/logback-test.xml | 2 +- .../test/scala/akka/event/slf4j/Slf4jLoggerSpec.scala | 8 ++++++++ 5 files changed, 34 insertions(+), 3 deletions(-) diff --git a/akka-docs/rst/java/logging.rst b/akka-docs/rst/java/logging.rst index 8aea87f93e..bfaa7547b7 100644 --- a/akka-docs/rst/java/logging.rst +++ b/akka-docs/rst/java/logging.rst @@ -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``:: +Finally, the actor system in which the logging was performed +is available in the MDC with attribute name ``sourceActorSystem``:: + + + + %date{ISO8601} %-5level %logger{36} %X{sourceActorSystem} - %msg%n + + + For more details on what this attribute contains—also for non-actors—please see `How to Log`_. diff --git a/akka-docs/rst/scala/logging.rst b/akka-docs/rst/scala/logging.rst index d6091003cd..7384b2e98a 100644 --- a/akka-docs/rst/scala/logging.rst +++ b/akka-docs/rst/scala/logging.rst @@ -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``:: +Finally, the actor system in which the logging was performed +is available in the MDC with attribute name ``sourceActorSystem``:: + + + + %date{ISO8601} %-5level %logger{36} %X{sourceActorSystem} - %msg%n + + + For more details on what this attribute contains—also for non-actors—please see `How to Log`_. diff --git a/akka-slf4j/src/main/scala/akka/event/slf4j/Slf4jLogger.scala b/akka-slf4j/src/main/scala/akka/event/slf4j/Slf4jLogger.scala index d3934b5be3..afa11627c1 100644 --- a/akka-slf4j/src/main/scala/akka/event/slf4j/Slf4jLogger.scala +++ b/akka-slf4j/src/main/scala/akka/event/slf4j/Slf4jLogger.scala @@ -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 } /** diff --git a/akka-slf4j/src/test/resources/logback-test.xml b/akka-slf4j/src/test/resources/logback-test.xml index 35249108ea..5a4112faf3 100644 --- a/akka-slf4j/src/test/resources/logback-test.xml +++ b/akka-slf4j/src/test/resources/logback-test.xml @@ -7,7 +7,7 @@ - %date{ISO8601} level=[%level] logger=[%logger] akkaSource=[%X{akkaSource}] sourceThread=[%X{sourceThread}] mdc=[ticket-#%X{ticketNumber}: %X{ticketDesc}] - msg=[%msg]%n----%n + %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