代码改变世界

设置log rotation避免tomcat catalina.out文件增长过大

  abce  阅读(2495)  评论(0编辑  收藏  举报

创建logrotate配置文件

1
$ vi /etc/logrotate.d/tomcat

 

添加以下内容:

1
2
3
4
5
6
7
8
9
/opt/tomcat/logs/catalina.out { 
  copytruncate 
  daily 
  rotate 7
  compress 
  missingok 
  dateext
  size 100m 
}

 

参数说明:

1
2
3
4
5
6
7
8
specify the path to the log file.
copytruncate – creates a copy of the log file and then truncates the original to an empty file so that the service can keep on logging uninterrupted.
daily – rotates the catalina.out daily.
rotate – keeps at most 7 log files.
compress – create a gzip compressed file of the rotated files.
missingok – suppresses error messages if the file does not exist.
dateext – add the date to the filename of the archived log file.
size – rotates if the size of catalina.out is bigger than 100m. 

  

在大多数linux系统上都可以找到文件:/etc/cron.daily/logrotate,该文件每天都会被运行。运行时会调用:

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

这样就会rotate掉tomcat的日志。
如果有多个tomcat实例,就需要配置多个单独的rotation文件。

 

也可以手动编写定时任务:

1
2 * * * * /usr/sbin/logrotate /etc/logrotate.d/tomcat

  

也可以顺便将tomcat的临时文件删除掉:

1
2
3
4
5
6
7
8
9
10
11
12
/opt/tomcat/logs/catalina.out { 
  notifempty
  copytruncate
  dateext
  daily
  rotate 10
  compress
  missingok
  postrotate
    /bin/nice /usr/bin/find /opt/tomcat/temp -type f -mtime +10 -exec /bin/rm {} \; > /dev/null
  endscript
}

  

 

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2016-01-12 python笔记-print输出
2016-01-12 python笔记-列表和元组
2016-01-12 python笔记-字符串
点击右上角即可分享
微信分享提示