How can I change the default location of log4j2.xml in Java Spring Boot?
As mentioned in the log4j2 documentation here, you can include a file named log4j2.component.properties
in your resources folder (or anywhere in the classpath) and inside that file, you can provide the name of the file location (or a new file name) like this:
log4j.configurationFile=path/to/log4j2.xml
or
log4j.configurationFile=classpath:log4j2-custom.xml (if the file is on the classpath)
You can alternatively provide the config file location via the context-param
fields of web.xml
as mentioned here, but I haven't tried that option
(This works with Spring Boot too)
As specified in the Spring reference documentation, the logging.config
property cannot be set among the application properties, as they are read after the logging has already been initialised.
The solution is to provide the path to the external logging configuration this way:
java -Dlogging.config='/path/to/log4j2.xml' -jar app-current.jar