|
| 1 | +package log |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/natefinch/lumberjack" |
| 9 | + "go.uber.org/zap" |
| 10 | + "go.uber.org/zap/zapcore" |
| 11 | +) |
| 12 | + |
| 13 | +type Level func(zapcore.Level) bool |
| 14 | + |
| 15 | +type Logs map[string]Level |
| 16 | + |
| 17 | +// LoggerParam |
| 18 | +type LoggerParam struct { |
| 19 | + Path string `json:"path" toml:"path" yaml:"path" xml:"path"` // log file path |
| 20 | + MaxSize int `json:"maxSize" toml:"maxSize" yaml:"maxSize" xml:"maxSize"` // log file max size |
| 21 | + MaxBackups int `json:"maxBackups" toml:"maxBackups" yaml:"maxBackups" xml:"maxBackups"` // log file max backups |
| 22 | + MaxAge int `json:"maxAge" toml:"maxAge" yaml:"maxAge" xml:"maxAge"` // log file max save day |
| 23 | + Compress bool `json:"compress" toml:"compress" yaml:"compress" xml:"compress"` // log file whether to compress |
| 24 | +} |
| 25 | + |
| 26 | +// DebugLevel method is Logs map value |
| 27 | +func DebugLevel(level zapcore.Level) bool { |
| 28 | + return level == zap.DebugLevel |
| 29 | +} |
| 30 | + |
| 31 | +// InfoLevel method is Logs map value |
| 32 | +func InfoLevel(level zapcore.Level) bool { |
| 33 | + return level == zap.InfoLevel |
| 34 | +} |
| 35 | + |
| 36 | +// WarnLevel method is Logs map value |
| 37 | +func WarnLevel(level zapcore.Level) bool { |
| 38 | + return level == zap.WarnLevel |
| 39 | +} |
| 40 | + |
| 41 | +// ErrorLevel method is Logs map value |
| 42 | +func ErrorLevel(level zapcore.Level) bool { |
| 43 | + return level == zap.ErrorLevel |
| 44 | +} |
| 45 | + |
| 46 | +// DPanicLevel method is Logs map value |
| 47 | +func DPanicLevel(level zapcore.Level) bool { |
| 48 | + return level == zap.DPanicLevel |
| 49 | +} |
| 50 | + |
| 51 | +// PanicLevel method is Logs map value |
| 52 | +func PanicLevel(level zapcore.Level) bool { |
| 53 | + return level == zap.PanicLevel |
| 54 | +} |
| 55 | + |
| 56 | +// FatalLevel method is Logs map value |
| 57 | +func FatalLevel(level zapcore.Level) bool { |
| 58 | + return level == zap.FatalLevel |
| 59 | +} |
| 60 | + |
| 61 | +// Default |
| 62 | +func (log *LoggerParam) Default() *zap.SugaredLogger { |
| 63 | + logLevels := Logs{ |
| 64 | + "debug": DebugLevel, |
| 65 | + "info": InfoLevel, |
| 66 | + "warn": WarnLevel, |
| 67 | + "error": ErrorLevel, |
| 68 | + "dpanic": DPanicLevel, |
| 69 | + "panic": PanicLevel, |
| 70 | + "fatal": FatalLevel, |
| 71 | + } |
| 72 | + return log.New(logLevels) |
| 73 | +} |
| 74 | + |
| 75 | +// New |
| 76 | +func (log *LoggerParam) New(logs Logs) *zap.SugaredLogger { |
| 77 | + encoderConfig := zapcore.EncoderConfig{ |
| 78 | + TimeKey: "time", |
| 79 | + LevelKey: "level", |
| 80 | + NameKey: "log", |
| 81 | + CallerKey: "lineNum", |
| 82 | + MessageKey: "msg", |
| 83 | + StacktraceKey: "stacktrace", |
| 84 | + LineEnding: zapcore.DefaultLineEnding, |
| 85 | + EncodeLevel: zapcore.LowercaseLevelEncoder, |
| 86 | + EncodeTime: func(t time.Time, pae zapcore.PrimitiveArrayEncoder) { |
| 87 | + pae.AppendString(t.Format("[2006-01-02 15:04:05]")) |
| 88 | + }, |
| 89 | + EncodeDuration: zapcore.SecondsDurationEncoder, |
| 90 | + EncodeCaller: zapcore.FullCallerEncoder, |
| 91 | + EncodeName: zapcore.FullNameEncoder, |
| 92 | + } |
| 93 | + |
| 94 | + cores := make([]zapcore.Core, 0) |
| 95 | + |
| 96 | + for fileName := range logs { |
| 97 | + if fileName == "" { |
| 98 | + continue |
| 99 | + } |
| 100 | + // logger write |
| 101 | + write := &lumberjack.Logger{ |
| 102 | + Filename: getLogFilePath(log.Path, fileName), |
| 103 | + MaxSize: log.MaxSize, |
| 104 | + MaxBackups: log.MaxBackups, |
| 105 | + MaxAge: log.MaxAge, |
| 106 | + Compress: log.Compress, |
| 107 | + } |
| 108 | + // the log level |
| 109 | + level := zap.LevelEnablerFunc(logs[fileName]) |
| 110 | + // log core |
| 111 | + core := zapcore.NewCore(zapcore.NewConsoleEncoder(encoderConfig), zapcore.NewMultiWriteSyncer(zapcore.AddSync(write)), level) |
| 112 | + cores = append(cores, core) |
| 113 | + } |
| 114 | + // append default info log level |
| 115 | + cores = append(cores, zapcore.NewCore(zapcore.NewConsoleEncoder(encoderConfig), zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout)), zap.InfoLevel)) |
| 116 | + |
| 117 | + core := zapcore.NewTee(cores...) |
| 118 | + caller := zap.AddCaller() |
| 119 | + development := zap.Development() |
| 120 | + |
| 121 | + return zap.New(core, caller, development, zap.Fields()).Sugar() |
| 122 | +} |
| 123 | + |
| 124 | +// logFile logger file out path |
| 125 | +func getLogFilePath(filePath, fileName string) string { |
| 126 | + filePath = strings.ReplaceAll(filePath, "\\", "/") |
| 127 | + fp := strings.Split(filePath, "/") |
| 128 | + realPath := make([]string, 0) |
| 129 | + for i := range fp { |
| 130 | + if fp[i] != "" { |
| 131 | + realPath = append(realPath, fp[i]) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + filePath = strings.Join(realPath, "/") |
| 136 | + if filePath == "" { |
| 137 | + filePath = "." |
| 138 | + } |
| 139 | + |
| 140 | + if !strings.HasSuffix(fileName, ".log") { |
| 141 | + fileName = strings.ReplaceAll(fileName, ".", "_") |
| 142 | + fileName = fileName + ".log" |
| 143 | + } |
| 144 | + return filePath + "/" + fileName |
| 145 | +} |
0 commit comments