Log4j appender for debug output if test fails

I would like to configure my test log so that the log output is suppressed mainly if the test has not completed. then I would like to get debug output.

I know the manual solution is only to modify log4j.properties in the test path or just debug logging is always active for tests, but I do not want to spam the console with an ill-conceived output for green tests.

In case of unsuccessful tests, I want the automatic build to give me debug output in case of some problems with wiring in the environment.

I thought that there should be an appender that can be combined with a junit rule that suppresses the output until the test result is known, and then only postpones registration in another application if the test does not work.

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

+4
source share
1 answer

You can add an observer who registers in a separate journal.

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);
    }
  };

}

Then just add log4j ErrorLoggerto your properties

log4j.logger.ErrorLogger=debug, someAppender
0
source

All Articles