Log4j daily rolling catalina.out without restartin

2019-05-18 22:38发布

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

2条回答
smile是对你的礼貌
2楼-- · 2019-05-18 22:49

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.

查看更多
对你真心纯属浪费
3楼-- · 2019-05-18 22:51

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

Yes, if you're willing to work for it.

Your log4j configuration will only end up fighting with the standard shell redirection that bin/catalina.sh uses to redirect stdout to logs/catalina.out. You can't simply use log4j configuration to change how System.out behaves.

If you want to rotate conf/catalina.out you will have to take some alternative measures depending on how to launch Tomcat:

  • If you use jsvc to launch Tomcat and you are using commons-daemon 1.0.4 or later, then you can send SIGUSR1 to the jsvc process to re-open the log files. That will allow you to move the existing log file to another file (which just changes its name and continues to log to the new filename) and then do 'kill SIGUSR1': the original filename will then be re-opened and new logs messages will go to it.
  • If you use bin/catalina.sh to launch Tomcat, you can modify it so that it no longer does redirection and instead pipes output to a rolling-logger process like Apache httpd's rotatelogs or chronolog.
查看更多
登录 后发表回答