Log Rotation Tool – Keeping Only a Specified Amount of Logs

apache-2.2log-filesloggingsyslog

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.

Best Answer

Logrotate can be made to only keep one copy of a logfile... If you RTFM you'll find the following bit regarding configuration settings:

rotate count
    Log files are rotated count times before being removed or mailed to the 
    address specified in a mail directive. If count is  0,  old  versions  
    are  removed  rather than rotated.

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.

size size
    Log  files  are rotated when they grow bigger than size bytes. If size is 
    followed by M, the size if assumed to be in megabytes.  If the G suffix is 
    used, the size  is  in gigabytes.   If  the k is used, the size is in 
    kilobytes. So size 100, size 100k, and size 100M are all valid.