-
Notifications
You must be signed in to change notification settings - Fork 0
/
conditional-log.go
84 lines (71 loc) · 3.3 KB
/
conditional-log.go
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
package zc
import (
"context"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func logLevel(level zapcore.Level, logger *zap.Logger, msg string, fields ...zap.Field) {
const logLevelSkipCallers = 3
ce := logger.WithOptions(zap.AddCallerSkip(logLevelSkipCallers)).Check(level, msg)
if ce != nil {
ce.Write(fields...)
}
}
func logConditional(levelOK, levelError zapcore.Level, logger *zap.Logger, err error, msg string, fields ...zap.Field) {
var level zapcore.Level
if err == nil {
level = levelOK
} else {
level = levelError
fields = append([]zap.Field{zap.Error(err)}, fields...)
}
logLevel(level, logger, msg, fields...)
}
func DebugDPanic(logger *zap.Logger, err error, msg string, fields ...zap.Field) {
logConditional(zapcore.DebugLevel, zapcore.DPanicLevel, logger, err, msg, fields...)
}
func DebugDPanicCtx(ctx context.Context, err error, msg string, fields ...zap.Field) {
logConditional(zapcore.DebugLevel, zapcore.DPanicLevel, L(ctx), err, msg, fields...)
}
func DebugError(logger *zap.Logger, err error, msg string, fields ...zap.Field) {
logConditional(zapcore.DebugLevel, zapcore.ErrorLevel, logger, err, msg, fields...)
}
func DebugErrorCtx(ctx context.Context, err error, msg string, fields ...zap.Field) {
logConditional(zapcore.DebugLevel, zapcore.ErrorLevel, L(ctx), err, msg, fields...)
}
func DebugFatal(logger *zap.Logger, err error, msg string, fields ...zap.Field) {
logConditional(zapcore.DebugLevel, zapcore.FatalLevel, logger, err, msg, fields...)
}
func DebugFatalCtx(ctx context.Context, err error, msg string, fields ...zap.Field) {
logConditional(zapcore.DebugLevel, zapcore.FatalLevel, L(ctx), err, msg, fields...)
}
func DebugInfo(logger *zap.Logger, err error, msg string, fields ...zap.Field) {
logConditional(zapcore.DebugLevel, zapcore.InfoLevel, logger, err, msg, fields...)
}
func DebugInfoCtx(ctx context.Context, err error, msg string, fields ...zap.Field) {
logConditional(zapcore.DebugLevel, zapcore.InfoLevel, L(ctx), err, msg, fields...)
}
func InfoDPanic(logger *zap.Logger, err error, msg string, fields ...zap.Field) {
logConditional(zapcore.InfoLevel, zapcore.DPanicLevel, logger, err, msg, fields...)
}
func InfoDPanicCtx(ctx context.Context, err error, msg string, fields ...zap.Field) {
logConditional(zapcore.InfoLevel, zapcore.DPanicLevel, L(ctx), err, msg, fields...)
}
func InfoError(logger *zap.Logger, err error, msg string, fields ...zap.Field) {
logConditional(zapcore.InfoLevel, zapcore.ErrorLevel, logger, err, msg, fields...)
}
func InfoErrorCtx(ctx context.Context, err error, msg string, fields ...zap.Field) {
logConditional(zapcore.InfoLevel, zapcore.ErrorLevel, L(ctx), err, msg, fields...)
}
func InfoFatal(logger *zap.Logger, err error, msg string, fields ...zap.Field) {
logConditional(zapcore.InfoLevel, zapcore.FatalLevel, logger, err, msg, fields...)
}
func InfoFatalCtx(ctx context.Context, err error, msg string, fields ...zap.Field) {
logConditional(zapcore.InfoLevel, zapcore.FatalLevel, L(ctx), err, msg, fields...)
}
func InfoPanic(logger *zap.Logger, err error, msg string, fields ...zap.Field) {
logConditional(zapcore.InfoLevel, zapcore.PanicLevel, logger, err, msg, fields...)
}
func InfoPanicCtx(ctx context.Context, err error, msg string, fields ...zap.Field) {
logConditional(zapcore.InfoLevel, zapcore.PanicLevel, L(ctx), err, msg, fields...)
}