file-rotatelogs: github.com/lestrrat-go/file-rotatelogs Index | Examples | Files | Directories

package rotatelogs

import "github.com/lestrrat-go/file-rotatelogs"

package rotatelogs is a port of File-RotateLogs from Perl (https://metacpan.org/release/File-RotateLogs), and it allows you to automatically rotate output files when you write to them according to the filename pattern that you can specify.

Index

Examples

Package Files

event.go interface.go options.go rotatelogs.go

Variables

var Local = clockFn(time.Now)

Local is an object satisfying the Clock interface, which returns the current time in the local timezone

var UTC = clockFn(func() time.Time { return time.Now().UTC() })

UTC is an object satisfying the Clock interface, which returns the current time in UTC

type Clock Uses

type Clock interface {
    Now() time.Time
}

Clock is the interface used by the RotateLogs object to determine the current time

type Event Uses

type Event interface {
    Type() EventType
}

type EventType Uses

type EventType int
const (
    InvalidEventType EventType = iota
    FileRotatedEventType
)

type FileRotatedEvent Uses

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

func (*FileRotatedEvent) CurrentFile Uses

func (e *FileRotatedEvent) CurrentFile() string

func (*FileRotatedEvent) PreviousFile Uses

func (e *FileRotatedEvent) PreviousFile() string

func (*FileRotatedEvent) Type Uses

func (e *FileRotatedEvent) Type() EventType

type Handler Uses

type Handler interface {
    Handle(Event)
}

type HandlerFunc Uses

type HandlerFunc func(Event)

func (HandlerFunc) Handle Uses

func (h HandlerFunc) Handle(e Event)

type Option Uses

type Option interface {
    Name() string
    Value() interface{}
}

Option is used to pass optional arguments to the RotateLogs constructor

func ForceNewFile Uses

func ForceNewFile() Option

ForceNewFile ensures a new file is created every time New() is called. If the base file name already exists, an implicit rotation is performed

Code:

logDir, err := ioutil.TempDir("", "rotatelogs_test")
if err != nil {
    fmt.Println("could not create log directory ", err)

    return
}
logPath := fmt.Sprintf("%s/test.log", logDir)

for i := 0; i < 2; i++ {
    writer, err := rotatelogs.New(logPath,
        rotatelogs.ForceNewFile(),
    )
    if err != nil {
        fmt.Println("Could not open log file ", err)

        return
    }

    n, err := writer.Write([]byte("test"))
    if err != nil || n != 4 {
        fmt.Println("Write failed ", err, " number written ", n)

        return
    }
    err = writer.Close()
    if err != nil {
        fmt.Println("Close failed ", err)

        return
    }
}

files, err := ioutil.ReadDir(logDir)
if err != nil {
    fmt.Println("ReadDir failed ", err)

    return
}
for _, file := range files {
    fmt.Println(file.Name(), file.Size())
}

err = os.RemoveAll(logDir)
if err != nil {
    fmt.Println("RemoveAll failed ", err)

    return
}

Output:

test.log 4
test.log.1 4

func WithClock Uses

func WithClock(c Clock) Option

WithClock creates a new Option that sets a clock that the RotateLogs object will use to determine the current time.

By default rotatelogs.Local, which returns the current time in the local time zone, is used. If you would rather use UTC, use rotatelogs.UTC as the argument to this option, and pass it to the constructor.

func WithHandler Uses

func WithHandler(h Handler) Option

WithHandler creates a new Option that specifies the Handler object that gets invoked when an event occurs. Currently `FileRotated` event is supported

func WithLinkName Uses

func WithLinkName(s string) Option

WithLinkName creates a new Option that sets the symbolic link name that gets linked to the current file name being used.

func WithLocation Uses

func WithLocation(loc *time.Location) Option

WithLocation creates a new Option that sets up a "Clock" interface that the RotateLogs object will use to determine the current time.

This optin works by always returning the in the given location.

func WithMaxAge Uses

func WithMaxAge(d time.Duration) Option

WithMaxAge creates a new Option that sets the max age of a log file before it gets purged from the file system.

func WithRotationCount Uses

func WithRotationCount(n uint) Option

WithRotationCount creates a new Option that sets the number of files should be kept before it gets purged from the file system.

func WithRotationSize Uses

func WithRotationSize(s int64) Option

WithRotationSize creates a new Option that sets the log file size between rotation.

func WithRotationTime Uses

func WithRotationTime(d time.Duration) Option

WithRotationTime creates a new Option that sets the time between rotation.

type RotateLogs Uses

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

RotateLogs represents a log file that gets automatically rotated as you write to it.

func New Uses

func New(p string, options ...Option) (*RotateLogs, error)

New creates a new RotateLogs object. A log filename pattern must be passed. Optional `Option` parameters may be passed

func (*RotateLogs) Close Uses

func (rl *RotateLogs) Close() error

Close satisfies the io.Closer interface. You must call this method if you performed any writes to the object.

func (*RotateLogs) CurrentFileName Uses

func (rl *RotateLogs) CurrentFileName() string

CurrentFileName returns the current file name that the RotateLogs object is writing to

func (*RotateLogs) Rotate Uses

func (rl *RotateLogs) Rotate() error

Rotate forcefully rotates the log files. If the generated file name clash because file already exists, a numeric suffix of the form ".1", ".2", ".3" and so forth are appended to the end of the log file

Thie method can be used in conjunction with a signal handler so to emulate servers that generate new log files when they receive a SIGHUP

func (*RotateLogs) Write Uses

func (rl *RotateLogs) Write(p []byte) (n int, err error)

Write satisfies the io.Writer interface. It writes to the appropriate file handle that is currently being used. If we have reached rotation time, the target file gets automatically rotated, and also purged if necessary.

Directories

PathSynopsis
internal/fileutil
internal/option

Package rotatelogs imports 12 packages (graph) and is imported by 68 packages. Updated 2020-10-29. Refresh now. Tools for package owners.