How to Disable Log4J Logging from Java Code

How to disable log4j logging from Java code

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.

Disabling Log4J Output in Java

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

Log4J: can I enable/disable logging from a class at runtime?

Try using something like :

LogManager.getRootLogger().setLevel(Level.DEBUG);

You can also get the exact logger passing class into the get.

Is it possible to programmatically disable log4j for one method and methods it will call?

I think for performance reasons you should replace calls like this:

 log.debug("some informantion: " + objA);

with this:

if(log.isDebugEnabled()) {
log.debug("some informantion: " + objA);
}

OR else you can use a FormatLogger class like this: https://stackoverflow.com/a/105908/548225

EDIT

To programmatically disable logging, you can use code like this:

Logger.getLogger("your.logger.name").setLevel(Level.OFF);
Logger.getRootLogger().setLevel(Level.OFF);

How to disable log4j logging in Http Client 4.1 to log to FileAppender

What worked finally,
Logger.getLogger("org.apache.http").setLevel(org.apache.log4j.Level.OFF);
I was not using the right key.

How to disable loggers of a class or of whole package?

In Log4j you can specify a logging level for specified package, class or logger identified by string. You just simply write this in log4j.properties file:

log4j.logger.<your package> = DEBUG|INFO|OFF|WARN...

Disable logging from log4j 2 library, how?

You can control the internal logging Log4j2 prints to the console with the status attribute at the top of the configuration file.

I would recommend that you switch off the verbose debug-level logging but keep the warn and error level logging so you get informed when something goes wrong.

To do this, change the beginning of the configuration file to this:

<Configuration status="WARN">
...


Related Topics



Leave a reply



Submit