Tuesday, 25 November 2014

How to set specific logging for tests in Play Framework 2 ?

Specifying a different logging configuration is very simple.

Play Framework 2's default logging is logback. To set specific logging configurations for tests follow the steps below:
  1. Just create a logback configuration file (eg test-logger.xml):
  2. 
    <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>
    
    
  3. Go to root project folder, open
    build.sbt
    file and add:
    
    javaOptions in Test +="-Dlogger.resource=test-logger.xml"
    
    
    Now whenever you run your tests, the logging configuration written in
    test-logger.xml
    will be read.

No comments:

Post a Comment