Documentation ¶
Overview ¶
Package logging provides a logging API and an implementation to output log messages to stderr or any other Writer. The complete logging package can be used concurrently from multiple goroutines. The performance costs for handling suppressed log messages are minimal (one non-blocking atomic integer comparison).
Each part of the application creates its own Logger in the global var section or within the init function:
var ( log := logging.Get("my_id") )
For each log statement a level is set by the invoked log function. The log levels are ordered as follows: error (highest severity) < warning < info < debug < trace (lowest severity).
The Logger provides various functions to log and format messages at different levels. The log messages are formatted through fmt.Sprint and fmt.Sprintf:
log.Error("my error message: ", myValue) log.Debugf("my debug message: %v", myValue)
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type LogFlags ¶
type LogFlags int32
LogFlags are used to modify the output format of log messages.
type LogLevel ¶
type LogLevel int32
LogLevel describes the severity of log messages.
Defined log levels.
func (LogLevel) MarshalText ¶
MarshalText implements TextUnmarshaler (for e.g. JSON encoding). For the method to be found by the JSON encoder, use a value receiver.
func (*LogLevel) Set ¶
Set updates the log level. Valid identifiers are: off, error, warning, info, debug, trace. Upper/lower case is ignored. Identifiers can be shortened (e.g. err, i). This function is part of the flag.Value interface.
func (LogLevel) String ¶
String returns a string representation of the stored level. This function is part of the flag.Value interface.
func (*LogLevel) UnmarshalText ¶
UnmarshalText implements TextMarshaler (for e.g. JSON decoding).
type LogLevelFlag ¶
type LogLevelFlag struct{}
LogLevelFlag can be used with flag.Var and sets/gets the active level.
func (*LogLevelFlag) Set ¶
func (*LogLevelFlag) Set(value string) error
Set updates the active log level. Valid identifiers are: off, error, warning, info, debug, trace. Upper/lower case is ignored. Identifiers can be shortened (e.g. err, i). This function is part of the flag.Value interface.
func (*LogLevelFlag) String ¶
func (*LogLevelFlag) String() string
String returns a string representation of the active level. This function is part of the flag.Value interface.
type Logger ¶
type Logger interface { // Will a specific log level output a log message to the Writer? These // functions can be used to avoid costly builds of log messages. ErrorEnabled() bool WarningEnabled() bool InfoEnabled() bool DebugEnabled() bool TraceEnabled() bool // Create a log message using fmt.Sprint for formatting. Error(values ...interface{}) Warning(values ...interface{}) Info(values ...interface{}) Debug(values ...interface{}) Trace(values ...interface{}) // Create a log message using fmt.Sprintf for formatting. Errorf(format string, values ...interface{}) Warningf(format string, values ...interface{}) Infof(format string, values ...interface{}) Debugf(format string, values ...interface{}) Tracef(format string, values ...interface{}) }
A Logger is used to generate log messages.
Example ¶
SetLevel(InfoLevel) SetWriter(os.Stdout) SetFlags(LevelFlag | IdentifierFlag) log1 := Get("test1") log2 := Get("test2") p1 := 123 p2 := "abc" log1.Error("error messsage ", p1, " ", p2) log2.Trace("trace messsage") log1.Warning("warning messsage") log1.Info("info messsage") log2.Warningf("warning messsage %v %v", p1, p2) log1.Debug("debug messsage") log1.Trace("trace messsage") log2.Info("1st line\n2nd line")
Output: ERROR |test1 |error messsage 123 abc WARNING|test1 |warning messsage INFO |test1 |info messsage WARNING|test2 |warning messsage 123 abc INFO |test2 |1st line\n2nd line