log4j2 specify relative path to tomcat home dir for FileAppender

TomcatLoggingLog4j2Fileappender

Tomcat Problem Overview


I am trying to configure log4j2 for my tomcat server running locally. I am unable to pipe the logs to a location that is relative to the tomcat install.

If I specify an absolute path it works. If I use a relative path it outputs to the location of System.getProperty("user.dir").

But when I try to use either ${catalina.home} or ${catalina.base} it does not work. If I output those two system properties on startup, they are defined and point to the proper location.

Also,

  • Tomcat 7.0.26
  • servlet 2.5
  • log4j2.0 beta 8
  • we are using slf4j with log4j2 behind it.
  • I setup all loggers to be async through the environment variable -DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector.

My feeling is that Tomcat might not be fully initialized when it processes the log4j2.xml? Any ideas would be greatly appreciated!

Here is my log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="debug" name="LoggingConfig"> 
  <appenders>
    <FastFile name="ALog" fileName="${catalina.home}/logs/test.log" immediateFlush="true" append="true">
      <PatternLayout>
        <pattern>%d %p %c{1.} [%t] %m%n</pattern>
      </PatternLayout>
    </FastFile>
  </appenders>
 
  <loggers>
    <logger name="a.namespace.dir" level="info" additivity="false">
      <appender-ref ref="ALog"/>
    </logger>
 
    <root level="info">
      <appender-ref ref="ALog"/>
    </root>
  </loggers>
 
</configuration>

From catalina.out

2013-07-31 11:22:00,313 DEBUG Calling createAppender on class org.apache.logging.log4j.core.appender.FastFileAppender for element FastFile with params(fileName="${catalina.home}/logs/test.log", append="true", name="ALog", immediateFlush="true", suppressExceptions="null", PatternLayout(%d %p %c{1.} [%t] %m%n), null, advertise="null", advertiseURI="null", Configuration(LoggingConfig))

2013-07-31 11:22:00,315 DEBUG Starting FastFileManager ${catalina.home}/logs/test.log

2013-07-31 11:22:00,316 DEBUG Calling createAppenders on class org.apache.logging.log4j.core.config.plugins.AppendersPlugin for element appenders with params(appenders={ALog})

2013-07-31 11:22:00,317 DEBUG Generated plugins in 0.000016000 seconds

2013-07-31 11:22:00,318 DEBUG Calling createAppenderRef on class org.apache.logging.log4j.core.config.AppenderRef for element appender-ref with params(ref="ALog", level="null", null)

2013-07-31 11:22:00,320 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig for element logger with params(additivity="false", level="info", name="a.namespace.dir", includeLocation="null", appender-ref={org.apache.logging.log4j.core.config.AppenderRef@6e8ef177}, properties={}, Configuration(LoggingConfig), null)

2013-07-31 11:22:00,321 DEBUG Calling createAppenderRef on class org.apache.logging.log4j.core.config.AppenderRef for element appender-ref with params(ref="ALog", level="null", null)

2013-07-31 11:22:00,322 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig$RootLogger for element root with params(additivity="null", level="info", includeLocation="null", appender-ref={org.apache.logging.log4j.core.config.AppenderRef@6a8ef455}, properties={}, Configuration(LoggingConfig), null)

2013-07-31 11:22:00,323 DEBUG Calling createLoggers on class org.apache.logging.log4j.core.config.plugins.LoggersPlugin for element loggers with params(loggers={a.namespace.dir, root})

Tomcat Solutions


Solution 1 - Tomcat

I solved my own issue. For system properties you need to prefix variables with sys:.

<FastFile name="ALog" fileName="${sys:catalina.home}/logs/test.log" immediateFlush="true" append="true">
  <PatternLayout>
    <pattern>%d %p %c{1.} [%t] %m%n</pattern>
  </PatternLayout>
</FastFile>

Found it after re-reading this part of the documentation: http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution

Solution 2 - Tomcat

After many hours trying to resolve my own issues with log4j2 file logging with Tomcat I found another possible cause. Hopefully this saves someone else some time down the road!

I had my Tomcat directory located in /Library/Tomcat. At wit's end, I tried reinstalling Tomcat, this time placing the working directory in /usr/local with a symlink to /Library/Tomcat. This, along with Moemars suggestion of including the sys prefix resolved my logging issues.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMoemarsView Question on Stackoverflow
Solution 1 - TomcatMoemarsView Answer on Stackoverflow
Solution 2 - TomcatPat PutnamView Answer on Stackoverflow