如何在Play Framework 2.0中为Junit测试配置日志记录 [英] How to configure logging in Play Framework 2.0 for Junit tests

查看:91
本文介绍了如何在Play Framework 2.0中为Junit测试配置日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Eclipse>以Junit Test或play命令行运行时,仅将错误记录到控制台.如何显示在Junit测试期间生成的警告,信息和调试消息?

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 ?

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>

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

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/

产生以下输出:

[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

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

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

推荐答案

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

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

这篇关于如何在Play Framework 2.0中为Junit测试配置日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆