Skip to content

Instantly share code, notes, and snippets.

@ysae
Created November 28, 2011 05:48
Show Gist options
  • Save ysae/1399271 to your computer and use it in GitHub Desktop.
Save ysae/1399271 to your computer and use it in GitHub Desktop.
Remove preceding format of thread names and throwable info in logger
diff --git a/akka-actor/src/main/scala/akka/event/Logging.scala b/akka-actor/src/main/scala/akka/event/Logging.scala
index 5b3ae4b..484b23d 100644
--- a/akka-actor/src/main/scala/akka/event/Logging.scala
+++ b/akka-actor/src/main/scala/akka/event/Logging.scala
@@ -428,7 +428,23 @@ object Logging {
}
}
- def stackTraceFor(e: Throwable) = {
+ class EmptyThrowable extends Throwable {
+ val no_stacktrace_message = "[NO STACK TRACE]"
+
+ override def getStackTrace(): Array[java.lang.StackTraceElement] = Array()
+
+ override def printStackTrace(pw: java.io.PrintWriter): Unit =
+ pw.println(no_stacktrace_message)
+
+ override def printStackTrace(sw: java.io.PrintStream): Unit =
+ sw.println(no_stacktrace_message)
+ }
+
+ def safeThrowable(e: Throwable): Throwable =
+ if (e eq null) new EmptyThrowable() else e
+
+ def stackTraceFor(cause: Throwable) = {
+ val e = safeThrowable(cause)
if (e ne null) {
import java.io.{ StringWriter, PrintWriter }
val sw = new StringWriter
diff --git a/akka-slf4j/src/main/scala/akka/event/slf4j/SLF4J.scala b/akka-slf4j/src/main/scala/akka/event/slf4j/SLF4J.scala
index dc175e4..f7380c3 100644
--- a/akka-slf4j/src/main/scala/akka/event/slf4j/SLF4J.scala
+++ b/akka-slf4j/src/main/scala/akka/event/slf4j/SLF4J.scala
@@ -4,7 +4,11 @@
package akka.event.slf4j
-import org.slf4j.{ Logger ⇒ SLFLogger, LoggerFactory ⇒ SLFLoggerFactory }
+import org.slf4j.{
+ Logger ⇒ SLFLogger,
+ LoggerFactory ⇒ SLFLoggerFactory,
+ MDC ⇒ SLFMDC
+}
import akka.event.Logging._
import akka.actor._
@@ -31,22 +35,35 @@ object Logger {
*/
class Slf4jEventHandler extends Actor with SLF4JLogging {
+ private val thread_name_attr = "actor_thread_name"
+
+ def with_mdc_attr(name: String, value: String)(log_statement: ⇒ Unit) {
+ SLFMDC.put(name, value)
+ log_statement
+ SLFMDC.remove(name)
+ }
+
def receive = {
case event @ Error(cause, logSource, message) ⇒
- Logger(logSource).error("[{}] [{}] [{}]",
- Array[AnyRef](event.thread.getName, message.asInstanceOf[AnyRef], stackTraceFor(cause)))
+ with_mdc_attr(thread_name_attr, event.thread.getName) {
+ Logger(logSource).error(message.asInstanceOf[String],
+ safeThrowable(cause))
+ }
case event @ Warning(logSource, message) ⇒
- Logger(logSource).warn("[{}] [{}]",
- event.thread.getName, message.asInstanceOf[AnyRef])
+ with_mdc_attr(thread_name_attr, event.thread.getName) {
+ Logger(logSource).warn(message.asInstanceOf[String])
+ }
case event @ Info(logSource, message) ⇒
- Logger(logSource).info("[{}] [{}]",
- event.thread.getName, message.asInstanceOf[AnyRef])
+ with_mdc_attr(thread_name_attr, event.thread.getName) {
+ Logger(logSource).info(message.asInstanceOf[String])
+ }
case event @ Debug(logSource, message) ⇒
- Logger(logSource).debug("[{}] [{}]",
- event.thread.getName, message.asInstanceOf[AnyRef])
+ with_mdc_attr(thread_name_attr, event.thread.getName) {
+ Logger(logSource).debug(message.asInstanceOf[String])
+ }
case InitializeLogger(_) ⇒
log.info("Slf4jEventHandler started")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment