Search code examples
scalaloggingakka

Akka Logging outside Actor


I have an Akka Actor that makes a call to MyObject.foo(). MyObject is not an Actor. How do I setup Logging in it? With an Actor it's simple, because I can just mixin ActorLogging. In MyObject, I don't have access to context.system. Do I create an akka.event.Logging with AkkaSystem() and then what for the LogSource implicit?


Solution

  • Actually I would redirect Akka logging to and use this API directly in all unrelated classes. First add this to your configuration:

    akka {
        event-handlers = ["akka.event.slf4j.Slf4jEventHandler"]
        loglevel = "DEBUG"
    }
    

    Then choose some SLF4J implementation, I suggest . In your actors continue using ActorLogging trait. In other classes simply rely on SLF4J API - or even better - try out slf4s facade around SLF4J.

    Tip: try out the following logging pattern in Logback:

    <pattern>%d{HH:mm:ss.SSS} | %-5level | %thread | %X{akkaSource} | %logger{1} | %m%n%rEx</pattern>
    

    The %X{akkaSource} will print actor path when available (just like standard logging).