logr

package module
Version: v0.0.0-...-a1d083a Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2017 License: MIT Imports: 8 Imported by: 0

README

logr

This library is unmaintained: use lumberjack instead

logr is a simplistic type which implements log rotating, suitable for use with Go's log package.

Build Status GoDoc

Usage

Daily rotation.

w := logr.NewWriter("/var/log/mylog.log", &logr.Options{
    RotateDaily: true,
})
log.SetOutput(w)

log.Println("foobar")

Maximum size rotation.

// Rotate every 500 Mib.
w := logr.NewWriter("/var/log/mylog.log", &logr.Options{
    MaximumSize: 1024 * 1024 * 500,
})
log.SetOutput(w)

log.Println("foobar")

Compress the rotated file.

// Rotate every 500 Mib then compress the file.
w := logr.NewWriter("/var/log/mylog.log", &logr.Options{
    MaximumSize: 1024 * 1024 * 500,
    Compress: true,
})
log.SetOutput(w)

log.Println("foobar")

Documentation

Index

Constants

View Source
const (
	// TimeFormat is the default format used for the suffix date and time on each rotated log.
	TimeFormat = "2006-01-02_1504"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	TimeFormat         string
	TimeFormatAsPrefix bool
	RotateDaily        bool
	Compress           bool
	MaximumSize        int64
}

Options allows you to customize tne behavior of a RotatingWriter.

type RotatingWriter

type RotatingWriter struct {
	sync.Mutex
	// contains filtered or unexported fields
}

RotatingWriter is a io.Writer which wraps a *os.File, suitable for log rotation.

func NewWriter

func NewWriter(filename string, opts *Options) (*RotatingWriter, error)

NewWriter creates a new file and returns a rotating writer.

func NewWriterFromFile

func NewWriterFromFile(file *os.File, opts *Options) (*RotatingWriter, error)

NewWriterFromFile creates a rotating writer using the provided file as base.

The caller must take care to not close the file it provides here, as the RotatingWriter will do it automatically when rotating.

func (*RotatingWriter) Write

func (w *RotatingWriter) Write(b []byte) (int, error)

Write implements the io.Writer interface.

NOTE(vincent): the rotating is not perfect when you want to rotate based on the maximum size. We won't rotate in the middle of a call to Write, so a call to Write will either end up in the original file or the new file if it needs to be rotated. That is to say, a call to Write will never split up the data from b into two different files. With this you can see that, given a big enough b, the original file CAN be significantly bigger than the requested maximum size. However, in practice it will never be a problem:

- the std package log will always write small buffers
- if you use your own logger, you should never use a big buffer anyway.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL