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 ¶
- func Criticalf(format string, args ...interface{})
- func Debugf(format string, args ...interface{})
- func Errorf(format string, args ...interface{})
- func Fatalf(format string, args ...interface{})
- func Infof(format string, args ...interface{})
- func OSFatalExiter()
- func PanicFatalExiter()
- func Warningf(format string, args ...interface{})
- type Entries
- type FatalExiterFunc
- type FilterFunc
- type LogLevel
- type TestWriter
- type Writer
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 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 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.
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 ¶
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.
Log levels to control the logging output.
type TestWriter ¶
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 NewStandardOutWriter ¶
func NewStandardOutWriter() Writer
NewStandardOutWriter creates the standard writer writing to STDOUT.
func NewStandardWriter ¶
NewStandardWriter creates the standard writer writing to the passed output.
func NewSysWriter ¶
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 ¶
NewTimeformatWriter creates a writer writing to the passed output and with the specified time format.