logger

package
v4.24.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2017 License: BSD-3-Clause Imports: 10 Imported by: 130

Documentation

Overview

Package logger of the Tideland Go Library provides a flexible way to log information with different levels and on different backends.

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 logger.Fatalf() may end the program depending on the set FatalExiterFunc.

Different backends may be set. The standard logger writes to an io.Writer (initially os.Stdout), the go logger uses the Go log package, and the sys logger uses the Go syslog package on the according operating systems. For testing the test logger exists. When created also a fetch function is return. It returns the logged strings which can be used inside of tests then.

Changes to the standard behavior can be made with logger.SetLevel(), logger.SetLogger(), and logger.SetFatalExiter(). Own logger backends and exiter can be defined.

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 independent of any level. After logging the message the functions calls the fatal exiter function, which by default means exiting the application with error code -1.

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 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 and returns the current one.

type FilterFunc

type FilterFunc func(level LogLevel, info, 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 and returns the current one.

func UnsetFilter

func UnsetFilter() FilterFunc

UnsetFilter removes the global output folter and returns the current one.

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 switches to a new log level and returns the current one.

func SetLevelString

func SetLevelString(levelstr string) LogLevel

SetLevelString switches to a new log level passed as string. Accepted are the values "debug", "info", "warning" "error", "critical", and "fatal". The passed string will be set to lower-case. The function is intended to be used when then log level is read out of a configuration.

type Logger

type Logger interface {
	// Debug logs a debugging message.
	Debug(info, msg string)

	// Info logs an informational message.
	Info(info, msg string)

	// Warning logs a warning message.
	Warning(info, msg string)

	// Error logs an error message.
	Error(info, msg string)

	// Critical logs a critical error message.
	Critical(info, msg string)

	// Fatal logs a fatal error message.
	Fatal(info, msg string)
}

Logger is the interface for different logger backends.

func NewGoLogger

func NewGoLogger() Logger

NewGoLogger returns a logger implementation using the Go log package.

func NewStandardLogger

func NewStandardLogger(out io.Writer) Logger

NewStandardLogger creates the standard logger writing to the passed output.

func NewSysLogger

func NewSysLogger(tag string) (Logger, error)

NewSysLogger returns a logger implementation using the Go syslog package.

func NewTimeformatLogger

func NewTimeformatLogger(out io.Writer, timeFormat string) Logger

NewTimeformatLogger creates a logger writing to the passed output and with the time formatted with the passed time format.

func SetLogger

func SetLogger(l Logger) Logger

SetLogger sets a new logger.

type SysLogger

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

SysLogger uses the Go syslog package as logging backend. It does not work on Windows or Plan9.

func (*SysLogger) Critical

func (sl *SysLogger) Critical(info, msg string)

Critical is specified on the Logger interface.

func (*SysLogger) Debug

func (sl *SysLogger) Debug(info, msg string)

Debug is specified on the Logger interface.

func (*SysLogger) Error

func (sl *SysLogger) Error(info, msg string)

Error is specified on the Logger interface.

func (*SysLogger) Fatal

func (sl *SysLogger) Fatal(info, msg string)

Fatal is specified on the Logger interface.

func (*SysLogger) Info

func (sl *SysLogger) Info(info, msg string)

Info is specified on the Logger interface.

func (*SysLogger) Warning

func (sl *SysLogger) Warning(info, msg string)

Warning is specified on the Logger interface.

type TestLogger

type TestLogger interface {
	Logger

	// Len returns the number of collected entries.
	Len() int

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

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

TestLogger extends the Logger interface with methods to retrieve and reset the collected data.

func NewTestLogger

func NewTestLogger() TestLogger

NewTestLogger returns a special logger for testing.

Jump to

Keyboard shortcuts

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