github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/logger/level.go (about)

     1  package logger
     2  
     3  import (
     4  	"os"
     5  
     6  	"go.uber.org/zap/zapcore"
     7  )
     8  
     9  const (
    10  	DebugLevel = "debug"
    11  	InfoLevel  = "info"
    12  	WarnLevel  = "warn"
    13  	ErrorLevel = "error"
    14  	PanicLevel = "panic"
    15  	FatalLevel = "fatal"
    16  )
    17  
    18  var loggerLevels = map[string]zapcore.Level{
    19  	DebugLevel: zapcore.DebugLevel,
    20  	InfoLevel:  zapcore.InfoLevel,
    21  	WarnLevel:  zapcore.WarnLevel,
    22  	ErrorLevel: zapcore.ErrorLevel,
    23  	PanicLevel: zapcore.PanicLevel,
    24  	FatalLevel: zapcore.FatalLevel,
    25  }
    26  
    27  func getZapLevel(l string) zapcore.Level {
    28  	level, exist := loggerLevels[l]
    29  	if !exist {
    30  		return zapcore.DebugLevel
    31  	}
    32  
    33  	return level
    34  }
    35  
    36  func Debug(args ...any) {
    37  	logger.Log(DebugLevel, args...)
    38  }
    39  
    40  func Debugf(format string, args ...any) {
    41  	logger.Logf(DebugLevel, format, args...)
    42  }
    43  
    44  func Info(args ...any) {
    45  	logger.Log(InfoLevel, args...)
    46  }
    47  
    48  func Infof(format string, args ...any) {
    49  	logger.Logf(InfoLevel, format, args...)
    50  }
    51  
    52  func Warn(args ...any) {
    53  	logger.Log(WarnLevel, args...)
    54  }
    55  
    56  func Warnf(format string, args ...any) {
    57  	logger.Logf(WarnLevel, format, args...)
    58  }
    59  
    60  func Error(args ...any) {
    61  	logger.Log(ErrorLevel, args...)
    62  }
    63  
    64  func Errorf(format string, args ...any) {
    65  	logger.Logf(ErrorLevel, format, args...)
    66  }
    67  
    68  func Fatal(args ...any) {
    69  	logger.Log(FatalLevel, args...)
    70  	os.Exit(1)
    71  }
    72  
    73  func Fatalf(format string, args ...any) {
    74  	logger.Logf(FatalLevel, format, args...)
    75  	os.Exit(1)
    76  }