• Stars
    star
    4,272
  • Rank 10,098 (Top 0.2 %)
  • Language
    Go
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

lumberjack is a log rolling package for Go

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

More Repositories

1

gorram

It's like go run for any go function
Go
1,038
star
2

pie

a toolkit for creating plugins for Go applications
Go
747
star
3

npipe

A Windows named pipe implementation written in pure Go.
Go
282
star
4

deputy

deputy is a go package that adds smarts on top of os/exec
Go
230
star
5

atomic

atomic is a go package for atomic file writing
Go
190
star
6

godocgo

An example of good godoc documentation.
Go
154
star
7

gocog

Generate code for any language, with any language.
Go
68
star
8

sh

Package sh makes working with shell commands more shell-like. Inspired by https://github.com/amoffat/sh
Go
52
star
9

npf

code to host a professional site
CSS
52
star
10

graffiti

graffiti is a tool to automatically add struct tags to fields in your go code
Go
38
star
11

claymud

Highly configurable and performant MUD written in Go
Go
28
star
12

keep

Go bindings wrapping the Google Keep API
Go
22
star
13

diceware

an implementation of the diceware passphrase generation algorithm
Go
21
star
14

blogimport

A tool to import from Blogger to Hugo
Go
20
star
15

tree

A statically typed binary tree in Go without casts or reflection
Go
19
star
16

pcgrep

Go
15
star
17

covergen

autogenerate a coverage badge for your README with with one dumb trick
Go
10
star
18

convert

An application to convert and resize images written in Go.
Go
10
star
19

plugman

a plugin manifest manager for go
9
star
20

q

Q is a CLI tool for developers that can do anything.
Go
8
star
21

avl

Go
6
star
22

natefinch.github.io

nate's sweet website
HTML
5
star
23

tod

A Hugo site for my D&D campaign
CSS
4
star
24

filterr

go package that helps filter returned errors to a specific list
Go
4
star
25

wrap

Wrap contains a method for wrapping one Go error with another.
Go
4
star
26

Ashes2Ashes

A modified CircleMUD that ran in the late 90's.
C
3
star
27

cavalier

a command line interface generator for Go
Go
3
star
28

emojis-and-dragons

2
star
29

nolog

just a dumb little tool to run juju tests and filter out all the logging
Go
2
star
30

natefinch

1
star
31

harvard-4-h

CSS
1
star
32

rpg

a personal website about RPGs
CSS
1
star
33

circle2json

converts CircleMUD room definitions to json equivalents
Go
1
star
34

go-quickstart

Go
1
star
35

burningchrome

A website for the in-development Burning Chrome RPG.
CSS
1
star
36

eg

An enhanced error package for Go
Go
1
star
37

tabletop.design

HTML
1
star
38

treesample

Sample project using natefinch/tree
Go
1
star