Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the default location of log4j2.xml in Java Spring Boot?

Log4j2 is working nicely with Spring Boot through the log4j2.xml configuration file in the root classpath, exactly as the documentation states.

When trying to move this file to a different location though, I'm not able to pass the new location to Spring at startup. From the documentation:

The various logging systems can be activated by including the appropriate libraries on the classpath, and further customized by providing a suitable configuration file in the root of the classpath, or in a location specified by the Spring Environment property logging.config.

I tried setting the new location with a Java system property

java -jar -Dlogging.config="classpath:/config/log4j2.xml" target/app.jar 

or using an external application.properties containing the relevant property

logging.config=classpath:/config/log4j2.xml 

But I am regularly greeted by the following error message.

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. 
like image 255
Michele Palmia Avatar asked Feb 05 '15 16:02

Michele Palmia


People also ask

Where should log4j2 xml be placed?

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.

How do you set a log4j properties file location?

The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file. The level of the root logger is defined as INFO and attaches the ROLLINGFILE appender to it.

Is log4j default in spring boot?

All the Spring Boot starters depend on spring-boot-starter-logging , which uses Logback by default. For using Log4j2, you need to exclude spring-boot-starter-logging and add spring-boot-starter-log4j2 dependency.

Where does log4j properties file go in spring boot?

properties in the classpath. In a Spring Boot application, the log4j2. properties file will typically be in the resources folder.


1 Answers

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 
like image 148
Michele Palmia Avatar answered Oct 04 '22 05:10

Michele Palmia