I'm looking for a cronolog-like tool that will keep only last n lines or last x minutes of logs piped to it and discard everything else
Is there such a beast?
UPDATE:
I know about logrotate and it renames and zips old logfiles, which is not what I want.
I want to discard old log lines and keep only recent lines.
Like i.e. doing this every so often: tail -10000 logfile > logfile.new mv logfile.new logfile except that with this technique you will most certainly lose log lines and you have to restart or otherwise signal the logging application to reopen the logfile.
Logrotate can be made to only keep one copy of a logfile... If you RTFM you'll find the following bit regarding configuration settings:
You can couple rotate with size, again from the logrotate(8) man page, to keep the file size small. While not by number of lines but by k, M, G size.
You can use
logrotate
and putas part of postrotate command.
logrotate
allows you to specify postrotate commands.You will have to restart or signal the application anyway. The application somehow has to get to know the new offset for seek()ing or hast to reopen the filehandle when you trim the logfile.
Not entirely what you're saying, but you might check out
logrotate(8)
. From the man page:It's installed by default on RHEL and derivitives. I don't know about anything else like Ubuntu/derivitives or Windows.
At the end I've solved it like this (not the most elegant, but it works):
in apache (or whoever logs):
CustomLog "|/usr/local/cronolog/sbin/cronolog /var/tmp/mylog.%Y%m%d.log" logformat
in cron.daily:
find /var/tmp/ -name mylog* -mtime +$days | xargs --no-run-if-empty rm
this will delete old logs
and finally in the analyzing script:
lastdate=$(date -d "$INTERVAL sec ago" +%Y-%m-%dT%H:%M:%S)
grep -h $SEARCH /var/tmp/mylog* | awk -v lastdate="$lastdate" '$1>lastdate { print }' > /tmp/cutlog
and then work with /tmp/cutlog
the above example assumes ISO timestamps in the first field like: 2009-07-20T13:52:32
not the most elegant way but it does what I want. Maybe one day I'll write a feature for cronolog that would do the same thing :)