/
zap.go
165 lines (140 loc) · 4.09 KB
/
zap.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
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
package logger
// thx zap sugar
import (
"fmt"
"time"
"github.com/exuan/waka-api/internal/config"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"go.uber.org/multierr"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
_oddNumberErrMsg = "Ignored key without a value."
_nonStringKeyErrMsg = "Ignored key-value pairs with non-string keys."
)
type (
Config struct {
Level zapcore.Level
Name string
Format string
}
Sugar struct {
base *zap.Logger
}
)
func New(cfg *config.Config, configs ...*Config) (*Sugar, error) {
cores := make([]zapcore.Core, 0)
encoder := zapcore.EncoderConfig{
TimeKey: "time",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05"))
},
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
for _, c := range configs {
conf := c
link := fmt.Sprintf("%s%s.%s.log", cfg.GetString("logPath"), cfg.SrvName, conf.Name)
path := fmt.Sprintf("%s%s.%s.%s.log", cfg.GetString("logPath"), cfg.SrvName, conf.Name, conf.Format)
rl, err := rotatelogs.New(path, rotatelogs.WithLinkName(link))
if err != nil {
return nil, err
}
cores = append(cores, zapcore.NewCore(
zapcore.NewJSONEncoder(encoder),
zapcore.AddSync(rl),
zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
return lev == conf.Level
}),
))
}
// like zl.Sugar()
zl := zap.New(zapcore.NewTee(cores...)).WithOptions(zap.AddCallerSkip(2))
return &Sugar{zl}, nil
}
func (s *Sugar) Write(lvl zapcore.Level, msg string, keysAndValues ...interface{}) {
s.log(lvl, msg, nil, keysAndValues)
}
func (s *Sugar) Sync() error {
return s.base.Sync()
}
func (s *Sugar) log(lvl zapcore.Level, template string, fmtArgs []interface{}, context []interface{}) {
s.base.WithOptions()
if lvl < zap.DPanicLevel && !s.base.Core().Enabled(lvl) {
return
}
// Format with Sprint, Sprintf, or neither.
msg := template
if msg == "" && len(fmtArgs) > 0 {
msg = fmt.Sprint(fmtArgs...)
} else if msg != "" && len(fmtArgs) > 0 {
msg = fmt.Sprintf(template, fmtArgs...)
}
if ce := s.base.Check(lvl, msg); ce != nil {
ce.Write(s.sweetenFields(context)...)
}
}
func (s *Sugar) sweetenFields(args []interface{}) []zap.Field {
if len(args) == 0 {
return nil
}
// Allocate enough space for the worst case; if users pass only structured
// fields, we shouldn't penalize them with extra allocations.
fields := make([]zap.Field, 0, len(args))
var invalid invalidPairs
for i := 0; i < len(args); {
// This is a strongly-typed field. Consume it and move on.
if f, ok := args[i].(zap.Field); ok {
fields = append(fields, f)
i++
continue
}
// Make sure this element isn't a dangling key.
if i == len(args)-1 {
s.base.DPanic(_oddNumberErrMsg, zap.Any("ignored", args[i]))
break
}
// Consume this value and the next, treating them as a key-value pair. If the
// key isn't a string, add this pair to the slice of invalid pairs.
key, val := args[i], args[i+1]
if keyStr, ok := key.(string); !ok {
// Subsequent errors are likely, so allocate once up front.
if cap(invalid) == 0 {
invalid = make(invalidPairs, 0, len(args)/2)
}
invalid = append(invalid, invalidPair{i, key, val})
} else {
fields = append(fields, zap.Any(keyStr, val))
}
i += 2
}
// If we encountered any invalid key-value pairs, log an error.
if len(invalid) > 0 {
s.base.DPanic(_nonStringKeyErrMsg, zap.Array("invalid", invalid))
}
return fields
}
type invalidPair struct {
position int
key, value interface{}
}
func (p invalidPair) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddInt64("position", int64(p.position))
zap.Any("key", p.key).AddTo(enc)
zap.Any("value", p.value).AddTo(enc)
return nil
}
type invalidPairs []invalidPair
func (ps invalidPairs) MarshalLogArray(enc zapcore.ArrayEncoder) error {
var err error
for i := range ps {
err = multierr.Append(err, enc.AppendObject(ps[i]))
}
return err
}