Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a custom logging class that creates a log for each instance of the process and adds a unique id to the log file name, example:

  • process.1234.log
  • process.1235.log

Also I can add a date/time stamp as well, example:

  • process.1234.03012012.log
  • process.1235.03012012.log

Does LogRotate have the ability to use a regex so I could archive my log files by date and/or process id?

share|improve this question

I don't know if later versions do, but after researching further I found that shell wildcards are supported. Both * (match multiple characters) and ? (match a single character) can be combined to match specific files.

As an example, here are patterns which match your use case along with the rest of a file which could be found at /etc/logrotate.d/process

/path/to/my/logfiles/process.????.????????.log
/path/to/my/logfiles/process.????.log
{

    # Look for previously matched log files and rotate daily if found
    daily

    # use date as a suffix of the rotated file
    dateext

    # Compress log file, optional if the files are small enough
    compress

    # Allow for a log file pattern to NOT match in order to support both
    # filename formats
    missingok

    # Do not create replacement log files, the application will do that
    nocreate

    # Keep 30 days worth of rotated logs
    maxage 30
}

However if you intended for process to be a placeholder for an actual process id, then I believe you can get away with using a wildcard in place of the process id number like so:

/path/to/my/logfiles/*.????.????????.log
/path/to/my/logfiles/*.????.log
{
    ...
}

I hope this helps. I too went searching for a regex approach that would work and finally decided on the use of shell wildcards instead of what I felt were workable, but more complex solutions.

An example:

While searching for a regex solution I found a blog post titled, "Excluding files from logrotate globbing matches" which provided this solution:

/var/log/upstart/*.log {
    daily
    missingok
    rotate 7
    compress
    notifempty
    nocreate
    nosharedscripts
    prerotate
        bash -c "[[ ! $1 =~ testprogram ]]"
    endscript
}

I ended up pulling two items out of that example and modified the match slightly. In my situation I wanted to rotate all files in a directory except for some input files which have a .inp extension.

This is what I came up with:

# Force the prerotate "script" below to be run on each individual file
# in order to verify that it isn't an unprocessed input file
nosharedscripts

# Skip rotating any unprocessed input files (*.inp extension)
prerotate
   bash -c "[[ ! $1 =~ \.inp$ ]]"
endscript

According to logrotate -d /etc/logrotate.d/myfilename it appears to work. However as I mentioned I opted for the shell wildcards approach as it seemed easier for someone coming behind me to maintain.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .