7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Akka - Log into files

Posted at

Ref:
[1]: Stackoverflow - How to redirect logging in akka?
[2]: How to setup SLF4J and LOGBack in a web app - fast
[3]: Stackoverflow - Akka (2.3.0) fails to load Slf4jEventHandler class with java.lang.ClassNotFoundException
[4]: Akka - Logging

It is essentially import to write logs in the file system when you are tracking issues or benchmark your Akka program. And it is quite a surprise that Akka can not directly log into files.

To achieve this, you will also need logback and slf4j.

NOTE:

All the dependency for this issue are changing these days, the version of Akka matters much. How you do this may change slightly according to your own version, but still could hurt much (at least to me).

Here is some version info of mine:

  • Akka: 2.3.4

Add dependency

In your build.sbt

resolvers += "Typesafe Repo" at "http://repo.typesafe.com/typesafe/releases/"

libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-actor" % "2.3.4",
  "com.typesafe.akka" % "akka-slf4j_2.10" % "2.3.4",
  "ch.qos.logback" % "logback-classic" % "1.0.13",
)

Config Akka

In your /src/resources/application.conf, change default logger to Slf4jLogger.

akka {
    loglevel = "DEBUG"
    loggers = ["akka.event.slf4j.Slf4jLogger"]
}

Config logback

Create a file named logback.xml in /src/main/resources to config logback.

<configuration>
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>log/log.log</file>
        <append>true</append>
        <!-- encoders are assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%d,%msg%n</pattern>
        </encoder>
    </appender>

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

The destination of log is in file element.
The pattern of each line is set in pattern element.

Other config could refer to Logback's official document.

There is one thing show be pointed out: the date time in the pattern %d, is not the time when program call Log.info(msg) but the time when the line is written.

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?