I am developing a logging framework using Log4j. I am not able to figure out how to maintain separate log files for different environment, i.e., development, testing, staging and production.
Asked
Active
Viewed 9,607 times
2 Answers
15
Firstly you'll need a different copy of your log4j.xml for each environment.
Lets call it log4j-dev.xml, log4j-test.xml, log4j-stage.xml and log4j-prod.xml each having their own settings like log file name and log levels.
You then pass in the corresponding file at the the server startup as a system property like below -
-Dlog4j.configuration=log4j-dev.xml
This URL has the example on how to pass this for Tomcat. The concept is the same for whichever server you are deploying on.

JoseK
- 31,141
- 14
- 104
- 131
-
Like @Daniel said in their comment, we had to use -Dlog4j.configurationFile=log4j-dev.xml on an Amazon Linux AMI (Centos) when using log4j v2.13.3 – Mobius May 29 '20 at 14:29
3
On Windows, I have used "set CATALINA_OPTS=-Dlog4j.configurationFile=log4j2-dev.xml"
instead of log4j.configuration
-
1this als works for log4j2 with spring boot on our environments. `-Dlog4j.configurationFile=log4j2-dev.xml` – Daniel Bubenheim Jun 21 '16 at 21:38
-
worth mentioning that this is only relevant if you are using Apache Tomcat – MichaelRom Apr 27 '19 at 20:42