logger

package
v0.0.0-...-38ef2fc Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2020 License: BSD-3-Clause Imports: 8 Imported by: 5

Documentation

Overview

Package logger provides a flexible way to log information with different levels and on different backends. For tests create a test writer with

w := logger.NewTestWriter()
current := logger.SetWriter(w)
defer logger.SetWriter(current)

Now logged entries can be retrieved and reseted.

es := w.Entries()
w.Reset()

The default logger writes to stdout, others can be instantiated with any io.Writer. logger.NewGoWriter() returns a writer using the standard Go logging implementation and logger.NewSysWriter() returs a writer based on the system log.

The levels are Debug, Info, Warning, Error, Critical, and Fatal. Here logger.Debugf() also logs information about file name, function name, and line number while log.Fatalf() may end the program depending on the set FatalExiterFunc.

Changes to the standard behavior can be made with logger.SetLevel() and logger.SetFatalExiter(). Own logger backends and exiter can be defined. Additionally a filter function allows to drill down the logged entries.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Criticalf

func Criticalf(format string, args ...interface{})

Criticalf logs a message at critical level.

func Debugf

func Debugf(format string, args ...interface{})

Debugf logs a message at debug level.

func Errorf

func Errorf(format string, args ...interface{})

Errorf logs a message at error level.

func Fatalf

func Fatalf(format string, args ...interface{})

Fatalf logs a message at fatal level. After logging the message the function calls the fatal exiter function, which by default means exiting the application with error code -1. So only call in real fatal cases.

func Infof

func Infof(format string, args ...interface{})

Infof logs a message at info level.

func OSFatalExiter

func OSFatalExiter()

OSFatalExiter exits the application with os.Exit and the return code -1.

func PanicFatalExiter

func PanicFatalExiter()

PanicFatalExiter exits the application with a panic.

func Warningf

func Warningf(format string, args ...interface{})

Warningf logs a message at warning level.

Types

type Entries

type Entries interface {
	// Len returns the number of collected entries.
	Len() int

	// Entries returns the collected entries.
	Entries() []string

	// Reset clears the collected entries.
	Reset()
}

Entries contains the collected entries of a test writer.

type FatalExiterFunc

type FatalExiterFunc func()

FatalExiterFunc defines a functions that will be called in case of a Fatalf call.

func SetFatalExiter

func SetFatalExiter(fef FatalExiterFunc) FatalExiterFunc

SetFatalExiter sets the fatal exiter function to a new one and returns the current.

type FilterFunc

type FilterFunc func(level LogLevel, msg string) bool

FilterFunc allows to filter the output of the logging. Filters have to return true if the received entry shall be filtered and not output.

func SetFilter

func SetFilter(ff FilterFunc) FilterFunc

SetFilter sets the global output filter to a new one and returns the current. Nil function is allowed, it unsets the filter.

func UnsetFilter

func UnsetFilter() FilterFunc

UnsetFilter removes the global output filter and returns the current.

type LogLevel

type LogLevel int

LogLevel describes the chosen log level between debug and critical.

const (
	LevelDebug LogLevel = iota
	LevelInfo
	LevelWarning
	LevelError
	LevelCritical
	LevelFatal
)

Log levels to control the logging output.

func Level

func Level() LogLevel

Level returns the current log level.

func SetLevel

func SetLevel(level LogLevel) LogLevel

SetLevel sets the log level to a new one and returns the current.

type TestWriter

type TestWriter interface {
	Writer
	Entries
}

TestWriter extends the Writer interface with methods to retrieve and reset the collected data for testing purposes.

func NewTestWriter

func NewTestWriter() TestWriter

NewTestWriter returns a special writer for testing purposes.

type Writer

type Writer interface {
	// Write writes the given message with additional
	// information at the specific log level.
	Write(level LogLevel, msg string) error
}

Writer is the interface for different log writers.

func NewGoWriter

func NewGoWriter() Writer

NewGoWriter creates a writer using the Go log package.

func NewStandardOutWriter

func NewStandardOutWriter() Writer

NewStandardOutWriter creates the standard writer writing to STDOUT.

func NewStandardWriter

func NewStandardWriter(out io.Writer) Writer

NewStandardWriter creates the standard writer writing to the passed output.

func NewSysWriter

func NewSysWriter(tag string) (Writer, error)

NewSysWriter creates a writer using the Go syslog package. It does not work on Windows or Plan9. Here the Go log package is used.

func NewTimeformatWriter

func NewTimeformatWriter(out io.Writer, timeFormat string) Writer

NewTimeformatWriter creates a writer writing to the passed output and with the specified time format.

func SetWriter

func SetWriter(out Writer) Writer

SetWriter sets the writing target to a new one and returns the current.

Jump to

Keyboard shortcuts

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