Shafique Jamal
Shafique Jamal

Reputation: 1688

Actor logger not writing to log file

In my Play-Scala application, the log.info("...") commands are being ignored.

I have consulted the following:

Akka actor logging not writing to file

Play Akka logger doesn't output debug messages to console

Akka documentation on logging

The relevant contents of my application.conf file are:

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

and 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>

In my code, I have something like :

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)
  }
}

Yet, my application.log file does not contain any of the messages fro the log.info command, even though the terminal shows the output of the println command:

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)

As the content of application.log shows, the line log-config-on-start = on in the application.conf file is working.

How can I get the log.info commands to write to the application.conf file?

Upvotes: 1

Views: 715

Answers (1)

Chaitanya
Chaitanya

Reputation: 3638

If you are using Play framework along with Akka, you can leverage the benefits of Logging that are provided by the play framework itself. Play framework creates the actor system for you and you can log messages using PlayLogging.

Configure your logback.xml file as follows:

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

You can then leverage the logging support provided by Play framework by extending the PlayLogging trait.

class SampleTestActor extends Actor with PlayLogging {

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

Upvotes: 1

Related Questions