Convert Figma logo to code with AI

natefinch logolumberjack

lumberjack is a log rolling package for Go

4,789
590
4,789
92

Top Related Projects

21,647

Blazing fast, structured, leveled logging in Go.

24,520

Structured, pluggable logging for Go.

10,371

Zero Allocation JSON Logger

1,103

Structured, composable logging for Go

Quick Overview

Lumberjack is a Go package that provides a rolling logger with automatic log file rotation. It's designed to be a simple, reliable solution for applications that need to log to files without worrying about disk space management or log file proliferation.

Pros

  • Simple to use and integrate into existing Go projects
  • Automatic log file rotation based on size or time
  • Supports concurrent writes from multiple goroutines
  • Minimal dependencies, relying only on the Go standard library

Cons

  • Limited configuration options compared to more complex logging frameworks
  • No built-in log level management
  • Lacks advanced features like log compression or remote logging
  • Not actively maintained (last commit was in 2019 as of this writing)

Code Examples

  1. Basic usage:
log := &lumberjack.Logger{
    Filename:   "/var/log/myapp/foo.log",
    MaxSize:    500, // megabytes
    MaxBackups: 3,
    MaxAge:     28, // days
    Compress:   true, // disabled by default
}
log.Write([]byte("This is a test log entry\n"))
  1. Using with the standard log package:
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 is a test log entry")
  1. Using with a more advanced logging library (e.g., zerolog):
logger := zerolog.New(&lumberjack.Logger{
    Filename:   "/var/log/myapp/foo.log",
    MaxSize:    500, // megabytes
    MaxBackups: 3,
    MaxAge:     28, // days
    Compress:   true, // disabled by default
}).With().Timestamp().Logger()

logger.Info().Str("foo", "bar").Msg("Hello World")

Getting Started

To use Lumberjack in your Go project, first install it:

go get -u github.com/natefinch/lumberjack

Then, import and use it in your code:

import (
    "github.com/natefinch/lumberjack"
    "log"
)

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

    log.Println("This is a test log entry")
}

This setup will create a rolling logger that writes to "/var/log/myapp/foo.log", rotating the file when it reaches 500MB, keeping up to 3 old log files, and deleting files older than 28 days.

Competitor Comparisons

21,647

Blazing fast, structured, leveled logging in Go.

Pros of zap

  • Extremely high performance with minimal allocations
  • Structured logging with type-safe field additions
  • Flexible configuration options and log levels

Cons of zap

  • Steeper learning curve due to more complex API
  • Requires more setup code compared to simpler loggers

Code Comparison

zap:

logger, _ := zap.NewProduction()
defer logger.Sync()
logger.Info("Failed to fetch URL",
    zap.String("url", url),
    zap.Int("attempt", attempt),
    zap.Duration("backoff", backoff),
)

lumberjack:

log.SetOutput(&lumberjack.Logger{
    Filename:   "/var/log/myapp/foo.log",
    MaxSize:    500, // megabytes
    MaxBackups: 3,
    MaxAge:     28, // days
})
log.Print("Failed to fetch URL")

Key Differences

  • zap focuses on high-performance structured logging, while lumberjack specializes in log file rotation and management
  • zap provides more granular control over log fields and levels, whereas lumberjack is simpler to set up for basic logging needs
  • zap is better suited for applications requiring detailed, structured logs, while lumberjack is ideal for managing log file sizes and retention

Both libraries serve different purposes and can be used together in a logging setup, with zap handling the logging itself and lumberjack managing the log files.

24,520

Structured, pluggable logging for Go.

Pros of Logrus

  • More feature-rich logging framework with structured logging support
  • Offers hooks for easy integration with various output formats and services
  • Provides customizable log levels and fields for detailed logging

Cons of Logrus

  • Heavier and more complex compared to Lumberjack's simplicity
  • May have a steeper learning curve for basic logging needs
  • Not focused on log rotation, which is Lumberjack's primary feature

Code Comparison

Logrus:

log := logrus.New()
log.WithFields(logrus.Fields{
    "animal": "walrus",
}).Info("A walrus appears")

Lumberjack:

log := &lumberjack.Logger{
    Filename:   "/var/log/myapp/foo.log",
    MaxSize:    500, // megabytes
    MaxBackups: 3,
    MaxAge:     28, // days
}

Summary

Logrus is a comprehensive logging framework with structured logging and extensive features, while Lumberjack focuses specifically on log rotation and management. Logrus is better suited for applications requiring detailed, structured logging with various output options. Lumberjack is ideal for simpler logging needs with a focus on file management and rotation.

10,371

Zero Allocation JSON Logger

Pros of zerolog

  • Faster performance due to zero-allocation design
  • Structured logging with JSON output
  • More flexible and customizable logging options

Cons of zerolog

  • Steeper learning curve for developers new to structured logging
  • Requires more setup and configuration compared to simpler loggers

Code Comparison

zerolog:

logger := zerolog.New(os.Stdout).With().Timestamp().Logger()
logger.Info().Str("key", "value").Msg("Hello, World!")

lumberjack:

log.SetOutput(&lumberjack.Logger{
    Filename:   "/var/log/myapp/foo.log",
    MaxSize:    500, // megabytes
    MaxBackups: 3,
    MaxAge:     28, // days
})
log.Print("Hello, World!")

Key Differences

  • zerolog focuses on structured logging and performance optimization
  • lumberjack specializes in log file rotation and management
  • zerolog provides more control over log formatting and fields
  • lumberjack is simpler to set up for basic logging needs

Use Cases

  • Choose zerolog for high-performance, structured logging in larger applications
  • Opt for lumberjack when log file rotation and management are primary concerns
  • Consider using both together for comprehensive logging solutions
1,103

Structured, composable logging for Go

Pros of log15

  • Offers structured logging with key-value pairs, allowing for more detailed and searchable logs
  • Provides a flexible and extensible architecture with support for custom handlers and formatters
  • Includes built-in support for logging contexts, making it easier to track related log entries

Cons of log15

  • May have a steeper learning curve due to its more complex API and features
  • Potentially higher memory usage and performance overhead compared to simpler logging solutions

Code Comparison

log15:

log := log15.New("module", "app/server")
log.Info("server started", "port", 8080)

lumberjack:

log.SetOutput(&lumberjack.Logger{
    Filename:   "/var/log/myapp.log",
    MaxSize:    100, // megabytes
    MaxBackups: 3,
    MaxAge:     28, // days
})

Summary

log15 offers more advanced features like structured logging and extensibility, making it suitable for complex applications with specific logging requirements. lumberjack, on the other hand, focuses on log rotation and management, providing a simpler solution for applications that primarily need to handle log file maintenance. The choice between the two depends on the specific logging needs and complexity of the project.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

lumberjack GoDoc Build Status Build status Coverage Status

Lumberjack is a Go package for writing logs to rolling files.

Package lumberjack provides a rolling logger.

Note that this is v2.0 of lumberjack, and should be imported using gopkg.in thusly:

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

The package name remains simply lumberjack, and the code resides at https://github.com/natefinch/lumberjack under the v2.0 branch.

Lumberjack is intended to be one part of a logging infrastructure. It is not an all-in-one solution, but instead is a pluggable component at the bottom of the logging stack that simply controls the files to which logs are written.

Lumberjack plays well with any logging package that can write to an io.Writer, including the standard library's log package.

Lumberjack assumes that only one process is writing to the output files. Using the same lumberjack configuration from multiple processes on the same machine will result in improper behavior.

Example

To use lumberjack with the standard library's log package, just pass it into the SetOutput function when your application starts.

Code:

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

type Logger

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"`
    // contains filtered or unexported fields
}

Logger is an io.WriteCloser that writes to the specified filename.

Logger opens or creates the logfile 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 in the name immediately before the file's extension (or the end of the filename if there's no extension). A new log file is then created using original filename.

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

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 with the time.Time format of 2006-01-02T15-04-05.000 and the extension 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

Cleaning Up Old Log Files

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.

func (*Logger) Close

func (l *Logger) Close() error

Close implements io.Closer, and closes the current logfile.

func (*Logger) Rotate

func (l *Logger) Rotate() error

Rotate 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. After rotating, this initiates a cleanup of old log files according to the normal rules.

Example

Example of how to rotate in response to SIGHUP.

Code:

l := &lumberjack.Logger{}
log.SetOutput(l)
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP)

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

func (*Logger) Write

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

Write implements io.Writer. 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.


Generated by godoc2md