Blankman
Blankman

Reputation: 267320

Why can't I see my log messages for my actors for levels other than error

So I have a play application with sub-modules like this:

/app  // play app
/modules/notifications

Inside my play app I have a task module that starts up my main actor:

Logger.debug("starting main supervisor actor Iot")
val supervisor = system.actorOf(IotSupervisor.props(), "iot-supervisor")

inside of notifications I have this file:

/modules/notifications/src/main/supervisor.scala

package com.example.notifications


import akka.actor.{ Actor, ActorLogging, Props }

object IotSupervisor {
  def props(): Props = Props(new IotSupervisor)
}

class IotSupervisor extends Actor with ActorLogging {
  log.debug("Iot constrcutor called..")
  override def preStart(): Unit = log.info("IoT Application started")
  override def postStop(): Unit = log.info("IoT Application stopped")

  // No need to handle any messages
  override def receive = Actor.emptyBehavior

}

I can see logs in my console:

[debug] application - starting main supervisor actor Iot [info] p.a.h.EnabledFilters - Enabled Filters (see https://www.playframework.com/documentation/latest/Filters):

play.filters.csrf.CSRFFilter
play.filters.headers.SecurityHeadersFilter

[info] play.api.Play - Application started (Dev)

My logback.xml looks like:

<!-- https://www.playframework.com/documentation/latest/SettingsLogger -->
<configuration>

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

  <!--<appender name="FILE" class="ch.qos.logback.core.FileAppender">-->
    <!--<file>${application.home:-.}/logs/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="DEBUG" />
  <logger name="com.example.notifications" LEVEL="TRACE" />




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

I do not see any log messages from inside my actors, unless I use log.error, why is that?

I set the level to TRACE for com.example.notifications

Am I correct in understanding that whenever I use a logger, it will use the logger defined in logback that matches the package name? i.e. com.example.notification will be used to lookup the logger in my logback.xml and use the LEVEL defined there?

Upvotes: 1

Views: 331

Answers (2)

Ivan Stanislavciuc
Ivan Stanislavciuc

Reputation: 7275

You need to read this documentation on how to enable slf4j in akka.

In summary,

add to build.sbt

libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-slf4j" % "2.5.19",
  "ch.qos.logback" % "logback-classic" % "1.2.3"
)

add to application.conf

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

You need to enable the Slf4jLogger in the loggers element in the configuration. Here you can also define the log level of the event bus. More fine grained log levels can be defined in the configuration of the SLF4J backend (e.g. logback.xml). You should also define akka.event.slf4j.Slf4jLoggingFilter in the logging-filter configuration property. It will filter the log events using the backend configuration (e.g. logback.xml) before they are published to the event bus.

Upvotes: 2

Aki
Aki

Reputation: 1754

Ensure that your project depends on "com.typesafe.akka" %% "akka-slf4j" and "ch.qos.logback" % "logback-classic". Otherwise akka might use a logger implentation other than logback.

If that does not solve your issue, try to explicitly use the logger of your ActorSystem:

system.log.debug("starting main supervisor actor Iot")

Upvotes: 1

Related Questions