| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Akka/Scala example source code file (Slf4jLogger.scala)

This example Akka source code file (Slf4jLogger.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Akka and Scala source code examples by using tags.

All credit for the original source code belongs to; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Akka tags/keywords

actor, akka, debug, error, event, logevent, logger, loggerinitialized, slf4jlogger, slf4jlogging, slflogger, slfloggerfactory, string, util

The Slf4jLogger.scala Akka example source code

 * Copyright (C) 2009-2014 Typesafe Inc. <>

package akka.event.slf4j

import org.slf4j.{ Logger ⇒ SLFLogger, LoggerFactory ⇒ SLFLoggerFactory }
import org.slf4j.MDC
import akka.event.Logging._
import akka.event.DummyClassForStringSources
import akka.util.Helpers

 * Base trait for all classes that wants to be able use the SLF4J logging infrastructure.
trait SLF4JLogging {
  lazy val log = Logger(this.getClass.getName)

 * Logger is a factory for obtaining SLF4J-Loggers
object Logger {
   * @param logger - which logger
   * @return a Logger that corresponds for the given logger name
  def apply(logger: String): SLFLogger = SLFLoggerFactory getLogger logger

   * @param logClass - the class to log for
   * @param logSource - the textual representation of the source of this log stream
   * @return a Logger for the specified parameters
  def apply(logClass: Class[_], logSource: String): SLFLogger = logClass match {
    case c if c == classOf[DummyClassForStringSources] ⇒ apply(logSource)
    case _ ⇒ SLFLoggerFactory getLogger logClass

   * Returns the SLF4J Root Logger
  def root: SLFLogger = apply(SLFLogger.ROOT_LOGGER_NAME)

 * SLF4J logger.
 * The thread in which the logging was performed is captured in
 * Mapped Diagnostic Context (MDC) with attribute name "sourceThread".
class Slf4jLogger extends Actor with SLF4JLogging {

  val mdcThreadAttributeName = "sourceThread"
  val mdcAkkaSourceAttributeName = "akkaSource"
  val mdcAkkaTimestamp = "akkaTimestamp"

  def receive = {

    case event @ Error(cause, logSource, logClass, message) ⇒
      withMdc(logSource, event) {
        cause match {
          case Error.NoCause | null ⇒ Logger(logClass, logSource).error(if (message != null) message.toString else null)
          case _                    ⇒ Logger(logClass, logSource).error(if (message != null) message.toString else cause.getLocalizedMessage, cause)

    case event @ Warning(logSource, logClass, message) ⇒
      withMdc(logSource, event) { Logger(logClass, logSource).warn("{}", message.asInstanceOf[AnyRef]) }

    case event @ Info(logSource, logClass, message) ⇒
      withMdc(logSource, event) { Logger(logClass, logSource).info("{}", message.asInstanceOf[AnyRef]) }

    case event @ Debug(logSource, logClass, message) ⇒
      withMdc(logSource, event) { Logger(logClass, logSource).debug("{}", message.asInstanceOf[AnyRef]) }

    case InitializeLogger(_) ⇒"Slf4jLogger started")
      sender() ! LoggerInitialized

  final def withMdc(logSource: String, logEvent: LogEvent)(logStatement: ⇒ Unit) {
    MDC.put(mdcAkkaSourceAttributeName, logSource)
    MDC.put(mdcThreadAttributeName, logEvent.thread.getName)
    MDC.put(mdcAkkaTimestamp, formatTimestamp(logEvent.timestamp))
    logEvent.mdc foreach { case (k, v) ⇒ MDC.put(k, String.valueOf(v)) }
    try logStatement finally {
      logEvent.mdc.keys.foreach(k ⇒ MDC.remove(k))

   * Override this method to provide a differently formatted timestamp
   * @param timestamp a "currentTimeMillis"-obtained timestamp
   * @return the given timestamp as a UTC String
  protected def formatTimestamp(timestamp: Long): String =

Other Akka source code examples

Here is a short list of links related to this Akka Slf4jLogger.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.