如何在Play Framework 2.0中为Junit测试配置日志记录

时间:2013-08-20 17:39:57

标签: unit-testing logging junit playframework playframework-2.0

使用Eclipse>作为Junit Test或播放命令行运行,只有错误记录到控制台。如何显示在Junit测试期间生成的警告,信息和调试消息?

应用程序/控制器/ 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();
    }
}

测试/ 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>

上面的代码有效地配置日志记录以输出INFO消息,同时从命令行开始播放:

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

产生此输出:

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

但是,运行测试时只显示错误:

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

注意:即使Logger.isErrorEnabled返回false,错误消息也会以测试模式显示..

1 个答案:

答案 0 :(得分:0)

我认为问题是Play开始新的jvm测试并且不包括新的jvm中的-D

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