我有一个 Tomcat 服务器,它通过阀门创建访问日志(org.apache.catalina.valves.FastCommonAccessLogValve)。该阀门负责轮换访问日志文件,但不会压缩它们或在一段时间后删除它们。
目前,我有一个 cron 作业,用于find [...] -mtime +30 [...]
压缩和删除日志。我宁愿使用 logrotate,这样日志轮换就可以集中在所有日志中。我不喜欢只为 Tomcat 提供单独的解决方案。
我尝试阅读 logrotate 文档,但还是有点困惑。我可以使用 logrotate 来压缩和清理日志文件吗?我该怎么做?
或者反过来想,是否存在一个 Tomcat 访问日志阀可以压缩和清理日志文件?
感谢您的帮助 !
答案1
我见过的相当简单的一个就是这样的。
在 /etc/logrotate.d 中创建一个名为 tomcat 的文件,包含以下内容:-
/var/log/tomcat/catalina.out {
copytruncate
daily
rotate 7
compress
missingok
}
这运行日常的,压缩保存文件,并保留 7 天的数据(旋转 7)。复制截断意味着它将复制然后截断原始文件,这样就不需要重新启动 tomcat。失踪了如果不存在则不会出错。
通过添加 rotatable=false,可以将 access.log 阀门更改为不旋转:-
<Valve className="org.apache.catalina.valves.AccessLogValve
...
...
suffix="log" rotatable="false" />
答案2
修改了 TimP 的脚本——添加了删除非常旧的文件的功能,添加了对旧压缩文件的扫描。
#!/bin/bash
#
# TPP 2013-02-21
# RJK 2014-08-14
#
# Tomcat apps use a variety of loggers, mostly log4j.
# These rotate, conflicting with logrotate, the unix log rotation system.
#
# Some files eg catalina.out
# are rotated to a backup containing a date eg catalina.2013-01-06.log
# which can then be compressed with bz2 to catalina.2013-01-06.log.bz2
# or removed if older than a given number of days(MTIME).
#
cd /var/log/tomcat6
# 2013-02-21
DATE=`date --rfc-3339=date`
YEAR=`date +%Y`
MILLENIUM=20
# 2014-08-14
MTIME=14
# 2014-08-14
#for f in $(find catalina* |grep -v bz2 |grep -v '$DATE' |grep $YEAR)
for f in $(find catalina* |grep -v bz2 |grep -v '$DATE' |grep $MILLENIUM)
do
# 2014-08-14
if test `find $f -mtime +$MTIME`
then
echo "rm -f $f"
rm -f $f
else
echo "bzip2 $f"
bzip2 $f
fi
done
# However others are active whilst containing a date
# so we will find all and not compress the most recent
for l in 'localhost*' 'opt-db*' 'opt*' 'host-manager*' 'manager*'
do
export previous=
for f in $(find $l |grep -v bz2 |sort)
do
if [ "${previous}" != "" ]
then
echo "bzip2 ${previous}"
bzip2 $previous
fi
export previous=$f
done
done
# 2014-08-14
for f in $(find *bz2)
do
if test `find $f -mtime +$MTIME`
then
echo "rm -f $f"
rm -f $f
fi
done
exit 0
答案3
它非常简单。只需告诉 logrotate 您具体想要旋转哪些文件。 nocreate
告诉 logrotate 在移动旧文件后不要重新创建空文件(如果您将文件旋转到子文件夹中)。
/var/log/tomcat/catalina.out.* {
daily
nocreate
compress
missingok
}
答案4
对于 local_access_log.YYYY-MM-DD.txt 压缩,我在看到这篇文章后编写了这个脚本:-
#!/bin/bash
#
# If Tomcat uses server.xml config to rotate localhost_access_log,
# the daily rotated logs will need compressing and old ones deleted to stop filling
# the log partiton. Cannot use the system logrotate command as conficts with tomcat rotate
# therefore run this script in a daily cronjob
#
# localhost_access_log.2015-09-03.txt
#
# Add this script in /etc/cron.daily/ owned by root
#
CATALINA_BASE=`ps aux | grep catalina.base | awk -F'catalina.base\=' '{print $2}' | awk '{print $1}'`
if [ ! $CATALINA_BASE ]
then
if [ -r /var/lib/tomcat8 ]
then
CATALINA_BASE=/var/lib/tomcat8
else
echo "Error: cannot find CATALINA_BASE"
exit 1
fi
fi
cd ${CATALINA_BASE}/logs
if [ $? -ne 0 ]
then
echo "Error, cannot cd to logs directory, quitting...."
exit 1
fi
# today's date (not to be gzipped)
DATE=`date --rfc-3339=date`
# number of days to keep
MTIME=28
# Compress all previous days uncompressed logs
for log in `ls localhost_access_log* | grep -v bz2 | grep -v $DATE`
do
bzip2 $log
done
# delete old logs
find . -name "*.bz2" -mtime +$MTIME -exec rm {} \;
exit 0