javatomcatlog4j

How to specify Tomcat logs as home for log4j log file


My WAR is using a log4j FileAppender configured via a log4j.properties file under the classes/ directory inside the WAR.

I have configured my log4j appender as such:

# Set the root logger to DEBUG.
log4j.rootLogger=DEBUG

# MonitorLog - used to log messages in the Monitor.log file.
log4j.appender.MonitorAppender=org.apache.log4j.FileAppender
log4j.appender.MonitorAppender.File=MonitorLog.log
log4j.appender.MonitorAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.MonitorAppender.layout.ConversionPattern= %-4r [%t] %-5p %c %x - %m%n

# Use the MonitorAppender to log all messages.
log4j.logger.*=DEBUG,MonitorAppender

With this configuration I am trying to achieve the following:

Does this configuration achieve these items, and if not, what needs to change?


Solution

  • If you know this WAR will only be deployed to a tomcat, you can take advantage of the system property catalina.base, which represents the root of your tomcat base folder (there is also a cataline.home, but they are often the same unless you have multiple tomcats running on the same machine and are sharing the server libs, but i digress).

    So update as follows:

    log4j.appender.MonitorAppender.File=${catalina.base}/logs/MonitorLog.log