如果测试失败,则用于调试输出的Log4j附加程序 [英] Log4j appender for debug output if test fails

查看:71
本文介绍了如果测试失败,则用于调试输出的Log4j附加程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置测试日志,以便主要抑制日志输出-除非测试失败.那么我想获得调试输出.

I would like to set up my test-logging so that log output is mainly suppressed - unless a test fails. then I would like to have the debug output.

我知道仅修改测试类路径中的log4j.properties或仅使调试日志对于测试始终处于活动状态的手动解决方案-但我不想为绿色测试使用不必要的输出来向控制台发送垃圾邮件.

I know the manual solution of just modifying the log4j.properties in the test classpath or just having debug logging always active for tests - but I don't want to spam the console with unnessesary output for green tests.

对于失败的测试,我希望自动构建能够在出现一些奇怪的环境问题的情况下为我提供调试输出.

For failing tests I want the automatic build to give me the debug output in case there are some weired environment issues going on.

我一直在想应该有一个追加器,可以与一个junit规则结合使用,该规则会抑制输出,直到知道测试结果为止,然后仅在测试失败时才将日志推迟到另一个追加器.

I have been thinking there should be an appender that could be combined with a junit rule that supresses output until the result of the test is known and then only deferes logging to another appender if the test has failed.

我可能可以自己做,但是我想知道是否有人曾尝试过此方法,或者是否有更好的解决方案.

I could probably make this myself, but I wonder if anyone has tried this before or if there are better solutions to the problem.

推荐答案

您可以添加一个记录到单独记录器的观察器.

You can add a watcher which logs to a separate logger.

public class MyUnitTest {

  private static Logger errorLog = Logger.getLogger("ErrorLogger");

  @Rule
  public TestWatcher testWatcher = new TestWatcher() {
    @Override
    protected void failed(Throwable e, Description description) {
        errorLog.debug(description.toString(), e);
        super.failed(e, description);
    }
  };

}

然后只需将ErrorLogger添加到您的log4j属性中

Then just add ErrorLogger to your log4j properties

log4j.logger.ErrorLogger=debug, someAppender

这篇关于如果测试失败,则用于调试输出的Log4j附加程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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