2

在使用 Eclipse > Run as Junit Test 或 play 命令行时,只会将错误记录到控制台。如何显示在 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,也会在测试模式下显示错误消息。

4

1 回答 1

0

我认为问题在于 Play 启动了一个新的 jvm 进行测试,并且在新 jvm 中不包含 -D

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

于 2013-08-21T19:39:10.777 回答