logging

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2021 License: GPL-3.0 Imports: 8 Imported by: 18

README

go-logging

go-logging is a logging framework in the programming language Go.

License

This work is licensed under the GNU General Public License V3.

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

func SetFlags

func SetFlags(f LogFlags)

SetFlags sets flags for the output format.

func SetLevel

func SetLevel(lvl LogLevel)

SetLevel sets the required log level of the messages, which should be logged.

func SetWriter

func SetWriter(w io.Writer)

SetWriter sets a new Writer for outputting the log messages.

Types

type LogFlags

type LogFlags int32

LogFlags are used to modify the output format of log messages.

const (
	// Includes a timestamp in the log line.
	TimeFlag LogFlags = 1 << iota
	// Includes the log level.
	LevelFlag
	// Includes the identifer.
	IdentifierFlag
)

Defined flags for the output format.

func Flags

func Flags() LogFlags

Flags gets the current flags.

type LogLevel

type LogLevel int32

LogLevel describes the severity of log messages.

const (
	OffLevel LogLevel = iota
	ErrorLevel
	WarningLevel
	InfoLevel
	DebugLevel
	TraceLevel
)

Defined log levels.

func Level

func Level() LogLevel

Level gets the current log level.

func (LogLevel) MarshalText

func (l LogLevel) MarshalText() ([]byte, error)

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

func (l *LogLevel) Set(value string) error

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

func (l LogLevel) String() string

String returns a string representation of the stored level. This function is part of the flag.Value interface.

func (*LogLevel) UnmarshalText

func (l *LogLevel) UnmarshalText(text []byte) error

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

func Get

func Get(id string) Logger

Get creates an Logger with the specified identifier.

Jump to

Keyboard shortcuts

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