bugolog
A logging wrapper for golang
Installation
go get codeberg.org/mbuttner/bugolog
Usage
Import
import (
. codeberg.org/mbuttner/bugolog
)
Filter messages from the output
SetLogLevel(LogLevel)
Log levels are defined as constants in the module, available log levels
are LOG_DBG, LOG_INF, LOG_WRN, LOG_ERR and LOG_FATAL. Messages with a level
below the level passed to SetLogLevel will not be printed.
Default is LOG_INF, all messages will be printed except those with level LOG_DEBG.
Add source code references
SetTraceMode(bool)
When trace mode is enabled through a call to SetTraceMode(true), each printed
message will contain the source file and line of the logging statement.
Standalone mode
SetStandaloneMode(bool)
The default behavior is to NOT add a timestamp to the logging message, as the
expected usage is within a service running under systemd (which already adds a
timestamp to the message). For console output during test activities, or in case
the application is not running as a systemd service, consider enabling the standalone
mode with a call to SetStandaloneMode(true), so that the library adds a timestamp
to each printed message.
Log messages, conditional logic
Read the currently selected log level
GetLogLevel()
In case your application needs at some point to find out to what level the filter
was set, call GetLogLevel(). For instance, some debugging code should only be executed
when the selected log level is "debug", than you will need GetLogLevel()
Log a message
Log(LogLevel, string, ...interface{}) string
The signature of the Log function is close to something like fprintf, with following
parameters:
- LogLevel: should be one of the LOG_* constants, if the passed LogLevel is below
the one configured by SetLogLevel (or below the default LOG_INF), the
message will be discarded from output
- string: printf-style format string. Do not add the 'end-of-line', it will be
done automatically by the library.
- ...interface{}: data for substitution to be applied to the format
Misc
Note the special behavior of LogLevel LOG_FATAL: the application will first log the message
and than stop. SetLogLevel(LOG_FATAL) will result in a log output reduced to no (if your
application completes fine) or 1 message (in case your application reaches the Log(LOG_FATAL ...)
statement).