1

I am using log4j2 in my spring boot application. This works in all respects re: excluding slf4j, including log4j2, etc.

But when the application deploys I need to customize the file for each target host. I have created an ansible role that does this. Ultimately I end up with a log4j2.xml file deployed in another directory e.g. /prod/produsrX/data/log4j2.xml.

I am using the spring-boot-maven-plugin "repackage" goal to generate an executable jar file. It doesn't seem like that should matter but it is a data point in the problem.

This was supposed to be the easiest part of the project. Always before I have just been able to set -Dlog4j.configurationFile - advice which is echoed on about 3,000 web pages and DOES NOT WORK in Spring Boot 2.1.3.

The most useful info I've found is this question. It talks about using -Dlogging.config because logging must be initialized before other properties are read. Unfortunately that didn't help either.

I did find one example that suggested specifying the above directory in a -classpath parameter to java. But that didn't help either.

Does anyone know how to get a spring boot application to read the log4j2.xml file?

Terry
  • 911
  • 10
  • 26

1 Answers1

0

The property actually has to be put into the application context (e.g. application.yml). Using a -D property does not work!

logging: config: /prod/produsrX/data/log4j2.xml #fully qualified name to your log4j.xml

Terry
  • 911
  • 10
  • 26
  • I'm not sure I follow. As far as I can tell, using -D works. I looked at the SO link in your question, and I tried -Dlogging.config, and it works. More precisely, I use both -Dlog4j.configurationFile and -Dlogging.config, the former for when the app is starting up, and the latter for when it finished the initialization and is actually being used. Both point to the same XML configuration file, which is given as a full path, and they both get used. Even reconfiguring a running app works, by simply changing the configuration file (I didn't do anything to make this happen, it "just worked"). – ciobi Aug 11 '21 at 15:08