使用Logrotate分隔Tomcat工作日志

??本文不涉及Tomcat訪問日志,僅對Tomcat工作日志進行了分隔,原理相同。Tomcat工作日志配置文件為tomcat目錄下的/conf/logging.properties。
??常見日志分隔方法有1.cronolog,2.log4j, 3.logrotate,4.自寫腳本等,本文僅記錄使用Linux自帶logrotate工具對tomcat日志的分隔。
??Logrotate基于crontab定時執(zhí)行,其配置文件為/etc/logrotate.conf和/etc/logrotate.d/下配置文件組成。其中l(wèi)ogrotate.conf配置文件如下:

# see "man logrotate" for details
# rotate log files weekly   默認每周輪詢執(zhí)行
weekly

# keep 4 weeks worth of backlogs 保留4周備份
rotate 4

# create new (empty) log files after rotating old ones 創(chuàng)建新備份文件
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自動執(zhí)行日志輪轉,想手動強制切割日志,需要加-f參數(shù);不過正式執(zhí)行前最好通過Debug選項來驗證一下(-d參數(shù)),這對調(diào)試也很重要

/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 :壓縮日志后,發(fā)送日志到指定郵箱。
-s, --state=statefile :使用指定的狀態(tài)文件。
-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

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容