()
| 14 | var Logger = zap.NewNop() |
| 15 | |
| 16 | // Configure replaces Logger with a stdout logger and, when Path is non-empty, |
| 17 | // a second rolling JSON log destination. |
| 18 | func Configure(cfg config.LogConfig) error { |
| 19 | if err := cfg.Validate(); err != nil { |
| 20 | return err |
| 21 | } |
| 22 | |
| 23 | level := levelMap[cfg.Level] |
| 24 | encoderConfig := zapcore.EncoderConfig{ |
| 25 | TimeKey: "ts", |
| 26 | LevelKey: "level", |
| 27 | NameKey: "logger", |
| 28 | CallerKey: "caller", |
| 29 | MessageKey: "msg", |
| 30 | StacktraceKey: "stacktrace", |
| 31 | LineEnding: zapcore.DefaultLineEnding, |
| 32 | EncodeLevel: zapcore.LowercaseLevelEncoder, |
| 33 | EncodeTime: TimeEncoder, |
| 34 | EncodeDuration: zapcore.SecondsDurationEncoder, |
| 35 | EncodeCaller: zapcore.ShortCallerEncoder, |
| 36 | } |
| 37 | encoder := zapcore.NewJSONEncoder(encoderConfig) |
| 38 | enabler := zap.LevelEnablerFunc(func(candidate zapcore.Level) bool { |
| 39 | return candidate >= level |
| 40 | }) |
| 41 | |
| 42 | cores := []zapcore.Core{ |
| 43 | zapcore.NewCore(encoder, zapcore.Lock(os.Stdout), enabler), |
| 44 | } |
| 45 | if cfg.Path != "" { |
| 46 | hook := &lumberjack.Logger{ |
| 47 | Filename: cfg.Path, |
| 48 | MaxSize: 100, |
| 49 | MaxBackups: 5, |
| 50 | MaxAge: 30, |
| 51 | Compress: true, |
| 52 | } |
| 53 | cores = append(cores, zapcore.NewCore(encoder, zapcore.AddSync(hook), enabler)) |
| 54 | } |
| 55 | |
| 56 | Logger = zap.New(zapcore.NewTee(cores...), zap.AddCaller()) |
| 57 | return nil |
| 58 | } |
| 59 | |
| 60 | var levelMap = map[string]zapcore.Level{ |
| 61 | "debug": zapcore.DebugLevel, |
| 62 | "info": zapcore.InfoLevel, |
| 63 | "warn": zapcore.WarnLevel, |
| 64 | "error": zapcore.ErrorLevel, |
| 65 | "dpanic": zapcore.DPanicLevel, |
| 66 | "panic": zapcore.PanicLevel, |
| 67 | "fatal": zapcore.FatalLevel, |
nothing calls this directly
no outgoing calls
no test coverage detected