Akka SLF4J logback配置和用法
我已完成以下步骤尝试为我的Akka应用程序配置日志记录:
I have done the following steps to try and configure logging for my akka application:
-
创建了一个application.conf文件,并将其放在src/main/resources中.看起来像:
created an application.conf file and placed it in src/main/resources. It looks like:
akka {
event-handlers = ["akka.event.slf4j.Slf4jEventHandler"]
loglevel = "INFO"
}
创建了一个logback.xml文件并将其放置在src/main/resources中.看起来像:
created a logback.xml file and placed it in src/main/resources. It looks like:
<configuration>
<appender name="FILE" class="ch.qos.logback.core.fileappender">
<File>./logs/akka.log</File>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%-5level] %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="FILE" />
</root>
</configuration>
在我的.scala sbt构建文件中添加了以下内容:
added the following to my .scala sbt build file:
libraryDependencies += "com.typesafe.akka" % "akka-slf4j" % "2.0.3",
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.0.9"
lazy val logback = "ch.qos.logback" % "logback-classic" % "1.0.9"
尝试使用此代码进行记录:
attempted this code to log:
import akka.event.Logging
val log = Logging(context.system, this)
log.info("...")
我得到的只是标准输出日志记录,没有使用日志创建日志文件.
All I am getting is standard output logging, no log file creation with the logs.
我错过了一步吗?还是配置错误?
Have I missed a step ? Or misconfigured something?
通过这种安排,我可以使用akka.event.Logging,无需指定SLF4J实例.
With this arrangement I can use an akka.event.Logging, no need to specify SLF4J instance.
(2013年12月13日测试)
(tested 13 Dec 2013)
我得到控制台日志并记录到文件. 为了证明这不是内置记录器,我更改为包括%X {akkaTimestamp},如下所述:
I get console logging and logging to a file. To prove this is not built-in logger I changed to include %X{akkaTimestamp} as explained here:
http://doc.akka.io/docs/akka/snapshot/scala/logging.html
build.sbt
build.sbt
library dependencies: (Akka version 2.2.3)
...
"com.typesafe.akka" %% "akka-slf4j" % "2.2.3"
"ch.qos.logback" % "logback-classic" % "1.0.9"
...
src/main/resources/application.conf
src/main/resources/application.conf
akka {
loggers = ["akka.event.slf4j.Slf4jLogger"]
loglevel = "INFO"
}
src/main/resources/logback.xml
src/main/resources/logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<target>System.out</target>
<encoder>
<pattern>%X{akkaTimestamp} %-5level[%thread] %logger{0} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>log/akka.log</file>
<append>false</append>
<encoder>
<pattern>%date{yyyy-MM-dd} %X{akkaTimestamp} %-5level[%thread] %logger{1} - %msg%n</pattern>
</encoder>
</appender>
<logger name="akka" level="INFO" />
<root level="DEBUG">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
</configuration>
当我使用ActorLogging混合并直接创建Logging时,这种安排有效:
This arrangement works when I use an ActorLogging mixin and also create a Logging directly:
import akka.event.Logging
val log = Logging(context.system, classOf[NameOfYourActor])
log.info("good luck!")