Logger package implements logging methods with formatted output and more detailed logging information than provided in the standard logging package of golang. It is possible to specify the level of logging information to be written as output.
setLevel(level LogLevel)
sets the level of logging information written as output.
There are four possible values for argument level
of type LogLevel
in ascending info order:
LEVEL_ERROR
: Only Error logs are written tostderr
.LEVEL_WARNING
: Warnings and errors are written tostderr
.LEVEL_INFO
: Errors and warnings are written tostderr
, infos tostdout
.LEVEL_DEBUG
: Errors and warnings are written tostderr
, infos and debugs tostdout
.
To print logging information you can choose one of the following *log.Logger
variables chaining to common I/O writers:
Error
Warning
Info
Debug
The following Log flags are set:
log.Ldate
: the date in the local time zone
log.Ltime
: the time in the local time zone
log.Lshortfile
: final file name element and line number
log.Lmsgprefix
: move the "prefix" log level tag from the beginning of the line to before the message
logger.SetLevel(logger.LEVEL_WARNING)
suffix := 2
logger.Error.Printf("error %d", suffix)
logger.Warning.Printf("warning %d", suffix)
logger.Info.Printf("info %d", suffix)
logger.Debug.Printf("debug %d", suffix)
has the following output:
2021/10/07 12:18:12 main.go:16: [ERROR]: error 2
2021/10/07 12:18:12 main.go:17: [WARNING]: warning 2
logger.SetLevel(logger.LEVEL_DEBUG)
suffix := 4
logger.Error.Printf("error %d", suffix)
logger.Warning.Printf("warning %d", suffix)
logger.Info.Printf("info %d", suffix)
logger.Debug.Printf("debug %d", suffix)
prints all logging information:
2021/10/07 12:18:12 main.go:32: [ERROR]: error 4
2021/10/07 12:18:12 main.go:33: [WARNING]: warning 4
2021/10/07 12:18:12 main.go:34: [INFO]: info 4
2021/10/07 12:18:12 main.go:35: [DEBUG]: debug 4
To perform the unit tests, you can call from the root folder:
go test
To run the example:
go run example/main.go