parent
4332dd475f
commit
be6f2692e2
|
@ -0,0 +1,29 @@
|
||||||
|
package logger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// getEnvBool returns a boolean value read from the environmnet variable.
|
||||||
|
// If the variable was not set, the default value will be returned
|
||||||
|
func getEnvBool(name string, defaultValue bool) bool {
|
||||||
|
val := defaultValue
|
||||||
|
if strVal, isSet := os.LookupEnv(name); isSet {
|
||||||
|
strVal = strings.ToLower(strVal)
|
||||||
|
return strVal == "1" || strVal == "true" || strVal == "yes" || strVal == "ja"
|
||||||
|
}
|
||||||
|
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
// getEnvString returns a string value read from the environmnet variable.
|
||||||
|
// If the variable was not set, the default value will be returned
|
||||||
|
func getEnvString(name string, defaultValue string) string {
|
||||||
|
val := defaultValue
|
||||||
|
if strVal, isSet := os.LookupEnv(name); isSet {
|
||||||
|
val = strVal
|
||||||
|
}
|
||||||
|
|
||||||
|
return val
|
||||||
|
}
|
|
@ -0,0 +1,62 @@
|
||||||
|
package logger
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
// Level of the log message
|
||||||
|
type Level uint8
|
||||||
|
|
||||||
|
const (
|
||||||
|
LevelTrace Level = iota
|
||||||
|
LevelDebug
|
||||||
|
LevelInfo
|
||||||
|
LevelWarning
|
||||||
|
LevelError
|
||||||
|
LevelFatal
|
||||||
|
)
|
||||||
|
|
||||||
|
// String returns a string expression of the level
|
||||||
|
func (lvl Level) String() string {
|
||||||
|
switch lvl {
|
||||||
|
case LevelTrace:
|
||||||
|
return "TRACE"
|
||||||
|
case LevelDebug:
|
||||||
|
return "DEBUG"
|
||||||
|
case LevelInfo:
|
||||||
|
return "INFO"
|
||||||
|
case LevelWarning:
|
||||||
|
return "WARNING"
|
||||||
|
case LevelError:
|
||||||
|
return "ERROR"
|
||||||
|
case LevelFatal:
|
||||||
|
return "FATAL"
|
||||||
|
}
|
||||||
|
|
||||||
|
return "DEBUG"
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLevelByName tries to convert the given level name to the represented level code.
|
||||||
|
// Allowed values are: 'trace', 'debug', 'info', 'warn', 'warning', 'error', 'panic' and 'fatal'
|
||||||
|
// If an incorrect level name was given a warning is logged and info will be returned
|
||||||
|
func GetLevelByName(levelName string) Level {
|
||||||
|
levelName = strings.ToLower(levelName)
|
||||||
|
switch levelName {
|
||||||
|
case "trace":
|
||||||
|
return LevelTrace
|
||||||
|
case "debug":
|
||||||
|
return LevelDebug
|
||||||
|
case "info":
|
||||||
|
return LevelInfo
|
||||||
|
case "warn", "warning":
|
||||||
|
return LevelWarning
|
||||||
|
case "error":
|
||||||
|
return LevelError
|
||||||
|
case "panic", "fatal":
|
||||||
|
return LevelFatal
|
||||||
|
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
Warning("Unable to parse the level name '%s'. Expected 'debug', 'info', 'warn', 'error' or 'fatal'", levelName)
|
||||||
|
return LevelWarning
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
84
logger.go
84
logger.go
|
@ -12,18 +12,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Level of the log message
|
|
||||||
type Level uint8
|
|
||||||
|
|
||||||
const (
|
|
||||||
LevelTrace Level = iota
|
|
||||||
LevelDebug
|
|
||||||
LevelInfo
|
|
||||||
LevelWarning
|
|
||||||
LevelError
|
|
||||||
LevelFatal
|
|
||||||
)
|
|
||||||
|
|
||||||
type Logger struct {
|
type Logger struct {
|
||||||
PrintLevel Level
|
PrintLevel Level
|
||||||
LogLevel Level
|
LogLevel Level
|
||||||
|
@ -39,7 +27,7 @@ type Logger struct {
|
||||||
// While logging, the file and line number of the
|
// While logging, the file and line number of the
|
||||||
// invoking (calling) line can be printed out.
|
// invoking (calling) line can be printed out.
|
||||||
// This defines an offset that is applied to the call stack.
|
// This defines an offset that is applied to the call stack.
|
||||||
// If you you are using an own wrapper function, you
|
// If you are using an own wrapper function, you
|
||||||
// have to set this value to one
|
// have to set this value to one
|
||||||
FuncCallIncrement int
|
FuncCallIncrement int
|
||||||
|
|
||||||
|
@ -101,27 +89,7 @@ func (l *Logger) log(level Level, message string, parameters ...any) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the name of the level to log
|
// Get the name of the level to log
|
||||||
var levelName string
|
var levelName = fmt.Sprintf("%-5s", level)
|
||||||
switch level {
|
|
||||||
case LevelTrace:
|
|
||||||
levelName = "TRACE"
|
|
||||||
case LevelDebug:
|
|
||||||
levelName = "DEBUG"
|
|
||||||
case LevelInfo:
|
|
||||||
levelName = "INFO "
|
|
||||||
case LevelWarning:
|
|
||||||
levelName = "WARN "
|
|
||||||
case LevelError:
|
|
||||||
levelName = "ERROR"
|
|
||||||
case LevelFatal:
|
|
||||||
levelName = "FATAL"
|
|
||||||
}
|
|
||||||
|
|
||||||
if levelName == "" {
|
|
||||||
message = fmt.Sprintf("Invalid level value given: %d. Original message: ", level) + message
|
|
||||||
levelName = "WARN "
|
|
||||||
level = LevelWarning
|
|
||||||
}
|
|
||||||
|
|
||||||
printMessage := "[" + levelName + "] " + time.Now().Local().Format("2006-01-02 15:04:05") +
|
printMessage := "[" + levelName + "] " + time.Now().Local().Format("2006-01-02 15:04:05") +
|
||||||
getSourceMessage(file, line, pc, *l) + " - " + fmt.Sprintf(message, parameters...)
|
getSourceMessage(file, line, pc, *l) + " - " + fmt.Sprintf(message, parameters...)
|
||||||
|
@ -266,29 +234,27 @@ func CloseFile() {
|
||||||
dLogger.CloseFile()
|
dLogger.CloseFile()
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLevelByName tries to convert the given level name to the represented level code.
|
// GetLoggerFromEnv returns a logging instance configured
|
||||||
// Allowed values are: 'trace', 'debug', 'info', 'warn', 'warning', 'error', 'panic' and 'fatal'
|
// from the available environment variables.
|
||||||
// If an incorrect level name was given an warning is logged and info will be returned
|
//
|
||||||
func GetLevelByName(levelName string) Level {
|
// The environment variables have to be named like the struct
|
||||||
levelName = strings.ToLower(levelName)
|
// fields in upper case with the prefix "LOGGER_".
|
||||||
switch levelName {
|
// Sub structs are divided also by an underscore. Example:
|
||||||
case "trace":
|
// "LOGGER_SUBCONFIG_DISABLED"
|
||||||
return LevelTrace
|
//
|
||||||
case "debug":
|
// If no env variable was found the default value of the given
|
||||||
return LevelDebug
|
// logger struct will be used.
|
||||||
case "info":
|
//
|
||||||
return LevelInfo
|
// Note that only generic options can be set like:
|
||||||
case "warn", "warning":
|
// - Print and Log Level
|
||||||
return LevelWarning
|
// - Log path
|
||||||
case "error":
|
// - ColoredOutput
|
||||||
return LevelError
|
// - Tracing disabled
|
||||||
case "panic", "fatal":
|
func GetLoggerFromEnv(defaultLogger *Logger) *Logger {
|
||||||
return LevelFatal
|
defaultLogger.ColoredOutput = getEnvBool("LOGGER_COLOREDOUTPUT", defaultLogger.ColoredOutput)
|
||||||
|
defaultLogger.PrintLevel = GetLevelByName(getEnvString("LOGGER_PRINTLEVEL", defaultLogger.PrintLevel.String()))
|
||||||
default:
|
defaultLogger.LogLevel = GetLevelByName(getEnvString("LOGGER_LOGLEVEL", defaultLogger.LogLevel.String()))
|
||||||
{
|
defaultLogger.LogFilePath = getEnvString("LOGGER_LOGFILEPATH", defaultLogger.LogFilePath)
|
||||||
Warning("Unable to parse the level name '%s'. Expected 'debug', 'info', 'warn', 'error' or 'fatal'", levelName)
|
defaultLogger.PrintSource = getEnvBool("LOGGER_PRINTSOURCE", defaultLogger.PrintSource)
|
||||||
return LevelInfo
|
return NewLogger(defaultLogger)
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue