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):
     <conversionRule conversionWord="coloredLevel" converterClass="play.api.Logger$ColoredLevel" />
     <appender name="FILE" class="ch.qos.logback.core.FileAppender">
     <pattern>%date - [%level] - from %logger in %thread %n%message%n%xException%n</pattern>
     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
     <pattern>%coloredLevel %logger{15} - %message%n%xException{5}</pattern>
     <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" />
  3. Go to root project folder, open
    file and add:
    javaOptions in Test +="-Dlogger.resource=test-logger.xml"
    Now whenever you run your tests, the logging configuration written in
    will be read.

No comments:

Post a Comment