Tomcat日志截断
为了避免生产环境Tomcat
的catalina.out
越来越大,我们需要轮替catalina.out
,那么我们该怎么做呢?
关于这个问题,我在Tomcat
的FAQ
1中找到了答案。
How do I rotate catalina.out?
CATALINA_BASE/logs/catalina.out does not rotate. But it should not be an issue because nothing should be printing to standard output since you are using a logging package, right?
If you really must rotate catalina.out, here are some techniques you can use:
- If you are using jsvc 1.0.4 or later (from Apache Commons Daemon project) to launch Tomcat, you can send SIGUSR1 signal to jsvc to get it to re-open its log files (Jira Ticket). You can couple this with ’logrotate’ or your favorite log-rotation utility (including good-old ‘mv’) to re-name catalina.out at intervals and then get jsvc to re-open the original (catalina.out) file and continue writing to it.
- Use ’logrotate’ with the ‘copytruncate’ option. This allows you to externally rotate catalina.out without changing anything within Tomcat.
- Modify bin/catalina.sh (or bin/catalina.bat) to pipe output from the JVM into a piped-logger such as cronolog or Apache httpd’s rotatelogs (note that the previous reference is for Apache httpd documentation and is not applicable to Tomcat – it merely illustrates the concept). See also the patch in Bug 53930, “Allow capture of catalina stdout/stderr to a command instead of just a file”.
大概意思是catalina.out
的轮替不应该是一个问题,因为你用了日志包的话没啥东西会打印到标准输出。但是你真的要轮替catalina.out
有三种方式。
- 使用jsvc 1.0.4 +
- 使用logrotate
- 使用cronolog或者rotatelogs
我选择了第二种,因为第二种最简单,最常用。而且不需要改Tomcat的任何东西。最重要的是,我们的Linux
服务器已经有logrotate(一般都有的)。
那么,我们该怎么做呢?很简单,只要做一件事就行了。
切换到
/etc/logrotate.d
1
cd /etc/logrotate.d
新建文件
1
touch tomcat
编辑新建的文件
1
vim tomcat
然后将下面的内容写到文件里就好了
1 2 3 4 5 6 7 8 9 10
/app/apache-tomcat-8.0.43/logs/catalina.out { copytruncate daily dateext rotate 7 compress missingok size 10M }
/app/apache-tomcat-8.0.43/logs/catalina.out
是你的catalina.out的路径
copytruncate
就是关键了,复制截断daily
表示每天轮替dateext
表示使用日期作为后缀rotate 7
表示轮替最多保留之前的数据几次,超出的将被删除或邮件接收compress
表示轮替下来的日志会被压缩missingok
表示如果日志丢失,不报错继续滚动下一个日志size 10M
表示日志文件超过10M才轮替
做完上面这些我们就等着定时执行就好了,我们也可以手动强制执行轮替来试试。
|
|