How to disable log4j logging from Java code

I use log4j to write to the old version Library of logs My default log4j The properties file points logs to the console, but in some specific features of the main program, I want to disable logging completely (from all classes)

I tried this:

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

Among them, "basic implementation" is one of the main classes for performing logging, but it does not work - the log is still written to the console

This 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

Solution

Therefore, you have defined 3 recorders, including the root:

log4j.rootLogger=warn,logfile
log4j.logger.org.BIU.utils.logging.ExperimentLogger=warn

Unfortunately, to disable them programmatically, you need to specify all their topics in your 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 to the content set in the configuration file

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>