5

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?

1 Answer 1

5

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.

3
  • Is the bash -c required? I think under Linux at least, the /bin/sh would support that syntax directly.
    – Peter
    Commented May 23, 2018 at 5:08
  • @Peter Not sure. I ended up going a different direction, so I don't believe I tested any further than what I noted. If you test and confirm I'll be happy to update my answer to emphasize what is/isn't necessary.
    – deoren
    Commented May 23, 2018 at 15:27
  • @Peter bash -c is not required unless you really want the script to run with bash instead of sh. According to the man page, your script executes with sh
    – Jellicle
    Commented Aug 3, 2020 at 14:50

You must log in to answer this question.

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