tgr
tgr

Reputation: 3608

How to redirect logging in akka?

I am implementing a distributed database with scala 2.9 and akka 2.0. My current problem is I want to redirect the standard logging to a file instead of stdout. I don't realy want to use SLF4J or SLF4S. Is there a simple way to redirect the logging output?

Upvotes: 4

Views: 5230

Answers (1)

drexin
drexin

Reputation: 24413

The akka documentation for logging says, that you can register handlers in the config like this:

akka {
  # Event handlers to register at boot time (Logging$DefaultLogger logs to STDOUT)
  event-handlers = ["akka.event.Logging$DefaultLogger"]
  # Options: ERROR, WARNING, INFO, DEBUG
  loglevel = "DEBUG"
}

There is also an SLF4J handler

akka.event.slf4j.Slf4jEventHandler

Using this you can add any SLF4J compliant library like logback to write your logs to wherever you want.

edit:

To use logback to log to a file you have to add logback as a dependency, add the Slf4jEventHandler to your config:

akka {
  # Event handlers to register at boot time (Logging$DefaultLogger logs to STDOUT)
  event-handlers = ["akka.event.slf4j.Slf4jEventHandler"]
  # Options: ERROR, WARNING, INFO, DEBUG
  loglevel = "DEBUG"
}

and add a logback config to your project that lokks something like this (taken from logback docs):

<configuration>

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>testFile.log</file>
    <append>true</append>
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="DEBUG">
    <appender-ref ref="FILE" />
  </root>
</configuration>

Due to the async logging in akka you cannot use the %thread variable in your log pattern, instead use the sourceThread variable from the MDC. You can read about that at the bottom of this page: http://doc.akka.io/docs/akka/2.0/scala/logging.html

You don't have to explicitly use slf4j or logback in your code, just use the normal akka logging, the handler will take care of everything else.

Upvotes: 5

Related Questions