A lightweight Golang logging library, supporting customizable log levels, color-coding, optional date/time formatting, and some more.
To get started, instantiate a Logger by calling logs.NewLogger(). This method returns a pointer to a new Logger instance:
logger := logs.NewLogger()
logger.Warn("my warning test") // stdout: 'WARN: my warning test'
logger.Info("my", "into", "test", 2) // stdout: 'INFO: my info test 2'
logger.Debug("hey", "check this", "debug") // stdout: 'DEBUG: hey check this debug'
logger.Error("here is the error") // stderr: 'ERROR: here is the error'The Logger struct implements the Builder design pattern allowing you to dynamically configure various settings.
Use builder methods to customize logging behavior, such as setting the log level, enabling colors, or adding date/time stamps.
logger := logs.NewLogger().
SetLogLvl(ll.WarnLvlName).
EnableColors(true).
AddTime(true).AddDate(true)
logger.Warn("This is my Warn log", "Test arg")
// OUTPUT: WARN[03/11/2024 18:35:43]: This is my Warn log Test arg
logger.SetEncoder(shared.JsonEncoderType)
logger.Debug("This is my Debug log", "Test arg")
// OUTPUT: {"level":"DEBUG","date":"03/11/2024","time":"18:35:43","message":"This is my Debug log Test arg"}
logger.AddTime(false)
logger.Debug("This is my Debug log", "Test arg")
// OUTPUT: {"level":"DEBUG","date":"03/11/2024","message":"This is my Debug log Test second arg"}Each Logger has a Log Level property that determines which message categories to print, based on a priority
hierarchy.
The log levels are ordered as follows:
Error: 0
Warn: 1
Info: 2
Debug: 3By default, the Logger is set to the Debug level, so all log levels will be printed to the output.
Adjusting the Log Level to a lower setting, such as Warn, will limit output to only Warn and Error messages,
filtering out Info and Debug.
- Using the method
logs.NewLogger().SetLogLvl(WarnLvlName)- Using an Environment Variable: Set the log level through an environment variable and configure the Logger to retrieve
it using the
SetLogLvlEnvVariable()method.
logs.NewLogger().SetLogLvlEnvVariable("MY_LOGLVL_ENV_VAR_NAME")