123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package logger
- import (
- "fmt"
- "os"
- )
- type Level int8
- const (
- // TraceLevel level. Designates finer-grained informational events than the Debug.
- TraceLevel Level = iota - 2
- // DebugLevel level. Usually only enabled when debugging. Very verbose logging.
- DebugLevel
- // InfoLevel is the default logging priority.
- // General operational entries about what's going on inside the application.
- InfoLevel
- // WarnLevel level. Non-critical entries that deserve eyes.
- WarnLevel
- // ErrorLevel level. Logs. Used for errors that should definitely be noted.
- ErrorLevel
- // FatalLevel level. Logs and then calls `logger.Exit(1)`. highest level of severity.
- FatalLevel
- )
- 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"
- case FatalLevel:
- return "fatal"
- }
- return ""
- }
- // LevelForGorm 转换成gorm日志级别
- func (l Level) LevelForGorm() int {
- switch l {
- case FatalLevel, ErrorLevel:
- return 2
- case WarnLevel:
- return 3
- case InfoLevel, DebugLevel, TraceLevel:
- return 4
- default:
- return 1
- }
- }
- // 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
- }
- return InfoLevel, fmt.Errorf("Unknown Level String: '%s', defaulting to InfoLevel", levelStr)
- }
- 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...)
- os.Exit(1)
- }
- func Fatalf(template string, args ...interface{}) {
- DefaultLogger.Logf(FatalLevel, template, args...)
- os.Exit(1)
- }
- // Returns true if the given level is at or lower the current logger level
- func V(lvl Level, log Logger) bool {
- l := DefaultLogger
- if log != nil {
- l = log
- }
- return l.Options().Level <= lvl
- }
|