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, logfile

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


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 :


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:


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


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

What worked finally,
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 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