forked from pingcap/tidb
/
logging.go
342 lines (289 loc) · 10.2 KB
/
logging.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0.
package logutil
import (
"encoding/hex"
"encoding/json"
"fmt"
"strings"
"github.com/google/uuid"
"github.com/pingcap/errors"
backuppb "github.com/pingcap/kvproto/pkg/brpb"
"github.com/pingcap/kvproto/pkg/import_sstpb"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/log"
"github.com/twotigers93/tidb/br/pkg/redact"
"github.com/twotigers93/tidb/kv"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// AbbreviatedArrayMarshaler abbreviates an array of elements.
type AbbreviatedArrayMarshaler []string
// MarshalLogArray implements zapcore.ArrayMarshaler.
func (abb AbbreviatedArrayMarshaler) MarshalLogArray(encoder zapcore.ArrayEncoder) error {
if len(abb) <= 4 {
for _, e := range abb {
encoder.AppendString(e)
}
} else {
total := len(abb)
encoder.AppendString(abb[0])
encoder.AppendString(fmt.Sprintf("(skip %d)", total-2))
encoder.AppendString(abb[total-1])
}
return nil
}
// AbbreviatedArray constructs a field that abbreviates an array of elements.
func AbbreviatedArray(
key string, elements interface{}, marshalFunc func(interface{}) []string,
) zap.Field {
return zap.Array(key, AbbreviatedArrayMarshaler(marshalFunc(elements)))
}
type zapFileMarshaler struct{ *backuppb.File }
func (file zapFileMarshaler) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("name", file.GetName())
enc.AddString("CF", file.GetCf())
enc.AddString("sha256", hex.EncodeToString(file.GetSha256()))
enc.AddString("startKey", redact.Key(file.GetStartKey()))
enc.AddString("endKey", redact.Key(file.GetEndKey()))
enc.AddUint64("startVersion", file.GetStartVersion())
enc.AddUint64("endVersion", file.GetEndVersion())
enc.AddUint64("totalKvs", file.GetTotalKvs())
enc.AddUint64("totalBytes", file.GetTotalBytes())
enc.AddUint64("CRC64Xor", file.GetCrc64Xor())
return nil
}
type zapFilesMarshaler []*backuppb.File
// MarshalLogObjectForFiles is an internal util function to zap something having `Files` field.
func MarshalLogObjectForFiles(files []*backuppb.File, encoder zapcore.ObjectEncoder) error {
return zapFilesMarshaler(files).MarshalLogObject(encoder)
}
func (fs zapFilesMarshaler) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
total := len(fs)
encoder.AddInt("total", total)
elements := make([]string, 0, total)
for _, f := range fs {
elements = append(elements, f.GetName())
}
_ = encoder.AddArray("files", AbbreviatedArrayMarshaler(elements))
totalKVs := uint64(0)
totalBytes := uint64(0)
totalSize := uint64(0)
for _, file := range fs {
totalKVs += file.GetTotalKvs()
totalBytes += file.GetTotalBytes()
totalSize += file.GetSize_()
}
encoder.AddUint64("totalKVs", totalKVs)
encoder.AddUint64("totalBytes", totalBytes)
encoder.AddUint64("totalSize", totalSize)
return nil
}
// File make the zap fields for a file.
func File(file *backuppb.File) zap.Field {
return zap.Object("file", zapFileMarshaler{file})
}
// Files make the zap field for a set of file.
func Files(fs []*backuppb.File) zap.Field {
return zap.Object("files", zapFilesMarshaler(fs))
}
type zapStreamBackupTaskInfo struct{ *backuppb.StreamBackupTaskInfo }
func (t zapStreamBackupTaskInfo) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("taskName", t.Name)
enc.AddUint64("startTs", t.StartTs)
enc.AddUint64("endTS", t.EndTs)
enc.AddString("tableFilter", strings.Join(t.TableFilter, ","))
return nil
}
// StreamBackupTaskInfo makes the zap fields for a stream backup task info.
func StreamBackupTaskInfo(t *backuppb.StreamBackupTaskInfo) zap.Field {
return zap.Object("streamTaskInfo", zapStreamBackupTaskInfo{t})
}
type zapRewriteRuleMarshaler struct{ *import_sstpb.RewriteRule }
func (rewriteRule zapRewriteRuleMarshaler) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("oldKeyPrefix", hex.EncodeToString(rewriteRule.GetOldKeyPrefix()))
enc.AddString("newKeyPrefix", hex.EncodeToString(rewriteRule.GetNewKeyPrefix()))
enc.AddUint64("newTimestamp", rewriteRule.GetNewTimestamp())
return nil
}
// RewriteRule make the zap fields for a rewrite rule.
func RewriteRule(rewriteRule *import_sstpb.RewriteRule) zap.Field {
return zap.Object("rewriteRule", zapRewriteRuleMarshaler{rewriteRule})
}
// RewriteRuleObject make zap object marshaler for a rewrite rule.
func RewriteRuleObject(rewriteRule *import_sstpb.RewriteRule) zapcore.ObjectMarshaler {
return zapRewriteRuleMarshaler{rewriteRule}
}
type zapMarshalRegionMarshaler struct{ *metapb.Region }
func (region zapMarshalRegionMarshaler) MarshalLogObject(enc zapcore.ObjectEncoder) error {
peers := make([]string, 0, len(region.GetPeers()))
for _, peer := range region.GetPeers() {
peers = append(peers, peer.String())
}
enc.AddUint64("ID", region.GetId())
enc.AddString("startKey", redact.Key(region.GetStartKey()))
enc.AddString("endKey", redact.Key(region.GetEndKey()))
enc.AddString("epoch", region.GetRegionEpoch().String())
enc.AddString("peers", strings.Join(peers, ","))
return nil
}
// Region make the zap fields for a region.
func Region(region *metapb.Region) zap.Field {
return zap.Object("region", zapMarshalRegionMarshaler{region})
}
// RegionBy make the zap fields for a region with name.
func RegionBy(key string, region *metapb.Region) zap.Field {
return zap.Object(key, zapMarshalRegionMarshaler{region})
}
// Leader make the zap fields for a peer as leader.
// nolint:interfacer
func Leader(peer *metapb.Peer) zap.Field {
return zap.String("leader", peer.String())
}
// Peer make the zap fields for a peer.
func Peer(peer *metapb.Peer) zap.Field {
return zap.String("peer", peer.String())
}
type zapSSTMetaMarshaler struct{ *import_sstpb.SSTMeta }
func (sstMeta zapSSTMetaMarshaler) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("CF", sstMeta.GetCfName())
enc.AddBool("endKeyExclusive", sstMeta.EndKeyExclusive)
enc.AddUint32("CRC32", sstMeta.Crc32)
enc.AddUint64("length", sstMeta.Length)
enc.AddUint64("regionID", sstMeta.RegionId)
enc.AddString("regionEpoch", sstMeta.RegionEpoch.String())
enc.AddString("startKey", redact.Key(sstMeta.GetRange().GetStart()))
enc.AddString("endKey", redact.Key(sstMeta.GetRange().GetEnd()))
sstUUID, err := uuid.FromBytes(sstMeta.GetUuid())
if err != nil {
enc.AddString("UUID", fmt.Sprintf("invalid UUID %s", hex.EncodeToString(sstMeta.GetUuid())))
} else {
enc.AddString("UUID", sstUUID.String())
}
return nil
}
// SSTMeta make the zap fields for a SST meta.
func SSTMeta(sstMeta *import_sstpb.SSTMeta) zap.Field {
return zap.Object("sstMeta", zapSSTMetaMarshaler{sstMeta})
}
type zapSSTMetasMarshaler []*import_sstpb.SSTMeta
func (m zapSSTMetasMarshaler) MarshalLogArray(encoder zapcore.ArrayEncoder) error {
for _, meta := range m {
if err := encoder.AppendObject(zapSSTMetaMarshaler{meta}); err != nil {
return errors.Trace(err)
}
}
return nil
}
// SSTMetas make the zap fields for SST metas.
func SSTMetas(sstMetas []*import_sstpb.SSTMeta) zap.Field {
return zap.Array("sstMetas", zapSSTMetasMarshaler(sstMetas))
}
type zapKeysMarshaler [][]byte
func (keys zapKeysMarshaler) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
total := len(keys)
encoder.AddInt("total", total)
elements := make([]string, 0, total)
for _, k := range keys {
elements = append(elements, redact.Key(k))
}
_ = encoder.AddArray("keys", AbbreviatedArrayMarshaler(elements))
return nil
}
// Key constructs a field that carries upper hex format key.
func Key(fieldKey string, key []byte) zap.Field {
return zap.String(fieldKey, redact.Key(key))
}
// Keys constructs a field that carries upper hex format keys.
func Keys(keys [][]byte) zap.Field {
return zap.Object("keys", zapKeysMarshaler(keys))
}
// AShortError make the zap field with key to display error without verbose representation (e.g. the stack trace).
func AShortError(key string, err error) zap.Field {
if err == nil {
return zap.Skip()
}
return zap.String(key, err.Error())
}
// ShortError make the zap field to display error without verbose representation (e.g. the stack trace).
func ShortError(err error) zap.Field {
if err == nil {
return zap.Skip()
}
return zap.String("error", err.Error())
}
var loggerToTerm, _, _ = log.InitLogger(new(log.Config), zap.AddCallerSkip(1))
// WarnTerm put a log both to terminal and to the log file.
func WarnTerm(message string, fields ...zap.Field) {
log.Warn(message, fields...)
if loggerToTerm != nil {
loggerToTerm.Warn(message, fields...)
}
}
// RedactAny constructs a redacted field that carries an interface{}.
func RedactAny(fieldKey string, key interface{}) zap.Field {
if redact.NeedRedact() {
return zap.String(fieldKey, "?")
}
return zap.Any(fieldKey, key)
}
// Redact replaces the zap field by a '?' if redaction is turned on.
func Redact(field zap.Field) zap.Field {
if redact.NeedRedact() {
return zap.String(field.Key, "?")
}
return field
}
// StringifyKeys wraps the key range into a stringer.
type StringifyKeys []kv.KeyRange
func (kr StringifyKeys) String() string {
sb := new(strings.Builder)
sb.WriteString("{")
for i, rng := range kr {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(StringifyRange(rng).String())
}
sb.WriteString("}")
return sb.String()
}
// StringifyRange is the wrapper for displaying a key range.
type StringifyRange kv.KeyRange
func (rng StringifyRange) String() string {
sb := new(strings.Builder)
sb.WriteString("[")
sb.WriteString(redact.Key(rng.StartKey))
sb.WriteString(", ")
var endKey string
if len(rng.EndKey) == 0 {
endKey = "inf"
} else {
endKey = redact.Key(rng.EndKey)
}
sb.WriteString(redact.String(endKey))
sb.WriteString(")")
return sb.String()
}
// StringifyMany returns an array marshaler for a slice of stringers.
func StringifyMany[T fmt.Stringer](items []T) zapcore.ArrayMarshaler {
return zapcore.ArrayMarshalerFunc(func(ae zapcore.ArrayEncoder) error {
for _, item := range items {
ae.AppendString(item.String())
}
return nil
})
}
// HexBytes is a wrapper which make a byte sequence printed by the hex format.
type HexBytes []byte
var (
_ fmt.Stringer = HexBytes{}
_ json.Marshaler = HexBytes{}
)
// String implements fmt.Stringer.
func (b HexBytes) String() string {
return hex.EncodeToString(b)
}
// MarshalJSON implements json.Marshaler.
func (b HexBytes) MarshalJSON() ([]byte, error) {
return json.Marshal(hex.EncodeToString(b))
}