Search code examples
scalaplayframeworklogbackplay-framework-2.7

How to enable logging DEBUG-level only for the Application Controllers'?


I'm on Scala 2.12.x Play 2.7.x and as part of my Application Controllers I'm using the logger like this (or equivalently by doing with play.api.Logging):

val logger = play.api.Logger(this.getClass)

and the configuration conf/logback.xml is shown below. The issue is that Application-Controllers' logger.debug(...) statements are not being output in the logs. I assumed that the application was set to DEBUG see one of the lasts entries <logger name="application" level="DEBUG" /> but there is obviously something wrong as the DEBUG statements are not being output?

<!-- 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="slick" level="INFO" />
    <logger name="slick.jdbc" level="DEBUG" />

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

</configuration>

PS: I know I can enable DEBUG for everything but then it is too much information I don't need, I'd like to see what's happening with my Controllers' logging.


Solution

  • The logger instantiated with play.api.Logger(this.getClass) inside a class like so

    package controller
    
    class HomeController extends ... {
      val logger = play.api.Logger(this.getClass)
      logger.debug("msg")  
    }
    

    is not the application logger, but a distinct logger named

    controller.HomeController
    

    Thus to specify its log level add the following config to logback.xml

    <logger name="controllers" level="DEBUG" />
    

    This makes DEBUG the log level of all classes under controllers package.

    Note application logger seems to be deprecated according to docs:

    There is also a play.api.Logger singleton object that allows you to access a logger named application, but its use is deprecated in Play 2.7.0 and above. You should declare your own logger instances...

    If the messages are still not logging try changing from async appender to synchronous ones like so:

    <root level="WARN">
      <appender-ref ref="FILE" />
      <appender-ref ref="STDOUT" />
    </root>