Monday, March 27, 2006

Property/Variable reference in log4j.properties

Keywords:
environment variable log4j.properties

Problem:
In your log4j.properties file a relative reference to a folder is fine as long as you start the program (eg tomcat) the same way from the same location every time.
Eg: log4j.appender.LOGGER.File=../logs/slide.log
(Ok if you're starting tomcat from CATALINA_HOME/bin/catalina.bat)

If not, you will get FileNotFound exceptions when the parent folder can't be found. In the above example, starting tomcat from a windows service will have the user.dir=C:\WINDOWS\SYSTEM32, so ../logs is C:\WINDOWS\logs & doesn't exist.

Solution:
Make explicit file references using system properties in the log4j.properties file. Note, you can not use environment variables (that's a platform specific concept).

System properties can be set in code or for this purpose should be set with the "-D" JVM option.

With the above tomcat example, most of the important environment variables to tomcat are copied into corresponding system properties if you use the standard scripts.
Eg: in catalina.bat: -Dcatalina.home=%CATALINA_HOME%

This allows you to reference the property using ${...} notation
Eg: log4j.appender.LOGGER.File=${catalina.home}/logs/slide.log

4 comments:

Anonymous said...

This worked for me.. thanks for the tip.

Anonymous said...

thank you. it worked for me too.
i want to share that i was deploying a war file created from tapestry and running it through jetty and starting jetty using
java -Dfoo.home=...path.. -jar start.jar just made things happen magically.

Uma Lade said...

This worked for me as well. Thanks for posting

Benny Neugebauer said...

Thank you very much. This helped me! :)