2020-02-07 05:35:46 +08:00
|
|
|
package logger
|
|
|
|
|
2020-03-09 09:23:42 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
)
|
2020-02-20 23:57:59 -08:00
|
|
|
|
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
|
2020-02-20 23:57:59 -08:00
|
|
|
// InfoLevel is the default logging priority.
|
|
|
|
// 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
|
2020-02-20 23:57:59 -08:00
|
|
|
// ErrorLevel level. Logs. Used for errors that should definitely be noted.
|
2020-02-07 05:35:46 +08:00
|
|
|
ErrorLevel
|
2020-02-20 23:57:59 -08:00
|
|
|
// FatalLevel level. Logs and then calls `logger.Exit(1)`. highest level of severity.
|
2020-02-07 05:35:46 +08:00
|
|
|
FatalLevel
|
|
|
|
)
|
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-02-26 21:34:40 +03:00
|
|
|
return InfoLevel, fmt.Errorf("Unknown Level String: '%s', defaulting to InfoLevel", levelStr)
|
2020-02-20 23:57:59 -08:00
|
|
|
}
|
2020-02-21 08:43:23 +00:00
|
|
|
|
|
|
|
func Info(args ...interface{}) {
|
|
|
|
DefaultLogger.Log(InfoLevel, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Infof(template string, args ...interface{}) {
|
|
|
|
DefaultLogger.Logf(InfoLevel, template, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Trace(args ...interface{}) {
|
|
|
|
DefaultLogger.Log(TraceLevel, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Tracef(template string, args ...interface{}) {
|
|
|
|
DefaultLogger.Logf(TraceLevel, template, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Debug(args ...interface{}) {
|
|
|
|
DefaultLogger.Log(DebugLevel, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Debugf(template string, args ...interface{}) {
|
|
|
|
DefaultLogger.Logf(DebugLevel, template, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Warn(args ...interface{}) {
|
|
|
|
DefaultLogger.Log(WarnLevel, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Warnf(template string, args ...interface{}) {
|
|
|
|
DefaultLogger.Logf(WarnLevel, template, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Error(args ...interface{}) {
|
|
|
|
DefaultLogger.Log(ErrorLevel, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Errorf(template string, args ...interface{}) {
|
|
|
|
DefaultLogger.Logf(ErrorLevel, template, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fatal(args ...interface{}) {
|
|
|
|
DefaultLogger.Log(FatalLevel, args...)
|
2020-03-09 09:23:42 +00:00
|
|
|
os.Exit(1)
|
2020-02-21 08:43:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Fatalf(template string, args ...interface{}) {
|
|
|
|
DefaultLogger.Logf(FatalLevel, template, args...)
|
2020-03-09 09:23:42 +00:00
|
|
|
os.Exit(1)
|
2020-02-21 08:43:23 +00:00
|
|
|
}
|
2020-03-09 23:43:05 +03:00
|
|
|
|
2020-03-13 17:36:42 +03:00
|
|
|
// Returns true if the given level is at or lower the current logger level
|
2020-09-05 02:11:29 +03:00
|
|
|
func V(lvl Level) bool {
|
|
|
|
return DefaultLogger.Options().Level <= lvl
|
2020-03-09 23:43:05 +03:00
|
|
|
}
|