27

我已完成以下步骤来尝试为我的 akka 应用程序配置日志记录:

  • 创建了一个 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("...")

我得到的只是标准输出日志,没有使用日志创建日志文件。

我错过了一步吗?还是配置错误?

4

4 回答 4

27

With this arrangement I can use an akka.event.Logging, no need to specify SLF4J instance.

(tested 13 Dec 2013)

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

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

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

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:

import akka.event.Logging

val log = Logging(context.system, classOf[NameOfYourActor])

log.info("good luck!")
于 2013-12-13T16:47:18.680 回答
4

我也遇到了这个问题,但就我而言,这不是类路径的问题。

我用 com.typesafe.scalalogging.slf4j.Logging 替换了 akka.event.Logging,它就像一个魅力!

我的 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>

我的 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>

并在代码中:

import com.typesafe.scalalogging.slf4j.Logging

class LoggingService extends Actor with Logging {
  // use logger.info() etc.
}

(没有使用application.conf)

于 2013-06-30T08:24:31.070 回答
3

尝试使用这个 application.conf:

akka {
  loggers = ["akka.event.slf4j.Slf4jLogger"]
  loglevel = "DEBUG"
  logging-filter = "akka.event.slf4j.Slf4jLoggingFilter"
}

我还注意到您可能在文件附加程序的 logback 配置中使用了错误的类名。你有<appender name="FILE" class="ch.qos.logback.core. fileappender">,但应该是<appender name="FILE" class="ch.qos.logback.core.FileAppender">

另一个问题可能是文件的路径。尝试使用绝对文件路径而不是相对路径。<file>/absolute/path/testFile.log</file>

您也可以尝试将 logback 版本更改为1.2.3. 以下是有关配置 sl4j 的 akka 文档:链接

于 2018-09-06T10:42:07.377 回答
1

我要在您的设置中更改的唯一一件事是添加weiglewilczek slf4j 适配器

我的以下设置工作得很好:

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")
  }
}
于 2013-04-10T10:06:26.243 回答