本文不涉及Tomcat访问日志,仅对Tomcat工作日志进行了分隔,原理相同。Tomcat工作日志配置文件为tomcat目录下的/conf/logging.properties。
常见日志分隔方法有1.cronolog,2.log4j, 3.logrotate,4.自写脚本等,本文仅记录使用Linux自带logrotate工具对tomcat日志的分隔。
Logrotate基于crontab定时执行,其配置文件为/etc/logrotate.conf和/etc/logrotate.d/下配置文件组成。其中logrotate.conf配置文件如下:
# see "man logrotate" for details
# rotate log files weekly 默认每周轮询执行
weekly
# keep 4 weeks worth of backlogs 保留4周备份
rotate 4
# create new (empty) log files after rotating old ones 创建新备份文件
create
# use date as a suffix of the rotated file
dateext
# uncomment this if you want your log files compressed 是否压缩选项
#compress
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d
# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
monthly
create 0664 root utmp
minsize 1M
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}
# system-specific logs may be also be configured here.
~
如果等不及cron自动执行日志轮转,想手动强制切割日志,需要加-f参数;不过正式执行前最好通过Debug选项来验证一下(-d参数),这对调试也很重要
/usr/sbin/logrotate -f /etc/logrotate.d/nginx
/usr/sbin/logrotate -d -f /etc/logrotate.d/nginx
logrotate命令格式:
logrotate [OPTION...] <configfile>
-d, --debug :debug模式,测试配置文件是否有错误。
-f, --force :强制转储文件。
-m, --mail=command :压缩日志后,发送日志到指定邮箱。
-s, --state=statefile :使用指定的状态文件。
-v, --verbose :显示转储过程。
本次尝试编写Tomcat日志分隔脚本如下:
[root@huanqiu-backup ~]# cat /etc/logrotate.d/tomcat
/Data/app/tomcat-7-huanqiu/logs/catalina.out {
rotate 14
daily
copytruncate
compress
notifempty
missingok
}
[root@huanqiu-backup ~]# ll /Data/app/tomcat-7-huanqiu/logs/catalina.*
-rw-r--r--. 1 root root 0 Jan 19 19:11 /Data/app/tomcat-7-huanqiu/logs/catalina.out
-rw-r--r--. 1 root root 95668 Jan 19 19:11 /Data/app/tomcat-7-huanqiu/logs/catalina.out.1.gz
参考文献:
1.https://www.cnblogs.com/kevingrace/p/6307298.html
2.http://www.cnblogs.com/operationhome/p/9680040.html