Actor记录器未写入日志文件

时间:2018-07-20 00:08:39

标签: playframework akka

在我的Play-Scala应用程序中,log.info("...")命令被忽略。

我已经咨询了以下内容:

Akka actor logging not writing to file

Play Akka logger doesn't output debug messages to console

Akka documentation on logging

我的application.conf文件的相关内容是:

akka {
  loggers = ["akka.event.slf4j.Slf4jLogger"]
  loglevel = "INFO"
  log-config-on-start = on
}
...

logback.xml

<configuration>

  <conversionRule conversionWord="coloredLevel" converterClass="play.api.libs.logback.ColoredLevel" />

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>/var/log/foo/application.log</file>
    <encoder>
      <pattern>%date [%level] from %logger in %thread - %message%n%xException</pattern>
    </encoder>
  </appender>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%coloredLevel %logger{15} - %message%n%xException{10}</pattern>
    </encoder>
  </appender>

  <appender name="ASYNCFILE" class="ch.qos.logback.classic.AsyncAppender">
    <appender-ref ref="FILE" />
  </appender>

  <appender name="ASYNCSTDOUT" class="ch.qos.logback.classic.AsyncAppender">
    <appender-ref ref="STDOUT" />
  </appender>

  <logger name="play" level="INFO" />
  <logger name="application" level="INFO" />
  <logger name="akka" level="INFO" />

  <!-- Off these ones as they are annoying, and anyway we manage configuration ourselves -->
  <logger name="com.avaje.ebean.config.PropertyMapLoader" level="OFF" />
  <logger name="com.avaje.ebeaninternal.server.core.XmlConfigLoader" level="OFF" />
  <logger name="com.avaje.ebeaninternal.server.lib.BackgroundThread" level="OFF" />
  <logger name="com.gargoylesoftware.htmlunit.javascript" level="OFF" />

  <root level="WARN">
    <appender-ref ref="ASYNCFILE" />
    <appender-ref ref="ASYNCSTDOUT" />
  </root>

</configuration>

在我的代码中,我有类似的东西:

class MyActor extends Actor with ActorLogging {
  override def receive = {
    case msg => 
      println("This should show up in the console")
      log.info("log this message", msg)
  }
}

但是,即使终端显示了application.log命令的输出,我的log.info文件也不包含来自println命令的任何消息:

2018-07-20 00:02:59,109 [INFO] from akka.event.slf4j.Slf4jLogger in application-akka.actor.default-dispatcher-4 - Slf4jLogger started
2018-07-20 00:02:59,145 [INFO] from akka.actor.ActorSystemImpl in application-akka.actor.default-dispatcher-4 - {
    # merge of play/reference-overrides.conf @ jar:file:/root/.ivy2/cache/com.typesafe.play/play_2.11/jars/play_2.11-2.6.9.jar!/play/reference-overrides.conf: 20,reference.conf @ jar:file:/root/.ivy2/cache/com.typesafe.akka/akka-actor_2.11/jars/akka-actor_2.11-2.5.14.jar!/reference.conf: 92
    "actor" : {
        # reference.conf @ jar:file:/root/.ivy2/cache/com.typesafe.akka/akka-actor_2.11/jars/akka-actor_2.11-2.5.14.jar!/reference.conf: 717
        # Additional serialization-bindings that are replacing Java serialization are
        # defined in this section for backwards compatibility reasons. They are included
        # by default but can be excluded for backwards compatibility with Akka 2.4.x.
...
    # system properties
    "user" : {
        # system properties
        "dir" : "/home",
        # system properties
        "home" : "/root",
        # system properties
        "language" : "en",
        # system properties
        "name" : "root",
        # system properties
        "timezone" : "Etc/UTC"
    },
1/jars/akka-actor_2.11-2.5.14.jar!/version.conf: 1
    "version" : "2.5.14"
}

ult-dispatcher-2 - Using the following cache for assets configuration:
         enabledCaching = false
         enabledCacheControl = false
         defaultCacheControl = public, max-age=3600
         aggressiveCacheControl = public, max-age=31536000, immutable
         configuredCacheControl:


ork.com/documentation/latest/Filters>):

    play.filters.csrf.CSRFFilter
    play.filters.headers.SecurityHeadersFilter
    play.filters.hosts.AllowedHostsFilter

fault-dispatcher-2 - Application started (Dev)

application.log的内容所示,log-config-on-start = on文件中的行application.conf在工作。

如何获取log.info命令以写入application.conf文件?

1 个答案:

答案 0 :(得分:1)

如果将Play框架与Akka一起使用,则可以利用play框架本身提供的Logging优势。 Play框架会为您创建actor系统,您可以使用PlayLogging记录消息。

如下配置您的logback.xml文件:

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    </appender>

    <appender name="ASYNCSTDOUT" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="STDOUT" />
    </appender>
    <root level="INFO">
        <appender-ref ref="ASYNCSTDOUT" />
    </root>
</configuration>

然后您可以通过扩展PlayLogging特性来利用Play框架提供的日志支持。

class SampleTestActor extends Actor with PlayLogging {

  def receive: Receive = {
    case m: AnyEvent =>
      logger.info(s"Received metric event $m")
  }
}