Log configuration in Play

The Play Framework uses Logback as the logging engine. The default configuration is as follows:

<configuration>
    
  <conversionRule conversionWord="coloredLevel" converterClass="play.api.Logger$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 %n%message%n%xException%n</pattern>
     </encoder>
   </appender>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%coloredLevel %logger{15} - %message%n%xException{5}</pattern>
    </encoder>
  </appender>
  
  <logger name="play" level="INFO" />
  <logger name="application" level="DEBUG" />
  
  <!-- Off these ones as they are annoying, and anyway we manage configuration ourself -->
  <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="ERROR">
    <appender-ref ref="STDOUT" />
    <appender-ref ref="FILE" />
  </root>
  
</configuration>

This configuration writes logs in projectHome/logs/application.log. Due to this, one huge file is generated. We could modify this configuration by providing a custom logger.xml.

The custom log file configuration can be set in two ways:

  • By saving the configuration in conf/application-logger.xml or conf/logger.xml. Although using any one of the filenames, such as application-logger.xml or logger.xml, works when both are present, the settings of logger.xml are not applied.
  • By specifying the file via a system property. This method has a higher precedence over the other option.

There are three properties:

  • logger.resource: This property sets a file within the class path
  • logger.file: This property sets a file through its absolute path
  • logger.url: This property sets a file using a URL in this way:
    [app]$ start -Dlogger.url=http://serverPath/conf/appName/logger.xml
    

Another important aspect of configuring logging is by setting the desired log level. We will discuss this in the next section.

Log levels

Log levels can be set in conf/application.conf. The default values are as follows:

# Root logger:
logger.root=ERROR

# Logger used by the framework:
logger.play=INFO

# Logger provided to your application:
logger.application=DEBUG

We can also set the log levels for the classes belonging to specific packages and third-party libraries in this way:

logger.com.apache.cassandra = DEBUG

The supported log levels in the decreasing order of severity are as follows:

  • ERROR
  • WARN
  • INFO
  • DEBUG
  • TRACE

If we wish to turn off logging for some classes or packages, we can set the log level as OFF. This will disable logging for a particular logger.

Note

Some libraries have transitive dependencies on logging libraries. It is best to exclude these logging packages when defining a dependency. It can be done as follows:

"orgName" % "packageName" % "version" excludeAll(
      ExclusionRule(organization = "org.slf4j"),
      ExclusionRule(organization = "ch.qos.logback"))
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.107.152