代码之家  ›  专栏  ›  技术社区  ›  Shafique Jamal

参与者记录器未写入日志文件

  •  1
  • Shafique Jamal  · 技术社区  · 6 年前

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

    作为的内容 应用程序日志 显示,线条 log-config-on-start = on

    1 回复  |  直到 6 年前
        1
  •  1
  •   Chaitanya    6 年前

    如果您与akka一起使用play framework,则可以利用play framework本身提供的日志记录的好处。PlayFramework为您创建参与者系统,您可以使用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>
    

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

    class SampleTestActor extends Actor with PlayLogging {
    
      def receive: Receive = {
        case m: AnyEvent =>
          logger.info(s"Received metric event $m")
      }
    }