forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.go
60 lines (50 loc) · 1.46 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
// Copyright 2021 PingCAP, Inc. Licensed under Apache-2.0.
package rtree
import (
"fmt"
"github.com/pingcap/tidb/br/pkg/logutil"
"github.com/pingcap/tidb/br/pkg/redact"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// String formats a range to a string.
func (rg Range) String() string {
return fmt.Sprintf("[%s, %s)", redact.Key(rg.StartKey), redact.Key(rg.EndKey))
}
// ZapRanges make zap fields for logging Range slice.
func ZapRanges(ranges []Range) zapcore.Field {
return zap.Object("ranges", rangesMarshaler(ranges))
}
type rangesMarshaler []Range
func (rs rangesMarshaler) MarshalLogArray(encoder zapcore.ArrayEncoder) error {
for _, r := range rs {
encoder.AppendString(r.String())
}
return nil
}
func (rs rangesMarshaler) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
total := len(rs)
encoder.AddInt("total", total)
elements := make([]string, 0, total)
for _, r := range rs {
elements = append(elements, r.String())
}
_ = encoder.AddArray("ranges", logutil.AbbreviatedArrayMarshaler(elements))
totalKV := uint64(0)
totalBytes := uint64(0)
totalSize := uint64(0)
totalFile := 0
for _, r := range rs {
for _, f := range r.Files {
totalKV += f.GetTotalKvs()
totalBytes += f.GetTotalBytes()
totalSize += f.GetSize_()
}
totalFile += len(r.Files)
}
encoder.AddInt("totalFiles", totalFile)
encoder.AddUint64("totalKVs", totalKV)
encoder.AddUint64("totalBytes", totalBytes)
encoder.AddUint64("totalSize", totalBytes)
return nil
}