Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify Log4J 2.x config location?

Is there any way to specify Log4J 2.x log4j2.xml file location manually (like DOMConfigurator in Log4J 1.x), without messing with classpath and system properties?

like image 645
Andrei Petrenko Avatar asked May 23 '13 14:05

Andrei Petrenko


People also ask

Where is log4j configuration file located?

Sample log4j Configuration Files During Content Engine installation, two log4j sample files are placed on the system in the ContentEngine\config\samples\ folder: log4j. properties. client: A Java format file that contains client configuration settings.

Where do I put log4j2 xml?

We should put log4j2. xml anywhere in the application's classpath. Log4j will scan all classpath locations to find out this file and then load it. We can find this file mostly placed in the 'src/main/resources' folder.

What is configuration status in log4j2?

Configuration: the root element of a log4j2 configuration file; the status attribute represents the level at which internal log4j events should be logged. Appenders: this element contains a list of appenders; in our example, an appender corresponding to the System console is defined.


2 Answers

You could use the static method #initialize(String contextName, ClassLoader loader, String configLocation) (see source here) in org.apache.logging.log4j.core.config.Configurator. (You can pass null for the class loader.)

Be aware that this class is not part of the public API so your code may break with any minor release.

For completeness, you can also specify the location of the configuration file with this system property:

-Dlog4j.configurationFile=path/to/log4j2.xml 
like image 104
Remko Popma Avatar answered Sep 20 '22 23:09

Remko Popma


In Windows, be aware that you need to use a URI with the log4j.configurationFile property

-Dlog4j.configurationFile=file://C:\path\to\log4j2.xml 
like image 22
Glenn McElhoe Avatar answered Sep 18 '22 23:09

Glenn McElhoe