Skip to content

gounknown/logrotate

Repository files navigation

logrotate

GoDoc Go Report Card Coverage Status License

A powerful log rotation package for Go.

Examples

Use with stdlib log

See demo: Use with stdlib log

func main() {
    // logrotate is safe for concurrent use.
    l, _ := logrotate.New("/path/to/log.%Y%m%d")
    log.SetOutput(l)
    log.Printf("Hello, World!")
}

Use with Zap

See demo: Use with Zap

func main() {
    l, _ := logrotate.New(
        "/path/to/log.%Y%m%d%H",
        logrotate.WithSymlink("/path/to/log"),  // symlink to current logfile
        logrotate.WithMaxAge(30*24*time.Hour),  // remove logs older than 30 days
        logrotate.WithMaxInterval(time.Hour),   // rotate every hour
    )
    w := zapcore.AddSync(l)
    core := zapcore.NewCore(
        zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
        w,
        zap.InfoLevel,
    )
    logger := zap.New(core)
    logger.Info("Hello, World!")
}

Options

Pattern (Required)

See strftime: supported conversion specifications

The pattern used to generate actual log file names. You should use the strftime (3) - format date and time format. For example:

// YYYY-mm-dd (e.g.: 2024-04-04)
logrotate.New("/path/to/log.%Y-%m-%d")
// YY-mm-dd HH:MM:SS (e.g.: 2024-04-04 10:01:49)
logrotate.New("/path/to/log.%Y-%m-%d %H:%M:%S")

Clock (default: logrotate.DefaultClock)

You may specify an object that implements the logrotate.Clock interface. When this option is supplied, it's used to determine the current time to base all of the calculations on. For example, if you want to base your calculations in UTC, you may create a UTC clock:

type UTCClock struct{}

func (UTCClock) Now() time.Time {
    return time.Now().UTC()
}

logrotate.New(
    "/path/to/log.%Y%m%d",
    logrotate.WithClock(UTCClock),
)

Symlink (default: "")

You can set a symlink for the current log file being used. This allows you to always check at the same location for current log file even if the logs were rotated.

logrotate.New(
    "/path/to/log.%Y%m%d",
    logrotate.WithSymlink("/path/to/current"),
)
# Check current log file
$ tail -f /path/to/current

The symlink that share the same parent directory with the main log path will get a special treatment: linked paths will be RELATIVE to the main log file.

Main log file pattern Symlink path Linked path
/path/to/log.%Y%m%d /path/to/log log.YYYYMMDD
/path/to/log.%Y%m%d /path/to/nested/log ../log.YYYYMMDD
/path/to/log.%Y%m%d /foo/bar/baz/log /path/to/log.YYYYMMDD

If not provided, no link will be written.

MaxInterval (default: 24 hours)

Interval between file rotation. By default logs are rotated every 24 hours. In particular, the minimal interval unit is in time.Second level.

Note: Remember to use time.Duration values.

// Rotate every hour
logrotate.New(
    "/path/to/log.%Y%m%d",
    logrotate.WithMaxInterval(time.Hour),
)

MaxSize (default: 100 MiB)

MaxSize is the maximum size in MiB (megabytes) of the log file before it gets rotated. It defaults to 100 MiB.

// Rotate every 10 MiB
logrotate.New(
    "/path/to/log.%Y%m%d",
    logrotate.WithMaxSize(10*1024*1024),
)

MaxAge (default: 0)

Retain old log files based on the timestamp encoded in their filename. If MaxAge <= 0, that means not remove old log files based on age.

Note: Remember to use time.Duration values.

// Remove logs older than 7 days
logrotate.New(
    "/path/to/log.%Y%m%d",
    logrotate.WithMaxAge(7*24*time.Hour),
)

MaxBackups (default: 0)

The maximum number of old log files to retain. If MaxBackups <= 0, that means retain all old log files (though MaxAge may still cause them to be removed.)

// Remove logs except latest 7 files
logrotate.New(
    "/path/to/log.%Y%m%d",
    logrotate.WithMaxBackups(7),
)

BufferedWrite (default: 0)

If you want use buffered write, then sets the channel size to be > 0. If BufferedWrite <= 0, that means do not use buffered write.

// Use buffered write and set channel size to 100
logrotate.New(
    "/path/to/log.%Y%m%d",
    logrotate.WithBufferedWrite(100),
)

Releases

No releases published

Packages

No packages published