scala Akka SLF4J logback 配置和使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14149798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Akka SLF4J logback configuration and usage
提问by Apple Pie
I have done the following steps to try and configure logging for my akka application:
我已完成以下步骤来尝试为我的 akka 应用程序配置日志记录:
created an application.conf file and placed it in src/main/resources. It looks like:
akka { event-handlers = ["akka.event.slf4j.Slf4jEventHandler"] loglevel = "INFO" }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>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("...")
创建了一个 application.conf 文件并将其放置在 src/main/resources 中。看起来像:
akka { event-handlers = ["akka.event.slf4j.Slf4jEventHandler"] loglevel = "INFO" }创建了一个 logback.xml 文件并将其放置在 src/main/resources 中。看起来像:
<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 构建文件中:
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"尝试使用此代码记录:
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?
我错过了一步吗?还是配置错误?
回答by reggoodwin
With this arrangement I can use an akka.event.Logging, no need to specify SLF4J instance.
通过这种安排,我可以使用 akka.event.Logging,无需指定 SLF4J 实例。
(tested 13 Dec 2013)
(2013 年 12 月 13 日测试)
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:
我得到控制台日志记录并记录到文件。为了证明这不是内置记录器,我更改为包含 %X{akkaTimestamp} ,如下所述:
http://doc.akka.io/docs/akka/snapshot/scala/logging.html
build.sbt
生成.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>
This arrangement works when I use an ActorLogging mixin and also create a Logging directly:
当我使用 ActorLogging mixin 并直接创建 Logging 时,这种安排有效:
import akka.event.Logging
val log = Logging(context.system, classOf[NameOfYourActor])
log.info("good luck!")
回答by some some
I came across this problem too but in my case it wasn't a matter of classpath.
我也遇到了这个问题,但就我而言,这不是类路径的问题。
I replaced akka.event.Logging with com.typesafe.scalalogging.slf4j.Logging and it works like a charm!
我用 com.typesafe.scalalogging.slf4j.Logging 替换了 akka.event.Logging ,它就像一个魅力!
my logback.xml (under main/resource):
我的 logback.xml(在 main/resource 下):
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/filename.log</file>
<encoder>
<pattern>%date %level %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<maxIndex>10</maxIndex>
<FileNamePattern>logs/filename.log.%i.gz</FileNamePattern>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>20MB</maxFileSize>
</triggeringPolicy>
</appender>
<root level="info">
<appender-ref ref="FILE" />
</root>
</configuration>
my pom.xml:
我的 pom.xml:
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>scalalogging-slf4j_2.10</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
</dependency>
and in the code:
并在代码中:
import com.typesafe.scalalogging.slf4j.Logging
class LoggingService extends Actor with Logging {
// use logger.info() etc.
}
(didn't use application.conf)
(没有使用application.conf)
回答by Kirill Yankov
try using this application.conf:
尝试使用这个 application.conf:
akka {
loggers = ["akka.event.slf4j.Slf4jLogger"]
loglevel = "DEBUG"
logging-filter = "akka.event.slf4j.Slf4jLoggingFilter"
}
I also have noticed that you probably have wrong class name in you logback config for file appender.
You have <appender name="FILE" class="ch.qos.logback.core.
fileappender">but it should be <appender name="FILE" class="ch.qos.logback.core.FileAppender">.
我还注意到,您的文件追加器的 logback 配置中可能有错误的类名。你有,<appender name="FILE" class="ch.qos.logback.core.
fileappender">但应该是<appender name="FILE" class="ch.qos.logback.core.FileAppender">。
Another issue may be the path to file. Try using absolute file path instead of relative. <file>/absolute/path/testFile.log</file>
另一个问题可能是文件路径。尝试使用绝对文件路径而不是相对路径。<file>/absolute/path/testFile.log</file>
You can also try to change logback version to 1.2.3. Here are akka docs on configuring sl4j: link
您也可以尝试将 logback 版本更改为1.2.3. 这是配置 sl4j 的 akka 文档:链接
回答by iwein
The only thing I would change in your setup is to add the weiglewilczek slf4j adapter.
我要在您的设置中更改的唯一一件事是添加weiglewilczek slf4j 适配器。
I've got the following setup working just fine:
我有以下设置工作得很好:
akka {
event-handlers = ["akka.event.slf4j.Slf4jEventHandler"]
loglevel = DEBUG
}
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>log/app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>log/app.%d{dd-MM-yyyy}.log</fileNamePattern>
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d [%thread] [%class] %5p - %m%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="FILE"/>
</root>
</configuration>
trait test extends com.weiglewilczek.slf4s.Logging {
def logIt() {
logger.info("this logs fine")
}
}

