logrotate

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2025 License: Apache-2.0 Imports: 13 Imported by: 4

README

go-logrotate

go-logrotate is a Go package for writing logs to rolling files.

go-logrotate is based on lumberjack.

to import go-logrotate :

import "github.com/fahedouch/go-logrotate"

go-logrotate add new features to Lumberjack:

  • Supporting MaxBytes to specify the log size in bytes.
  • Supporting unlimited MaxBytes with -1.
  • Supporting multiple backups file names :
    • standard file name : foo.log.1
    • time file name: foo-2014-05-04T14-44-33.555.log

Example

To use go-logrotate with the standard library's log package and with file name standard format, just pass it into the SetOutput function when your application starts.

Code:

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

To use go-logrotate with file name time format. File name time format takes precedence over the standard format.

log.SetOutput(&logrotate.Logger{
    Filename:   "/var/log/myapp/foo.log",
    FilenameTimeFormat: "2006-01-02T15-04-05.000",
    MaxBytes:    500, // bytes
    MaxBackups: 3,
    MaxAge:     28, //days
    Compress:   true, // disabled by default
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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>.log in
	// os.TempDir() if empty.
	Filename string `json:"filename" yaml:"filename"`

	// FilenameTimeFormat determines whether the rotated log file name contains
	// timestamp or not and defines its format. It doesn't contain timestamp if empty.
	// (e.g `2006-01-02T15-04-05.000`)
	FilenameTimeFormat string `json:"filenameTimeFormat" yaml:"filenameTimeFormat"`

	// FileOrder is the starting order of old log file
	FileOrder int `json:"fileOrder" yaml:"fileOrder"`

	// MaxBytes is the maximum size in bytes of the log file before it gets
	// rotated. It defaults to 104857600 (100 megabytes).
	MaxBytes int64 `json:"maxbytes" yaml:"maxbytes"`

	// 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 MaxBytes, logrotate will open and append to that file. If the file exists and its size is >= MaxBytes, the file is renamed by putting an incremental number after the file's extension or 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 MaxBytes, 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 uses the log file name given to Logger, in the form `name.ext.num` or `name-timestamp.ext` where name is the filename without the extension, timestamp is the birth 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. if Logger.FilenameTimeFormat is not empty the backup name format is `name-timestamp.ext` if Logger.FilenameTimeFormat is empty the backup name format is `name.ext.num` For example, if your Logger.Filename is `/var/log/foo/server.log` and Logger.FilenameTimeFormat is not empty, 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 their birth time will be retained, up to a number equal to MaxBackups (or all of them if MaxBackups is 0). Any files birth time older than MaxAge days are deleted, regardless of MaxBackups.Note that the file's birth time 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 compression and removal of old log files according to the configuration.

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 MaxBytes, the file is closed, renamed and a new log file is created using the original log file name. If the length of the write is greater than MaxBytes, an error is returned.

Jump to

Keyboard shortcuts

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