How to rotate tomcat logs

If catalina.out becomes 2GB in size, tomcat crashes and fails to start without any error message. To avoid this scenario you should rotate catalina.out frequently. This article describes how to setup auto rotation of catalina.out on a linux/unix machine.

How to automatically rotate catalina.out dail

1. Create this file

1. /etc/logrotate.d/tomcat

 2. Copy the following contents into the above file

/var/log/tomcat/catalina.out { 
copytruncate 
daily 
  dateext 
rotate 7 
compress 
missingok 
}

 

About the above configuration:

  • Make sure that the path /var/log/tomcat/catalina.out above is adjusted to point to your tomcat’s catalina.out
  • daily - rotates the catalina.out daily
  • rotate – keeps at most 7 log files
  • compress – compresses the rotated files
  • dateext : 在归档文件后,将会使用日期进行标注。如果这参数不指定的话,归档文件将会标注为数字。

You don’t need to do anything else.

How it works

  1. Every night the cron daemon runs jobs listed in the /etc/cron.daily/ directory
  2. This triggers the /etc/cron.daily/logrotate file which is generally shipped with linux installations. It runs the command “/usr/sbin/logrotate /etc/logrotate.conf
  3. The /etc/logrotate.conf includes all scripts in the /etc/logrotate.d/ directory.
  4. This triggers the /etc/logrotate.d/tomcat file that you wrote in the previous step.

Run logrotate manually

Run the following command to run the cron job manually

1. /usr/sbin/logrotate /etc/logrotate.conf

More logrotate options

To see all logrotate options on your system, see the manual:

man logrotate

你可以看到在 Tomcat 的日志目录下创建了一个文件:
-rw-rw-r-- 1 tomcat tomcat   133540 Jul 13 09:39 catalina.out-20110713
If no dateext specified, 如下面的内容:
-rw-rw-r-- 1 tomcat tomcat 33790228 Jul 13 09:33 catalina.out.1

你可能感兴趣的:(tomcat)