or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

Lumberjack

Lumberjack is a Go package that provides a rolling logger implementation for writing logs to files with automatic rotation, compression, and cleanup. It is designed as a pluggable component at the bottom of the logging stack, controlling the files to which logs are written.

Package Information

  • Package Name: lumberjack
  • Package Type: golang
  • Language: Go
  • Module Path: gopkg.in/natefinch/lumberjack.v2
  • Version: 2.2.1
  • Installation: go get gopkg.in/natefinch/lumberjack.v2

Core Imports

import "gopkg.in/natefinch/lumberjack.v2"

Or with an alias:

import lumberjack "gopkg.in/natefinch/lumberjack.v2"

Basic Usage

import (
    "log"
    "gopkg.in/natefinch/lumberjack.v2"
)

func main() {
    log.SetOutput(&lumberjack.Logger{
        Filename:   "/var/log/myapp/foo.log",
        MaxSize:    500, // megabytes
        MaxBackups: 3,
        MaxAge:     28,   // days
        Compress:   true, // disabled by default
    })

    log.Println("This will be written to the rolling log file")
}

Architecture

Lumberjack implements io.WriteCloser and is designed to work with any logging package that can write to an io.Writer, including Go's standard library log package. It handles file rotation automatically based on size thresholds and maintains backup files according to configured retention policies.

Key behaviors:

  • Opens or creates the log file on first write
  • Rotates automatically when the file size exceeds MaxSize
  • Creates backup files with timestamp format: name-2006-01-02T15-04-05.000.ext
  • Optionally compresses rotated files with gzip (adds .gz suffix)
  • Cleans up old files based on MaxBackups (count) and MaxAge (time) policies
  • Thread-safe with internal mutex for concurrent writes

Important limitation: Only one process should write to a given log file. Using the same lumberjack configuration from multiple processes on the same machine will result in improper behavior.

Capabilities

Log Rotation

The Logger type provides automatic log file rotation based on size, with configurable retention policies and optional compression.

type Logger struct {
    // Filename is the file to write logs to. Backup log files will be retained
    // in the same directory. It uses <processname>-lumberjack.log in
    // os.TempDir() if empty.
    Filename string `json:"filename" yaml:"filename"`

    // MaxSize is the maximum size in megabytes of the log file before it gets
    // rotated. It defaults to 100 megabytes.
    MaxSize int `json:"maxsize" yaml:"maxsize"`

    // MaxAge is the maximum number of days to retain old log files based on the
    // timestamp encoded in their filename. Note that a day is defined as 24
    // hours and may not exactly correspond to calendar days due to daylight
    // savings, leap seconds, etc. The default is not to remove old log files
    // based on age.
    MaxAge int `json:"maxage" yaml:"maxage"`

    // MaxBackups is the maximum number of old log files to retain. The default
    // is to retain all old log files (though MaxAge may still cause them to get
    // deleted.)
    MaxBackups int `json:"maxbackups" yaml:"maxbackups"`

    // LocalTime determines if the time used for formatting the timestamps in
    // backup files is the computer's local time. The default is to use UTC
    // time.
    LocalTime bool `json:"localtime" yaml:"localtime"`

    // Compress determines if the rotated log files should be compressed
    // using gzip. The default is not to perform compression.
    Compress bool `json:"compress" yaml:"compress"`
}

Field Defaults:

  • Filename: <processname>-lumberjack.log in os.TempDir()
  • MaxSize: 100 megabytes
  • MaxAge: 0 (no age-based deletion)
  • MaxBackups: 0 (retain all old log files)
  • LocalTime: false (use UTC)
  • Compress: false (no compression)

Rotation Behavior:

The Logger opens or creates the log file on first write. If the file exists and is less than MaxSize megabytes, lumberjack will open and append to that file. If the file exists and its size is >= MaxSize megabytes, the file is renamed by putting the current time in a timestamp immediately before the file's extension (or at the end of the filename if there's no extension). A new log file is then created using the original filename.

Whenever a write would cause the current log file to exceed MaxSize megabytes, the current file is closed, renamed, and a new log file is created with the original name. Thus, the filename you give Logger is always the "current" log file.

Backup File Naming:

Backups use the log file name given to Logger, in the form name-timestamp.ext where:

  • name is the filename without the extension
  • timestamp is the time at which the log was rotated, formatted as 2006-01-02T15-04-05.000
  • ext is the original extension

For example, if your Logger.Filename is /var/log/foo/server.log, a backup created at 6:30pm on Nov 11 2016 would use the filename /var/log/foo/server-2016-11-04T18-30-00.000.log. If compression is enabled, it would be /var/log/foo/server-2016-11-04T18-30-00.000.log.gz.

Cleanup Behavior:

Whenever a new logfile gets created, old log files may be deleted. The most recent files according to the encoded timestamp will be retained, up to a number equal to MaxBackups (or all of them if MaxBackups is 0). Any files with an encoded timestamp older than MaxAge days are deleted, regardless of MaxBackups. Note that the time encoded in the timestamp is the rotation time, which may differ from the last time that file was written to.

If MaxBackups and MaxAge are both 0, no old log files will be deleted.

Writing Log Data

Write implements io.Writer for writing log data to the file with automatic rotation.

func (l *Logger) Write(p []byte) (n int, err error)

Parameters:

  • p []byte: The data to write to the log file

Returns:

  • n int: The number of bytes written
  • err error: An error if the write fails or if the length of the write is greater than MaxSize

Behavior:

If a write would cause the log file to be larger than MaxSize, the file is closed, renamed to include a timestamp of the current time, and a new log file is created using the original log file name. If the length of the write is greater than MaxSize, an error is returned.

The method is thread-safe and can be called concurrently from multiple goroutines.

Closing the Logger

Close implements io.Closer for closing the current log file.

func (l *Logger) Close() error

Returns:

  • error: An error if closing the file fails

Behavior:

Closes the currently open log file. This method is safe to call multiple times. After closing, the logger can be used again - it will open a new file on the next write.

Manual Rotation

Rotate manually triggers log rotation outside of the normal size-based rotation rules.

func (l *Logger) Rotate() error

Returns:

  • error: An error if rotation fails

Behavior:

Causes Logger to close the existing log file and immediately create a new one. This is a helper function for applications that want to initiate rotations outside of the normal rotation rules, such as in response to SIGHUP or other signals. After rotating, this initiates compression and removal of old log files according to the configuration.

Example - Signal-based rotation:

import (
    "log"
    "os"
    "os/signal"
    "syscall"
    "gopkg.in/natefinch/lumberjack.v2"
)

func main() {
    l := &lumberjack.Logger{
        Filename: "/var/log/myapp/app.log",
        MaxSize:  100, // megabytes
    }
    log.SetOutput(l)

    // Setup signal handling for SIGHUP
    c := make(chan os.Signal, 1)
    signal.Notify(c, syscall.SIGHUP)

    go func() {
        for {
            <-c
            l.Rotate()
        }
    }()

    // Your application code here
}

Usage Examples

Basic Logging with Standard Library

The simplest usage is to pass a Logger instance to Go's standard library log package:

import (
    "log"
    "gopkg.in/natefinch/lumberjack.v2"
)

func main() {
    log.SetOutput(&lumberjack.Logger{
        Filename:   "/var/log/myapp/foo.log",
        MaxSize:    500, // megabytes
        MaxBackups: 3,
        MaxAge:     28,   // days
        Compress:   true,
    })

    log.Println("Application started")
    log.Printf("Processing item: %s", "example")
}

Direct Usage as io.Writer

Logger can be used directly as an io.Writer:

import "gopkg.in/natefinch/lumberjack.v2"

func main() {
    logger := &lumberjack.Logger{
        Filename:   "/var/log/app.log",
        MaxSize:    100, // megabytes
        MaxBackups: 5,
        MaxAge:     30, // days
    }
    defer logger.Close()

    logger.Write([]byte("Direct write to log file\n"))
}

Configuration with Environment Variables

import (
    "log"
    "os"
    "strconv"
    "gopkg.in/natefinch/lumberjack.v2"
)

func setupLogger() {
    maxSize, _ := strconv.Atoi(os.Getenv("LOG_MAX_SIZE"))
    if maxSize == 0 {
        maxSize = 100
    }

    maxBackups, _ := strconv.Atoi(os.Getenv("LOG_MAX_BACKUPS"))
    maxAge, _ := strconv.Atoi(os.Getenv("LOG_MAX_AGE"))

    compress := os.Getenv("LOG_COMPRESS") == "true"

    log.SetOutput(&lumberjack.Logger{
        Filename:   os.Getenv("LOG_FILE"),
        MaxSize:    maxSize,
        MaxBackups: maxBackups,
        MaxAge:     maxAge,
        Compress:   compress,
    })
}

Multiple Loggers for Different Files

import (
    "log"
    "gopkg.in/natefinch/lumberjack.v2"
)

func main() {
    // Error logger
    errorLogger := log.New(&lumberjack.Logger{
        Filename:   "/var/log/myapp/error.log",
        MaxSize:    100,
        MaxBackups: 3,
        MaxAge:     7,
        Compress:   true,
    }, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)

    // Info logger
    infoLogger := log.New(&lumberjack.Logger{
        Filename:   "/var/log/myapp/info.log",
        MaxSize:    100,
        MaxBackups: 5,
        MaxAge:     14,
    }, "INFO: ", log.Ldate|log.Ltime)

    errorLogger.Println("This is an error")
    infoLogger.Println("This is info")
}

Rotation Without Size Limit

To rotate only based on manual triggers (e.g., daily rotation via cron or signals), set MaxSize very high:

import "gopkg.in/natefinch/lumberjack.v2"

func main() {
    logger := &lumberjack.Logger{
        Filename:   "/var/log/app.log",
        MaxSize:    10000, // Very high to prevent automatic rotation
        MaxBackups: 30,    // Keep 30 rotations
        LocalTime:  true,  // Use local time in backup filenames
    }

    // Rotate daily via cron or signal handler
    // logger.Rotate()
}

Error Handling

The Write method returns an error if:

  • The length of a single write exceeds MaxSize
  • File operations fail (permissions, disk space, etc.)

Example error handling:

logger := &lumberjack.Logger{Filename: "/var/log/app.log", MaxSize: 10}

n, err := logger.Write([]byte("log message\n"))
if err != nil {
    // Handle error - perhaps fall back to stderr
    fmt.Fprintf(os.Stderr, "Failed to write to log: %v\n", err)
}

Platform-Specific Behavior

Linux

On Linux systems, lumberjack preserves file ownership (UID/GID) when creating new log files after rotation. This ensures that rotated files maintain the same ownership as the original file.

Other Platforms

On non-Linux platforms (Windows, macOS, BSD, etc.), file ownership preservation is not performed. New files will be created with default ownership based on the process's effective user.