14

I use a legacy library that writes logs using log4j. My default log4j.properties file directs the log to the console, but in some specific functions of my main program, I would like to disable logging altogether (from all classes).

I tried this:

Logger.getLogger(BasicImplementation.class.getName()).setLevel(Level.OFF);

where "BasicImplementation" is one of the main classes that does logging, but it didn't work - the logs are still written to the console.

Here is my log4j.properties:

log4j.rootLogger=warn, stdout
log4j.logger.ac.biu.nlp.nlp.engineml=info, logfile
log4j.logger.org.BIU.utils.logging.ExperimentLogger=warn

log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %-5p %d{HH:mm:ss} [%t]: %m%n

log4j.appender.logfile = ac.biu.nlp.nlp.log.BackupOlderFileAppender
log4j.appender.logfile.append=false
log4j.appender.logfile.layout = org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern = %-5p %d{HH:mm:ss} [%t]: %m%n
log4j.appender.logfile.File = logfile.log
MaDa
  • 10,511
  • 9
  • 46
  • 84
Erel Segal-Halevi
  • 33,955
  • 36
  • 114
  • 183
  • 1
    It is likely that you have to disable the root logger, but please post your `log4j.properties`/`log4j.xml` first. – MaDa Jan 03 '12 at 09:22

8 Answers8

21

So, you have 3 loggers defined, including the root:

log4j.rootLogger=warn, stdout
log4j.logger.ac.biu.nlp.nlp.engineml=info, logfile
log4j.logger.org.BIU.utils.logging.ExperimentLogger=warn

Unfortunately, to disable them programatically, you need to specify ALL OF THEM in the code:

Logger.getLogger("ac.biu.nlp.nlp.engineml").setLevel(Level.OFF);
Logger.getLogger("org.BIU.utils.logging.ExperimentLogger").setLevel(Level.OFF);
Logger.getRootLogger().setLevel(Level.OFF);

Here's how to reset it back to what's set in the config file.

Community
  • 1
  • 1
MaDa
  • 10,511
  • 9
  • 46
  • 84
9

If you wanna achieve perfect silence (like for a quiet command line tool), you can always use the NullAppender.

Logger.getRootLogger().removeAllAppenders();
Logger.getRootLogger().addAppender(new NullAppender());
schnatterer
  • 7,525
  • 7
  • 61
  • 80
7

You can use

Logger.getRootLogger().setLevel(Level.OFF); 

to disable any logging in java code

Software Engineer
  • 15,457
  • 7
  • 74
  • 102
user1853204
  • 99
  • 1
  • 2
2

In the version 1.8 of java.util.logging.Logger the correctly form is:

Logger.getLogger("").setLevel(Level.OFF);

Tássio Coêlho
  • 334
  • 3
  • 6
0

Perhaps like that:

Logger.getRootLogger().shutdown();

YoelBen
  • 113
  • 8
0

For newer log4j versions, you may consider to call:

LogManager.shutdown();

Instead of:

Logger.shutdown();

which is deprecated. But it still makes the same thing:

Calling this method will safely close and remove all appenders in all the categories including root contained in the default hierachy.

luke8800gts
  • 398
  • 3
  • 7
-2

You could try TO disable it changing your log4j.properties:

log4j.rootLogger=off, stdout
#log4j.logger.ac.biu.nlp.nlp.engineml=info, logfile
#log4j.logger.org.BIU.utils.logging.ExperimentLogger=warn

...

Ref: http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Level.html#OFF

Rick
  • 121
  • 8
-2

Add the following to your log file

log4j.logger.com.class.with.package.BasicImplementation = off, logfiledata

log4j.appender.logfiledata = ac.biu.nlp.nlp.log.BackupOlderFileAppender
log4j.appender.logfiledata.append=false
log4j.appender.logfiledata.layout = org.apache.log4j.PatternLayout
log4j.appender.logfiledata.layout.ConversionPattern = %-5p %d{HH:mm:ss} [%t]: %m%n
log4j.appender.logfiledata.File = logfiledata.log

If the above will not stop if from logging, it will at least log all the data from BasicImplementation class to a separate file.

MaDa
  • 10,511
  • 9
  • 46
  • 84
Neeraj
  • 8,408
  • 8
  • 41
  • 69