lumberjackrus

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2019 License: MIT Imports: 3 Imported by: 29

README

lumberjackrus | local filesystem hook for Logrus

Example

package main

import (
	"github.com/sirupsen/logrus"
	"github.com/orandin/lumberjackrus"
)

func init() {
	logrus.SetFormatter(&logrus.TextFormatter{})
	logrus.SetLevel(logrus.DebugLevel)

	hook, err := lumberjackrus.NewHook(
		&lumberjackrus.LogFile{
			Filename:   "/tmp/general.log",
			MaxSize:    100,
			MaxBackups: 1,
			MaxAge:     1,
			Compress:   false,
			LocalTime:  false,
		},
		logrus.InfoLevel,
		&logrus.TextFormatter{},
		&lumberjackrus.LogFileOpts{
			logrus.InfoLevel: &lumberjackrus.LogFile{
				Filename: "/tmp/info.log",
			},
			logrus.ErrorLevel: &lumberjackrus.LogFile{
				Filename:   "/tmp/error.log",
				MaxSize:    100,   // optional
				MaxBackups: 1,     // optional
				MaxAge:     1,     // optional
				Compress:   false, // optional
				LocalTime:  false, // optional
			},
		},
	)

	if err != nil {
		panic(err)
	}

	logrus.AddHook(hook)
}

func main() {
	logrus.Debug("Debug message") // It is not written to a file (because debug level < minLevel)
	logrus.Info("Info message")   // Written in /tmp/info.log
	logrus.Warn("Warn message")   // Written in /tmp/general.log
	logrus.Error("Error message") // Written in /tmp/error.log
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hook

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

func NewHook

func NewHook(defaultLogger *LogFile, minLevel logrus.Level, formatter logrus.Formatter, opts *LogFileOpts) (*Hook, error)

func (*Hook) Fire

func (hook *Hook) Fire(entry *logrus.Entry) error

func (*Hook) Levels

func (hook *Hook) Levels() []logrus.Level

type LogFile

type LogFile 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.
	Compress bool `json:"compress" yaml:"compress"`
}

type LogFileOpts

type LogFileOpts map[logrus.Level]*LogFile

Jump to

Keyboard shortcuts

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