Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a Tomcat server which create access logs via a valve (org.apache.catalina.valves.FastCommonAccessLogValve). This valve takes care of rotating the access log file, but not of compressing them or deleting them after some time.

For the moment, I have a cron job that uses find [...] -mtime +30 [...] to compress and delete the logs. I would rather use logrotate, so that log rotation is in a centralized place for all logs. I dont like having a seperate solution just for Tomcat.

I tried to read logrotate documentation, but I am still a bit lost. Can I use logrotate just to compress and cleanup log files ? How would I do that ?

Or turning the problem around, is there a Tomcat access log valve that will compress and cleanup log files ?

Thank for your help !

share|improve this question
    
BTW: FastCommonAccessLogValve is deprecated in 6.0 tomcat.apache.org/tomcat-6.0-doc/api/org/apache/catalina/valves/… –  Janning Mar 22 '11 at 11:00

2 Answers 2

up vote 6 down vote accepted

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 then get's run daily and will compress the file and keep 7 days worth (rotate 7). copytruncate means it'll copy then truncate the origincal file, so 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
     ...
     ...
     rotatable=false/>
share|improve this answer
    
My problem is that unlike catalina.out, my access log is already rotated. –  Guillaume Mar 15 '11 at 15:39
    
Okay, access log rotation can be disabled by setting rotatable=false in the Valve arguments. Answeraltered to include this. –  Decado Mar 16 '11 at 9:45

I did not wish to change the Tomcat configuration, so created a script which compressed the rotated files

#!/bin/bash
#
# TPP 2013-02-21
#
# 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
#

cd /var/log/tomcat6

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

for f in $(find catalina* |grep -v bz2 |grep -v '$DATE' |grep $YEAR)
do
 echo "bzip2 $f" 
 bzip2 $f
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

exit 0

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.