Friday 16 October 2015

Log4j2: Configuration

Configuration of log4j2 can be done in two ways.
a.   Using configuration files written in XML, JSON, or YAML,
b.   Programmatically

In this section, I am going to explain log4j2 configuration using configuration files, later posts explain how to configure log4j programmatically.

You can provide log4j2 configurations in properties file, xml file, json, yaml file. When log4j starts, it locate all the ConfigurationFactory plugins and arrange then in weighted order from highest to lowest.

Following are the implementations provided.
Configuration class
Description
PropertiesConfigurationFactory
Creates a PropertiesConfiguration from a properties file.
JsonConfigurationFactory
Creates a PropertiesConfiguration from json properties file.
XmlConfigurationFactory
Creates a PropertiesConfiguration from xml properties file.
YamlConfigurationFactory
Creates a PropertiesConfiguration from yaml properties file.


Following is the order of evaluation to load properties file.
1.   Log4j will inspect the "log4j.configurationFile" system property and, if set, will attempt to load the configuration using the ConfigurationFactory that matches the file extension.
2.   If no system property is set the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.
3.   If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.
4.   If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.
5.   If a test file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the classpath.
6.   If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.
7.   If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.
8.   If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment