Log4j2 is working nicely with Spring Boot through the log4j2.xml
configuration file in the root classpath, exactly as the documentation states.
When try
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.
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)
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.
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>
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.
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