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?
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.
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.
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.
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
In Windows, be aware that you need to use a URI with the log4j.configurationFile
property
-Dlog4j.configurationFile=file://C:\path\to\log4j2.xml
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With