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.