2020-02-07 05:35:46 +08:00
|
|
|
package logger
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// Level means logger level
|
2020-02-07 05:35:46 +08:00
|
|
|
type Level int8
|
|
|
|
|
|
|
|
const (
|
2021-08-27 18:55:08 +03:00
|
|
|
// TraceLevel level usually used to find bugs, very verbose
|
2020-02-20 23:57:59 -08:00
|
|
|
TraceLevel Level = iota - 2
|
2021-08-27 18:55:08 +03:00
|
|
|
// DebugLevel level used only when enabled debugging
|
2020-02-07 05:35:46 +08:00
|
|
|
DebugLevel
|
2021-08-27 18:55:08 +03:00
|
|
|
// InfoLevel level used for general info about what's going on inside the application
|
2020-02-07 05:35:46 +08:00
|
|
|
InfoLevel
|
2021-08-27 18:55:08 +03:00
|
|
|
// WarnLevel level used for non-critical entries
|
2020-02-07 05:35:46 +08:00
|
|
|
WarnLevel
|
2021-08-27 18:55:08 +03:00
|
|
|
// ErrorLevel level used for errors that should definitely be noted
|
2020-02-07 05:35:46 +08:00
|
|
|
ErrorLevel
|
2021-08-27 18:55:08 +03:00
|
|
|
// FatalLevel level used for critical errors and then calls `os.Exit(1)`
|
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
|
|
|
}
|
2021-08-27 18:55:08 +03:00
|
|
|
return "info"
|
2020-02-15 10:19:28 -08:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2021-08-27 18:55:08 +03:00
|
|
|
// ParseLevel converts a level string into a logger Level value.
|
|
|
|
// returns an InfoLevel if the input string does not match known values.
|
|
|
|
func ParseLevel(lvl string) Level {
|
|
|
|
switch lvl {
|
2020-02-20 23:57:59 -08:00
|
|
|
case TraceLevel.String():
|
2021-08-27 18:55:08 +03:00
|
|
|
return TraceLevel
|
2020-02-20 23:57:59 -08:00
|
|
|
case DebugLevel.String():
|
2021-08-27 18:55:08 +03:00
|
|
|
return DebugLevel
|
2020-02-20 23:57:59 -08:00
|
|
|
case InfoLevel.String():
|
2021-08-27 18:55:08 +03:00
|
|
|
return InfoLevel
|
2020-02-20 23:57:59 -08:00
|
|
|
case WarnLevel.String():
|
2021-08-27 18:55:08 +03:00
|
|
|
return WarnLevel
|
2020-02-20 23:57:59 -08:00
|
|
|
case ErrorLevel.String():
|
2021-08-27 18:55:08 +03:00
|
|
|
return ErrorLevel
|
2020-02-20 23:57:59 -08:00
|
|
|
case FatalLevel.String():
|
2021-08-27 18:55:08 +03:00
|
|
|
return FatalLevel
|
2020-02-20 23:57:59 -08:00
|
|
|
}
|
2021-08-27 18:55:08 +03:00
|
|
|
return InfoLevel
|
2020-03-09 23:43:05 +03:00
|
|
|
}
|