Changing Logging Levels in Java when Testing

Logging is at the core of the Java Coding Experience, and if you're logging you're probably using some framework on top of SLF4J. The Simple Logging Facade for Java serves as an abstraction layer for logging frameworks like Logback, Log4j and many others.

When I'm running tests I don't want to see the console output. I only want to see this.

Logback-test.xml

This post is going to be really short, because to change logging levels for testing simply add a logback-test.xml file in your /resources directory. In logback-test.xml you can turn off logging to the console or even increase it if you want.

Keep in mind a couple of things. 1) If there are ANY logback-test.xml files in your referenced projects, those will take precedence over your logback.xml file at runtime when not testing. 2) When SLF4J sees a logback-test.xml in your project it will take precedence over your logback.xml file. To ensure your logging at runtime is based on logback.xml and not your logback-test.xml file, add it as an environment variable as shown below in Eclipse (or at the command line.)

More Like This Post