How is the log4j2 configuration file loaded in order?

The loading sequence of the log4j2 configuration file is as follows:

  1. Search for the log4j2-test.properties file in the classpath.
  2. If the log4j2-test.properties file is not found, then look for the log4j2-test.yaml file in the classpath.
  3. If the log4j2-test.properties and log4j2-test.yaml files are not found, search for the log4j2-test.yml file in the classpath.
  4. If the files log4j2-test.properties, log4j2-test.yaml, and log4j2-test.yml are not found, then the log4j2.properties file will be searched for in the classpath.
  5. If the log4j2.properties file is not found, look for the log4j2.yaml file in the classpath.
  6. If the files log4j2.properties and log4j2.yaml are not found, search for the file log4j2.yml in the classpath.
  7. If no configuration file is found, use the default configuration.

Please note that the loading order here is based on the alphabetical order of the file names. If there are multiple configuration files, they will be loaded in the order mentioned above, with the later loaded files overriding the earlier loaded configurations.

Additionally, you can specify the path to the configuration file by specifying the system property -Dlog4j.configurationFile=path/to/log4j2.xml at startup. This method will prioritize the specified configuration file instead of loading it in the order mentioned above.

bannerAds