1

I have my my application running on apache tomcat. I wanted to configure logrotate for the logs apache creates.

My logrotate config is as follows:

/var/company/apache-tomcat/logs/*.log /var/company/apache-tomcat/logs/*.txt {
    su root root
    copytruncate
    compress
    dateext
    dateformat -%s
    extension gz
    missingok
    notifempty
    rotate 90
    daily
}

I just realized that I won't need the copytruncate option for apache logs as apache seems to create a new log file each day with a timestamp appended to it.

eg: localhost.2016-02-26.log

I want logrotate to come in and zip previous logs and delete the them (only keeping the zipped files) and leaving the current log as it is.

Addendum:

Just found out that logrotate is not suitable for rotating logs that have timestamps in them. I am looking for ways in which I can make logrotate work with such logs or other rotating utilities that are more suitable for rotating such logs.

streetsoldier
  • 1,259
  • 1
  • 14
  • 32

1 Answers1

0

I created my own bash script that does this for me. It's running from a cron job and zips up pattern files and deletes files that are older than certain number of days:

#!/bin/bash
# Compress logs and run garbage collection

ME=$(basename $0)

usage() {
  echo "Usage:"
  echo "  $ME <log_path> <garbage_collection_days>"
  echo "Example:"
  echo "  $ME \"/path/to/logfile.log-???????\" 21"
  echo
}

# sanity checking
[[ $# -ne 2 ]] && usage && exit 1

file_pattern="${1}"
gc_days=$2

# compress logfiles that are older than one day
nice -n 10 find $file_pattern -mtime +1 -exec gzip {} \;

# garbage collection
find $(dirname $file_pattern)/*.gz -mtime +$gc_days -exec rm {} \;
streetsoldier
  • 1,259
  • 1
  • 14
  • 32