Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j, configuring a Web App to use a relative path

I have a java webapp that has to be deployed on either Win or Linux machines. I now want to add log4j for logging and I'd like to use a relative path for the log file as I don't want to change the file path on every deployment. The container will most likely be Tomcat but not necessarily.

What's the best way of doing this?

like image 558
Iker Jimenez Avatar asked Oct 19 '08 18:10

Iker Jimenez


1 Answers

Tomcat sets a catalina.home system property. You can use this in your log4j properties file. Something like this:

log4j.rootCategory=DEBUG,errorfile  log4j.appender.errorfile.File=${catalina.home}/logs/LogFilename.log 

On Debian (including Ubuntu), ${catalina.home} will not work because that points at /usr/share/tomcat6 which has no link to /var/log/tomcat6. Here just use ${catalina.base}.

If your using another container, try to find a similar system property, or define your own. Setting the system property will vary by platform, and container. But for Tomcat on Linux/Unix I would create a setenv.sh in the CATALINA_HOME/bin directory. It would contain:

export JAVA_OPTS="-Dcustom.logging.root=/var/log/webapps" 

Then your log4j.properties would be:

log4j.rootCategory=DEBUG,errorfile  log4j.appender.errorfile.File=${custom.logging.root}/LogFilename.log 
like image 68
Steve K Avatar answered Nov 08 '22 08:11

Steve K