Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LOG4J2: Disable "No log4j2 configuration file found.." print when configuring programmatically

Tags:

java

log4j2

I am not using any XML configuration file, rather I am setting the logger configuration programmatically. The logger works correctly but just when I call the first line of the code below, a warning ERROR message shows up to tell me that the configuration file was not found and default configuration will be used.

But I don't want this message to be displayed on the console every time I rung the program because I will programmatically add the configuration myself.

The message:

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

It comes when I call the code below:

LoggerContext context = (LoggerContext) LogManager.getContext(false);

Or if I get the root logger first:

Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);

Is there some kind of property that I can set to disable this message from being displayed? Or another approach?.

NOTE: I disabled another notification regarding Log4j2 JMX by setting the property -Dlog4j2.disable.jmx=true. But I couldn't find for this one.

like image 309
Excite Avatar asked Jan 16 '15 23:01

Excite


3 Answers

Add the following system property in order to not show that message:

-Dorg.apache.logging.log4j.simplelog.StatusLogger.level=OFF

Alternatively, and more simply:

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.status.StatusLogger;

...

StatusLogger.getLogger().setLevel(Level.OFF);
like image 92
Paul Vargas Avatar answered Sep 19 '22 21:09

Paul Vargas


You can simply do this in your main class.

static {
    StatusLogger.getLogger().setLevel(Level.OFF);
}
like image 34
Kin Cheung Avatar answered Sep 17 '22 21:09

Kin Cheung


You have to implement initialize method for proper initialization of log4j configurations from code.

Check the "How do I configure log4j2 in code without a configuration file" section in below page which provides the source code also.

http://logging.apache.org/log4j/2.x/faq.html

like image 41
Jagath Ariyarathne Avatar answered Sep 17 '22 21:09

Jagath Ariyarathne