0
votes

I am working on a grails application which was previously hosted on JBoss, but now we have moved to Apache Tomcat.

The Grails app on JBoss using log4j works perfectly, but we are facing a problem with the org.apache.log4j.DailyRollingFileAppender as we are using log4j in Apache Tomcat for both application logging and server logging.

The issue is that org.apache.log4j.DailyRollingFileAppender overwrites the old backup log files (e.g. we get log of date 9 in log of date 8 and no log of 8 found) and sometimes it starts logging in the previous day's backup file.

I don't know what the problem is with this appender since on JBoss they wrote org.jboss.logging.appender.DailyRollingFileAppender wrapper. Is there any issue with org.apache.log4j.DailyRollingFileAppender? Could you please suggest a solution?

<appender class="org.apache.log4j.DailyRollingFileAppender" name="FILE">
    <errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler"/>
<param name="Threshold" value="INFO"/> 
<param name="File" value="${catalina.base}/logs/applog.log"/>
 <param name="Append" value="true"/>
      <param name="DatePattern" value="'.'yyyy-MM-dd"/>
          <layout class="org.apache.log4j.PatternLayout">
              <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss,SSS Z} level=%-5p class=%c %X{uniqueId} %X{hostname} %X{requestURI} %X{clientIP} %X{userId} %X{realmId} %X{sessionId} %X{locale} %X{callingHost} %X{uniqueIdCallingHost} %X{asyncUserId} %X{isAsync} %X{taskId} %m%n"/>
      </layout>
   </appender>

Since this was not working we use org.apache.log4j.rolling.RollingFileAppender using log4j's "extras" library. Below is the appender configuration but it still does not work properly.

<appender name="ROLL" class="org.apache.log4j.rolling.RollingFileAppender">
    <param name="File" value="${catalina.base}/logs/paymentRolling.log" />
    <param name="Threshold" value="INFO"/> 
    <param name="Append" value="true"/>
   <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
      <param name="FileNamePattern" value="${catalina.base}/logs/appRolling.log.%d{yyyy-MM-dd}"/>
    </rollingPolicy>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss,SSS Z} level=%-5p class=%c %X{uniqueId} %X{hostname} %X{requestURI} %X{clientIP} %X{userId} %X{realmId} %X{sessionId} %X{locale} %X{callingHost} %X{uniqueIdCallingHost} %X{asyncUserId} %X{isAsync} %X{taskId} %m%n"/>
    </layout>
  </appender>
1

1 Answers

0
votes

I'm not sure how the XML configuration plays into everything, but it looks very similar to our (properties-based) configuration which works exactly as we expect it to work:

This is our log4j configuration and it works exactly as we expect it to:
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.file = /path/to/logs/log4j.log
# Roll-over the log once a day
log4j.appender.A1.DatePattern='.'yyyy-MM-dd
log4j.appender.A1.layout = org.apache.log4j.PatternLayout
log4j.appender.A1.layout.conversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.A1.append = true

There are a couple of things that may be interfering with your logging:

  1. If you are using log4j for both server-logging and webapp-logging, you may have both the server and the individual webapps writing to the same files. That is, the two loggers may be stepping on each other's toes. Make sure that each log4j configuration (server, each webapp) is writing to completely separate files.

  2. If the system time (specifically the timezone) differs from that of the Tomcat process and/or the user viewing the log files, you may find that the log file rolls-over at unexpected times. For instance, I live in Washington, DC which is currently UTC-04:00. If the Tomcat user's timezone is set to UTC but my shell account is configured to show me my local time, then the log file will appear to roll-over at 20:00 each day instead of 00:00. In this case, though, the log entries should all be in UTC so the contents of the log files should not be confusing.