92

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

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
cmcginty
  • 113,384
  • 42
  • 163
  • 163

6 Answers6

129

Set level to OFF (instead of DEBUG, INFO, ....)

Edwin
  • 2,671
  • 2
  • 19
  • 24
86

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);
}
Andrew Gilmartin
  • 1,776
  • 12
  • 12
52
 log4j.rootLogger=OFF
flybywire
  • 261,858
  • 191
  • 397
  • 503
15

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.

Rolf
  • 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
10

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;
Cory Klein
  • 51,188
  • 43
  • 183
  • 243
Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
5

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>
Vy Do
  • 46,709
  • 59
  • 215
  • 313