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

8

28

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.
Threlkeld answered 5/2, 2015 at 16:38 Comment(6)
Could you please confirm if /config is in the classpath? if you are using maven based project, put the xml file under src/main/resourcesTrembly
The config folder (package) is in the classpath, and it already contains application.yml, that is correctly picked up and succesfully used in the application.Threlkeld
Could you please put the xml file in src/main/resources and try so that we can narrow down the issue?Trembly
If I put the XML file in its right location, the classpath root (src/main/java), it works without problems. I'm not getting what we should check by putting the XML in the src/main/resources folder. What I want to be able to do is to put the log4j2.xml file wherever I want.Threlkeld
Since it is working fine when you put it in src/main/java, it means there is no problem with the configuration. If you want it to work irrespective of the location of the log4j2.xml, you have to make sure that the folder is in classpath. If you are using eclipse, right click on the project -> Build Path -> Configure Build Path to set the classpath.Trembly
I found out the right way to include an external configuration file for Log4j2 is mvn spring-boot:run -Dlogging.config="/path/to/file/test.xml" (or similarly when using java -jar). Still, I've not been able to include a file already in the classpath at build time. I'm not sure that's actually possible.Threlkeld
T
44

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
Threlkeld answered 21/5, 2015 at 14:0 Comment(3)
There's another solution via the log4j2.component.properties file as described in my answer below which doesn't necessitate providing the config file name via system properties or command line parametersDispread
From the documentation I read: The various logging systems can be activated by including the appropriate libraries on the classpath and can be further customized by providing a suitable configuration file in the root of the classpath or in a location specified by the following Spring Environment property: logging.config. The only exception is for ` logging from @PropertySources in Spring @Configuration file` which is dictated by initialization order. Hence, why "logging.config property cannot be set among the application properties"?Wallah
look at this answerWallah
D
17

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)

Dispread answered 5/12, 2019 at 7:57 Comment(0)
M
8

The answer of micpalmia is absolutely correct.

I needed to put the configuration outside the classpath I didn't want to pass the config file as a parameter. So i put a very simple logging configuration in the classpath resources and had the spring boot application reconfigure logging upon start, like so:

@SpringBootApplication
public class Application implements CommandLineRunner {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... param) throws UnsupportedEncodingException {
        Configurator.initialize(null, "config/log4j2.xml");
        // ...
    }
}

This approach has a significant disadvantage: The whole application boot process will not be logged as externally configured. But once the custom code is run the logger works as intended. While you may not, I find this to be a compromise I can live with.

Mohn answered 8/3, 2016 at 10:25 Comment(2)
Regarding the boot process, won't using both -Dlog4j.configurationFile and -Dlogging.config take care of this? (It does for me, but I'm not sure we have the same use case.)Suspend
Check this answer as well. linkTortious
M
2

I have the same problem in my project, besides log4j2.xml I also need other config files in the class path. Here is my 2 solutions that works:

Soluation 1 : Start spring boot application with org.springframework.boot.loader.JarLauncher

java -classpath SpringBootProject.jar;./config org.springframework.boot.loader.JarLauncher

Solution 2: Write a './config' class path entry in the MANIFEST.MF in the jar

    <build>
    <plugins>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifestEntries>
               <Class-Path>./config/</Class-Path>
            </manifestEntries>
          </archive>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>1.5.3.RELEASE</version>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
Marna answered 8/7, 2017 at 4:13 Comment(0)
G
2

In case of property file:

java -Dlog4j.configuration=file:/path/to/log4j.properties -jar app.jar

To the command line works in Spring Boot 2. Don't forget to add file: before the path.

Gladstone answered 13/2, 2019 at 10:5 Comment(0)
K
0

I have working solution to set custom path or change existing file path for logging file. If you have configured log4j2.xml file, open it and see where you have to do one line change to config log file path.enter image description here

Kilmer answered 5/6, 2018 at 7:22 Comment(2)
This doesn't answer the question. The question was asking about the location of the log4j configuration file (log4j.xml) rather than the file for writing the log statements toRosales
This also looks a whole lot like AnyPoint Studio and not a "normal" Java ApplicationPropjet
R
0

If you try to rename the log4j2.xml file other than the default name. In order to load the file. For a spring boot application in application.properties file add below property this would work

logging.config=classpath:logfilename.xml
Rachmaninoff answered 18/8, 2022 at 9:51 Comment(0)
H
0

Another way to do this in Spring boot

 java -jar app.jar --logging.config=/path/to/log4j2.xml
Hyperopia answered 3/3, 2023 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.