How to configure logging in Play Framework 2.0 for

2019-04-02 08:15发布

While using Eclipse > Run as Junit Test or the play command line, only the errors are logged to the console. How to show warning, info, and debug messages generated during Junit tests ?

app/controllers/Application.java

package controllers;

import play.*;
import play.mvc.*;

public class Application extends Controller {
    public static Result index() {
        Logger.trace("**** Logger.isTraceEnabled = " + Logger.isTraceEnabled()+ " *****");
        Logger.debug("**** Logger.isDebugEnabled = " + Logger.isDebugEnabled()+ " *****");
        Logger.info("**** Logger.isInfoEnabled = " + Logger.isInfoEnabled()+ " *****");
        Logger.warn("**** Logger.isWarnEnabled = " + Logger.isWarnEnabled()+ " *****");
        Logger.error("**** Logger.isErrorEnabled = " + Logger.isErrorEnabled()+ " *****");
        return ok();
    }
}

test/ApplicationTest.java

import org.junit.*;
import play.Logger;
import static org.junit.Assert.assertTrue;

public class ApplicationTest {
    @Test
    public void loggingTest() {
        Logger.trace("**** Logger.isTraceEnabled = " + Logger.isTraceEnabled()+ " *****");
        Logger.debug("**** Logger.isDebugEnabled = " + Logger.isDebugEnabled()+ " *****");
        Logger.info("**** Logger.isInfoEnabled = " + Logger.isInfoEnabled()+ " *****");
        Logger.warn("**** Logger.isWarnEnabled = " + Logger.isWarnEnabled()+ " *****");
        Logger.error("**** Logger.isErrorEnabled = " + Logger.isErrorEnabled()+ " *****");
        assertTrue("should be true", true);
    }
}

conf/application.conf

[...]
# Logger
# ~~~~~
# You can also configure logback (http://logback.qos.ch/), by providing a logger.xml file in the conf directory .
# (no logger configuration here)

conf/prod-logger.xml

<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="INFO" />

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

</configuration>

The code above effectively configures logging to output INFO messages, while starting play from the command line:

play -Dlogger.file=conf/prod-logger.xml start
curl http://localhost:9000/

yields this output:

[info] application - **** Logger.isInfoEnabled = true *****
[warn] application - **** Logger.isWarnEnabled = true *****
[error] application - **** Logger.isErrorEnabled = true *****

However only errors are shown when running tests:

david@localhost:~$ play -Dlogger.file=conf/prod-logger.xml test
13:19:10.354 [main] ERROR application - **** Logger.isErrorEnabled = false *****
[info] ApplicationTest
[info] + ApplicationTest.loggingTest
[info] 
[info] 
[info] Total for test ApplicationTest
[info] Finished in 0.034 seconds

Note: error messages are shown in test mode even when Logger.isErrorEnabled returns false..

1条回答
三岁会撩人
2楼-- · 2019-04-02 08:46

I believe the problem is that Play starts a new jvm for test and does not include the -D in the new jvm

http://play.lighthouseapp.com/projects/82401/tickets/981-overriding-configuration-for-tests

查看更多
登录 后发表回答