Wei Kleeff
Wei Kleeff

Reputation: 308

How to disable log4j2's auto initialization in console application?

How to disable log4j2's auto initialization in console application? I know it can be disable in servlet context by

 <context-param>
    <param-name>isLog4jAutoInitializationDisabled</param-name>
    <param-value>true</param-value>
</context-param>

but, how in in console application? I guess:

System.setProperty("isLog4jAutoInitializationDisabled", "true");

and I tried, but not.

Upvotes: 2

Views: 1414

Answers (1)

Remko Popma
Remko Popma

Reputation: 36844

When Log4j2 initializes, it needs a configuration. The default configuration is where all ERROR messages are logged to the console, and other messages are ignored.

This is usually not what you want, and you can change this by supplying a configuration that meets your needs. The most common way to do this is to create a file log4j2.xml and put this in the classpath. The configuration file syntax is well documented in the user manual with many, many examples. Especially the pages on appenders and layouts may be of interest.

Log4j2 also supports configuration files in JSON, YAML and properties format (different from the Log4j-1.2 format!), but most of the examples in the manual are xml.

Finally, you can specify the location of the configuration file with system property log4j.configurationFile, so it doesn't have to be on the classpath.

If you have trouble with the configuration, use <Configuration status="trace"> at the top of your configuration file to print internal Log4j2 status messages to the console. (Use WARN to switch it off.)

Since Log4j 2.9, you can enable internal status logging by specifying system property log4j2.debug (without a value). This should help with troubleshooting.

Upvotes: 1

Related Questions