Search code examples
javalog4jrotationtomcat7catalina

Log4j daily rolling catalina.out without restarting Tomcat?


i am having trouble with configuring Log4j correctly. I was expecting Log4j to rotate my catalina.out file at midnight when configuring it like the following..


log4j.properties:

log4j.rootLogger=INFO, CATALINA

# Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.File=/var/log/tomcat7/catalina.out
log4j.appender.CATALINA.Append=true
log4j.appender.CATALINA.Encoding=UTF-8

# Roll-over the log once per day
log4j.appender.CATALINA.DatePattern='.'yyyy-MM-dd-HH-mm'.log'
log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.ConversionPattern =%d{HH:mm:ss} %5p [%t] - %m%n


After configuring I restarted Tomcat and everything is written to:

/var/log/tomcat7/catalina.out


To test my configuration i changed the current date time to like 23:59:59:

#ls -l /var/log/tomcat7/
-rw-r--r-- 1 tomcat7 tomcat7 5840  4. May 00:00 catalina.out


As you can see, it didnt rotate at midnight... (?)

When restarting Tomcat it works perfectly fine:

#ls -l /var/log/tomcat7/
-rw-r--r-- 1 tomcat7 tomcat7 5840  4. May 13:37 catalina.out
-rw-r--r-- 1 tomcat7 root    2395  4. May 00:00 catalina.out.*CURRENTDATE*.log

Is it even possible to rotate my logfiles without restarting Tomcat?

Thanks in advance, Marley


Solution

  • There are three solutions for this problem:

    1. change default tomcat logging façade that writes to catalina.out to for example: slf4j, with all the benefits that comes with using it and log4j.
    2. configure system cron to run logrotate of tomcat log files
    3. change default logging class from ConsoleAppender to FileAppender.

    Benefits of solutions:

    1. very flexible as the slf4j offers many options especially with log4j, that you use anyway.
    2. simple and doesn't require touching tomcat configuration.
    3. simple change of configuration that disables console output

    Disadvantages:

    1. require additional libraries, affects all applications that tomcat is hosting, requires replacing default configuration with log4j.
    2. cron+logrotate works only in linux; it might be not as simple in windows with scheduler. Requires extra scripting in windows environment.
    3. provides only simple backup with date. Date pattern cannot be set. Does not compress rotated files.

      Solution for First issue, is described here
      Solution for Second issue is described here
      Solution for Third issue is described here

    You can as well combine solutions. For example use crontab to gzip files that where created by changing catalina.out to other name. I would also suggest to leave tomcat so it logs to catalina.out, and configure your application to different file with log4j. This way logs from tomcat that are not immaterial won't spam your logs.