rolling

package
v1.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2026 License: MIT Imports: 9 Imported by: 0

README

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrWriteTooLong = errors.New("write exceeds max file length")

ErrWriteTooLong indicates that a single write that is longer than the max size allowed in a single file.

Functions

This section is empty.

Types

type Option

type Option func(roller *Roller)

func WithMaxAge

func WithMaxAge(age int) Option

func WithMaxBackups

func WithMaxBackups(backups int) Option

type Roller

type Roller struct {
	// contains filtered or unexported fields
}

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

Roller 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 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 Roller is always the "current" log file.

Backups use the log file name given to Roller, 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 Roller.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.log.2016-11-04T18-30-00.000`

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 NewRoller

func NewRoller(filename string, maxSize int64, opt ...Option) (*Roller, error)

func (*Roller) Close

func (r *Roller) Close() error

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

func (*Roller) Rotate

func (r *Roller) Rotate() error

Rotate causes Roller 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 compression and removal of old log files according to the configuration.

func (*Roller) Write

func (r *Roller) 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL