For quite some time I have wanted logrotate to offer something
other than the existing filenaming scheme of renaming logs to:

log.1
log.2

etc...

I am interested in having rotated logs named instead with the
date and time in the filename such as:

messages
messages-2000-05-01-0401.gz
messages-2000-06-01-0401.gz

It was a feature request of my current employer at the time, and
I liked the idea very much.  I tried to implement it via a
postrotate script at the time, but they wanted it done on the
production server against all of my opposition.  You NEVER do
development on live data, but I was FORCED to, and sure enough,
the log files got destroyed, and I was to blame...  <sigh>

Well, that ended up relieving me of the duty of solving the
problem despite the fact I likely WOULD HAVE if they'd let me do
it MY WAY - the PROPER way - on a devel system.  Heck, a few
hours hacking is all it should have took.  I asked Erik Troan
(the author) if he could implement the feature, and he said if I
implemented it he'd consider adding it (or something like that).

Well, since I've been playing with logfile processing a lot
lately, the idea came back to me, and I decided to give it a
whirl.  I got the logrotate source and looked at it.  It seems
the concept of rotated logs being named ".x" where x is a number
is quite deeply intertwined into logrotate, so I figured I'd try
it again using postrotate instead.

After numerous frustrating attempts, I decided to look into the
logrotate source code to see how postrotate was
implemented.  Basically, it takes all your script code, puts it
in a temporary file and executes it with "#!/bin/sh".  THAT WAS
IT!!!!!  I was using command substitution with $() syntax and
"sh" doesn't grok that!  I changed it around a bit, and
determined that logrotate compresses the log files AFTER
postrotate is executed.  That meant it wouldn't work because I
was renaming the rotated log...  I then realized I could compress
the log myself, and disable logrotate from doing it.

As a result, I now have a cool logrotate config that can do
exactly what I want, which is rotating the logs with the DATE
appended to the filename instead of the .number scheme.

Erik, I told you if I come up with a solution I'd send you a
copy, and here it is.  

/var/log/messages {
    nocompress
    postrotate
        DATE_CODE=`date +"%Y-%m-%d-%H%M"`
        LOG_FILE=messages
        mv ${LOGFILE}.1 ${LOG_FILE}-$DATE_CODE
        gzip -9 ${LOG_FILE}-$DATE_CODE
    endscript
}

It isn't C source to add to the package, but it is functional
config file syntax nonetheless.  I'll bet that many out there are
looking for similar functionality but are unable to figure it out
easily enough.  You're welcome to include my above example in any
code or documentation for logrotate, HOWTO's, etc...

In order to use it with different logs, modify the logfilename,
and the LOG_FILE variable in the script section.

Be forewarned though that I don't know how his works with ALL of
logrotate's different features, so use it with CAUTION.  If you
use it, test it on DUMMY data, or copies of data, and not live
log files!!!  It works for me (tm), but it may explode and
destroy the universe and all of your data!  In particular, I'm
curious as to wether or not it will work with the logrotate
"olddir" directive or not..  Modification might be necessary.

Anyways, I hope someone out there has a use for this.  It makes
things much easier for me!

Take care,
TTYL

-- 
Mike A. Harris                                     Linux advocate     
Computer Consultant                                  GNU advocate  
Capslock Consulting                          Open Source advocate

       Try out Red Hat Linux today:  http://www.redhat.com
           ftp://ftp.redhat.com/pub/redhat/redhat-6.2/




_______________________________________________
Redhat-devel-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-devel-list

Reply via email to