How to rotate tomcat logs

本文介绍如何在Linux/Unix环境下通过配置实现Tomcat的catalina.out日志文件自动每日轮转,避免因日志文件过大导致Tomcat崩溃的问题。通过设置日志轮转,可以确保系统的稳定运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值