How can one quickly turn off all Log4J output using a log4j.properties
file?

- 31,138
- 14
- 118
- 137

- 113,384
- 42
- 163
- 163
6 Answers
If you want to turn off logging programmatically then use
List<Logger> loggers = Collections.<Logger>list(LogManager.getCurrentLoggers());
loggers.add(LogManager.getRootLogger());
for ( Logger logger : loggers ) {
logger.setLevel(Level.OFF);
}

- 1,776
- 12
- 12
-
6He specified "using a log4j.properties file" anyway it is very useful. – Jaime Hablutzel Jul 18 '11 at 17:53
-
And how do you revert the loggers to the previous setting (programmatically)? – Sachin Sharma Apr 04 '17 at 11:31
-
No magic, just simple programming. Store the current level in a Map
in long-lived context and then revert by iterating over the entry set calling e.getKey().setLevel(e.getValue()) – Andrew Gilmartin Apr 07 '17 at 20:40 -
This doesnt work now as LogManager does not exist – Joehot200 Oct 18 '21 at 14:36
You can change the level to OFF which should get rid of all logging. According to the log4j website, valid levels in order of importance are TRACE, DEBUG, INFO, WARN, ERROR, FATAL. There is one undocumented level called OFF which is a higher level than FATAL, and turns off all logging.
You can also create an extra root logger to log nothing (level OFF), so that you can switch root loggers easily. Here's a post to get you started on that.
You might also want to read the Log4J FAQ, because I think turning off all logging may not help. It will certainly not speed up your app that much, because logging code is executed anyway, up to the point where log4j decides that it doesn't need to log this entry.

- 7,098
- 5
- 38
- 55
-
And underneath TRACE is the "level" ALL, what's kind of the opposite. Secondly, it could be faster if the programm does something like "if(logger.isDebugEnabled()) logger.debug(...)" – Tim Büthe Feb 25 '09 at 10:48
-
The OP doesn't mention performance as the reason for wanting to turn off all logging. When I've needed to do this, it's because many libraries suffer from a kind of logging dysentery, which produces no useful information and I want a short-hand way of dealing with it. – Mark Slater Apr 07 '16 at 09:08
In addition, it is also possible to turn logging off programmatically:
Logger.getRootLogger().setLevel(Level.OFF);
Or
Logger.getRootLogger().removeAllAppenders();
Logger.getRootLogger().addAppender(new NullAppender());
These use imports:
import org.apache.log4j.Logger;
import org.apache.log4j.Level;
import org.apache.log4j.NullAppender;

- 51,188
- 43
- 183
- 243

- 31,138
- 14
- 118
- 137
-
1This is the best solution for standalone apps that shouldn't log anything. – basZero Feb 09 '18 at 10:57
Change level to what you want. (I am using Log4j2, version 2.6.2). This is simplest way, change to <Root level="off">
For example: File log4j2.xml
Development environment
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<Console name="SimpleConsole" target="SYSTEM_OUT">
<PatternLayout pattern="%msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
<Loggers>
<Root level="info">
<AppenderRef ref="SimpleConsole"/>
</Root>
</Loggers>
</Configuration>
Production environment
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<Console name="SimpleConsole" target="SYSTEM_OUT">
<PatternLayout pattern="%msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="off">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
<Loggers>
<Root level="off">
<AppenderRef ref="SimpleConsole"/>
</Root>
</Loggers>
</Configuration>

- 46,709
- 59
- 215
- 313