如何使用logrotate压缩和清除日志,但不旋转日志


10

我有一个Tomcat服务器,它通过一个阀门(org.apache.catalina.valves.FastCommonAccessLogValve)创建访问日志。该阀门负责旋转访问日志文件,但不压缩它们或在一段时间后将其删除。

目前,我有一个cron作业,用于find [...] -mtime +30 [...]压缩和删除日志。我宁愿使用logrotate,以便将日志轮换放在所有日志的集中位置。我不喜欢为Tomcat提供单独的解决方案。

我尝试阅读logrotate文档,但仍然有些失落。我可以仅使用logrotate压缩和清理日志文件吗?我该怎么做?

还是解决问题,是否有一个Tomcat访问日志阀可以压缩和清理日志文件?

谢谢您帮忙 !


Answers:


17

我见过的非常简单的作品就是这样。

在/etc/logrotate.d中创建一个名为tomcat的文件,其中包含以下内容:

/var/log/tomcat/catalina.out { 
  copytruncate 
  daily 
  rotate 7 
  compress 
  missingok 
}

每天运行一次,压缩文件,并保留7天(旋转7天)。copytruncate意味着它将先复制然后截断原始文件,从而无需重新启动tomcat。如果不存在,则缺少ok不会出错。

通过添加rotatable = false,可以将access.log阀门更改为不旋转:-

<Valve className="org.apache.catalina.valves.AccessLogValve
     ...
     ...
     suffix="log" rotatable="false" />

1
我的问题是,与catalina.out不同,我的访问日志已经轮换了。
纪尧姆

3
好的,可以通过在Valve参数中设置rotatable = false来禁用访问日志旋转。回答包括这一点。
Decado

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 

1

我不想更改Tomcat的配置,因此创建了一个脚本来压缩旋转的文件

#!/ bin / bash
#
#TPP 2013-02-21
#
#Tomcat应用程序使用各种记录器,主要是log4j。
#这些旋转与unix日志旋转系统logrotate冲突。 
#
#一些文件,例如catalina.out
#旋转到包含日期的备份,例如catalina.2013-01-06.log
#然后可以使用bz2压缩到catalina.2013-01-06.log.bz2
#

cd / var / log / tomcat6

#2013-02-21
DATE =`date --rfc-3339 = date`
YEAR =`日期+%Y`

为$中的f(找到catalina * | grep -v bz2 | grep -v'$ DATE'| grep $ YEAR)
做
 回声“ bzip2 $ f” 
 bzip2 $ f
做完了

#但是其他人在包含日期时处于活动状态
#因此,我们将查找所有内容而不压缩最新的
对于'localhost *''opt-db *''opt *''host-manager *''manager *'中的l
做
 导出以前的=
 为$中的f(找到$ l | grep -v bz2 | sort)
 做
  如果[“ $ {previous}”!=“”]
  然后
    回声“ bzip2 $ {上一个}” 
    bzip2 $上一个
  科幻
  导出前一个= $ f
 做完了
做完了

出口0


1

非常简单。只需告诉logrotate具体要旋转的文件即可。 nocreate告诉logrotate在移动旧文件后不要重新创建一个空文件(如果要将文件旋转到子文件夹中)。

/var/log/tomcat/catalina.out.* { 
  daily 
  nocreate
  compress 
  missingok 
}

0

对于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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.