Sequencer - pd93/plog GitHub Wiki

When rotating log files, a sequencer function is used to determine the name of the next log file. This is based on the format provided by the user and the name of the previous file. Users can make use of one of PLog's included sequencers, or they can provide their own if they need something more complex.

Included Sequencers

PLog includes several out-the-box sequencers for you to try. To use one of these sequencers, simply provide the function name as an argument to plog.NewRotatingFile(). e.g.

file, err := log.NewRotatingFile("/var/log/myapp/log-file-name-%03d", log.IncrementSequencer)
// Check err

Noop (No Operation) Sequencer

plog.Noop

The noop sequencer will always use the same file name given by the format parameter. This means that when rotating, the file will be overwritten. This is not recommended if you need to keep a history of log files.

Increment Sequencer

plog.IncrementSequencer

The increment sequencer will insert an incrementing number into the file name according to the given format. You should use the %d syntax to dictate where the number should appear in the string. You can also use something like %03d to prefix the number with zeros.

Date/Time Sequencer

plog.DateTimeSequencer

The date/time sequencer will insert the current date and time into the file name according to the given format. You should use the %s syntax to dictate where the date/time should appear in the stirng.

The date/time format for this sequencer roughly follows the time.RFC3339 format. However, in order to stop files from overwriting if they are created in the same second, we have adjusted the format string to include microseconds: 2006-01-02T15:04:05.000Z07:00. This is an important consideration if you plan on creating your own date/time sequencer.

Creating Your Own

To create a custom sequencer, you simply need to provide an anonymous function when setting up your log files. This function must satisfy the plog.Sequencer type:

func(format, prev string) (next string, err error)

e.g.

mySequencer := func(format, prev string) (next string, err error) {

    // Code to determine "next" from "prev"
    // ...

    return fmt.Sprintf(format, next), nil
}

file, err := log.NewRotatingFile("/var/log/myapp/log-file-name-%s", mySequencer)
// Check err

A sequencer function can also be assigned to a file after its creation. e.g.

err := file.SetSequencer(mySequencer)
// Check err