2020-02-07 05:35:46 +08:00
|
|
|
package logger
|
|
|
|
|
2020-03-09 09:23:42 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
2020-02-20 23:57:59 -08:00
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// Level means logger level
|
2020-02-07 05:35:46 +08:00
|
|
|
type Level int8
|
|
|
|
|
|
|
|
const (
|
2020-02-20 23:57:59 -08:00
|
|
|
// TraceLevel level. Designates finer-grained informational events than the Debug.
|
|
|
|
TraceLevel Level = iota - 2
|
|
|
|
// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
|
2020-02-07 05:35:46 +08:00
|
|
|
DebugLevel
|
2021-01-10 18:56:39 +03:00
|
|
|
// InfoLevel level. General operational entries about what's going on inside the application.
|
2020-02-07 05:35:46 +08:00
|
|
|
InfoLevel
|
2020-02-20 23:57:59 -08:00
|
|
|
// WarnLevel level. Non-critical entries that deserve eyes.
|
2020-02-07 05:35:46 +08:00
|
|
|
WarnLevel
|
2021-01-10 18:56:39 +03:00
|
|
|
// ErrorLevel level. Used for errors that should definitely be noted.
|
2020-02-07 05:35:46 +08:00
|
|
|
ErrorLevel
|
2020-11-04 00:38:12 +03:00
|
|
|
// FatalLevel level. Logs and then calls `os.Exit(1)`. highest level of severity.
|
2020-02-07 05:35:46 +08:00
|
|
|
FatalLevel
|
|
|
|
)
|
2020-02-15 10:19:28 -08:00
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// String returns logger level string representation
|
2020-02-15 10:19:28 -08:00
|
|
|
func (l Level) String() string {
|
|
|
|
switch l {
|
|
|
|
case TraceLevel:
|
|
|
|
return "trace"
|
|
|
|
case DebugLevel:
|
|
|
|
return "debug"
|
|
|
|
case InfoLevel:
|
|
|
|
return "info"
|
|
|
|
case WarnLevel:
|
|
|
|
return "warn"
|
|
|
|
case ErrorLevel:
|
|
|
|
return "error"
|
2020-02-20 23:57:59 -08:00
|
|
|
case FatalLevel:
|
|
|
|
return "fatal"
|
2020-02-15 10:19:28 -08:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2020-02-20 23:57:59 -08:00
|
|
|
|
|
|
|
// Enabled returns true if the given level is at or above this level.
|
|
|
|
func (l Level) Enabled(lvl Level) bool {
|
|
|
|
return lvl >= l
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLevel converts a level string into a logger Level value.
|
|
|
|
// returns an error if the input string does not match known values.
|
|
|
|
func GetLevel(levelStr string) (Level, error) {
|
|
|
|
switch levelStr {
|
|
|
|
case TraceLevel.String():
|
|
|
|
return TraceLevel, nil
|
|
|
|
case DebugLevel.String():
|
|
|
|
return DebugLevel, nil
|
|
|
|
case InfoLevel.String():
|
|
|
|
return InfoLevel, nil
|
|
|
|
case WarnLevel.String():
|
|
|
|
return WarnLevel, nil
|
|
|
|
case ErrorLevel.String():
|
|
|
|
return ErrorLevel, nil
|
|
|
|
case FatalLevel.String():
|
|
|
|
return FatalLevel, nil
|
|
|
|
}
|
2020-11-04 00:38:12 +03:00
|
|
|
return InfoLevel, fmt.Errorf("unknown Level String: '%s', use InfoLevel", levelStr)
|
2020-03-09 23:43:05 +03:00
|
|
|
}
|