How to compress and clean logs with logrotate but not rotate them
Fairly simple one that i have seen works like this.
Create a file in /etc/logrotate.d called tomcat containing the following:-
/var/log/tomcat/catalina.out {
copytruncate
daily
rotate 7
compress
missingok
}
This runs daily, compresses the file, and keeps 7 days worth (rotate 7). copytruncate means that it'll copy then truncate the original file so that there is no need to restart tomcat. missing ok will not error if it's not there.
The access.log Valve can be changed to not rotate by adding rotatable=false:-
<Valve className="org.apache.catalina.valves.AccessLogValve
...
...
suffix="log" rotatable="false" />
Modified TimP's script--Added deleting very old files, added scan for old compressed files.
#!/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
It's surprisingly simple. Just tell logrotate which files you specifically want to rotate. nocreate
tells logrotate not to recreate an empty file after moving the old one (if you're rotating files into a subfolder).
/var/log/tomcat/catalina.out.* {
daily
nocreate
compress
missingok
}