Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4J2 - How to disable logging in unit test?

Tags:

I am using Log4J v2.0 Beta3 in my application for logging and I am getting log messages generated when I run my unit tests. I checked the API for some way to set the log level to something like CRITICAL but I could not find any way to change the logger configuration.

In fact, I read this on the log4j2 website:

Note that unlike Log4j 1.x, the public Log4j 2 API does not expose methods to add, modify or remove appenders and filters or manipulate the configuration in any way.

So with that said. What is the correct way to disable logging from within unit tests?

like image 327
JBug Avatar asked Jan 04 '13 16:01

JBug


People also ask

How can I disable logging while running unit tests in Java?

You can disable LogLog's output by calling LogLog. setQuietMode(true) .

How do I disable Logback logging?

Logback does not allow logging to be disabled from the command line. However, if the configuration file allows it, you can set the level of loggers on the command line via a Java system property.


1 Answers

I found my answer on the log4j2 website under 'Testing in Maven'. The recommended way seems to be to place a log4j2-test.xml file in src/test/resources. Placing the xml file into this directory will cause it to be used instead of a log4j2.xml.

like image 75
JBug Avatar answered Oct 03 '22 05:10

JBug