1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
| package logger
import ( "encoding/json" "fmt" "go-api-practice/pkg/app" "go.uber.org/zap" "go.uber.org/zap/zapcore" "gopkg.in/natefinch/lumberjack.v2" "os" "strings" "time" )
var Logger *zap.Logger
func InitLogger(filename string, maxSize, maxBackups, maxAge int, compress bool, logType string, level string) { writeSyncer := getLoggerWriter(filename, maxSize, maxBackups, maxAge, compress, logType)
logLevel := new(zapcore.Level)
if err := logLevel.UnmarshalText([]byte(level)); err != nil { fmt.Println("日志级别设置错误") }
core := zapcore.NewCore(getEncoder(), writeSyncer, logLevel)
Logger = zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1), zap.AddStacktrace(zap.ErrorLevel))
zap.ReplaceGlobals(Logger) }
func getEncoder() zapcore.Encoder { encoderConfig := zapcore.EncoderConfig{ TimeKey: "time", LevelKey: "level", NameKey: "logger", CallerKey: "caller", FunctionKey: zapcore.OmitKey, MessageKey: "message", StacktraceKey: "stacktrace", LineEnding: zapcore.DefaultLineEnding, EncodeLevel: zapcore.CapitalLevelEncoder, EncodeTime: customTimeEncoder, EncodeDuration: zapcore.SecondsDurationEncoder, EncodeCaller: zapcore.ShortCallerEncoder, }
if app.IsLocal() { encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder return zapcore.NewConsoleEncoder(encoderConfig) }
return zapcore.NewJSONEncoder(encoderConfig) }
func customTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { enc.AppendString(t.Format("2006-01-02 15:04:05")) }
func getLoggerWriter(filename string, maxSize, maxBackups, maxAge int, compress bool, logType string) zapcore.WriteSyncer { if logType == "daily" { logname := time.Now().Format("2006-01-02.log") filename = strings.ReplaceAll(filename, "logs.log", logname) }
lumberJackLogger := &lumberjack.Logger{ Filename: filename, MaxSize: maxSize, MaxBackups: maxBackups, MaxAge: maxAge, Compress: compress, }
if app.IsLocal() { return zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(lumberJackLogger)) } else { return zapcore.AddSync(lumberJackLogger) } }
func Dump(value interface{}, msg ...string) { valueString := jsonString(value) if len(msg) > 0 { Logger.Warn("Dump", zap.String(msg[0], valueString)) } else { Logger.Warn("Dump", zap.String("data", valueString)) } }
func LogIf(err error) { if err != nil { Logger.Error("Error Occurred:", zap.Error(err)) } }
func LogWarnIf(err error) { if err != nil { Logger.Warn("Error Occurred:", zap.Error(err)) } }
func LogInfoIf(err error) { if err != nil { Logger.Info("Error Occurred:", zap.Error(err)) } }
func Debug(moduleName string, fields ...zap.Field) { Logger.Debug(moduleName, fields...) }
func Info(moduleName string, fields ...zap.Field) { Logger.Info(moduleName, fields...) }
func Warn(moduleName string, fields ...zap.Field) { Logger.Warn(moduleName, fields...) }
func Error(moduleName string, fields ...zap.Field) { Logger.Error(moduleName, fields...) }
func Fatal(moduleName string, fields ...zap.Field) { Logger.Fatal(moduleName, fields...) }
func DebugString(moduleName, name, msg string) { Logger.Debug(moduleName, zap.String(name, msg)) }
func InfoString(moduleName, name, msg string) { Logger.Info(moduleName, zap.String(name, msg)) }
func WarnString(moduleName, name, msg string) { Logger.Warn(moduleName, zap.String(name, msg)) }
func ErrorString(moduleName, name, msg string) { Logger.Error(moduleName, zap.String(name, msg)) }
func FatalString(moduleName, name, msg string) { Logger.Fatal(moduleName, zap.String(name, msg)) }
func DebugJSON(moduleName, name string, value interface{}) { Logger.Debug(moduleName, zap.String(name, jsonString(value))) }
func InfoJSON(moduleName, name string, value interface{}) { Logger.Info(moduleName, zap.String(name, jsonString(value))) }
func WarnJSON(moduleName, name string, value interface{}) { Logger.Warn(moduleName, zap.String(name, jsonString(value))) }
func ErrorJSON(moduleName, name string, value interface{}) { Logger.Error(moduleName, zap.String(name, jsonString(value))) }
func FatalJSON(moduleName, name string, value interface{}) { Logger.Fatal(moduleName, zap.String(name, jsonString(value))) }
func jsonString(value interface{}) string { b, err := json.Marshal(value) if err != nil { Logger.Error("Logger", zap.String("JSON marshal error", err.Error())) } return string(b) }
|