This repository has been archived by the owner on Nov 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
56 lines (49 loc) · 1.75 KB
/
config.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
package log
import (
"strconv"
"strings"
"time"
"github.com/fatih/color"
"go.uber.org/zap/zapcore"
)
// EpochTimeEncoder serializes a time.Time to an integer representing the
// number of seconds since the Unix epoch.
func EpochTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
sec := t.Unix()
enc.AppendInt64(sec)
}
// ColorEpochTimeEncoder serializes a time.Time to an integer representing the
// number of seconds since the Unix epoch and adds color.
func ColorEpochTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
sec := t.Unix()
enc.AppendString(color.HiWhiteString(strconv.FormatInt(sec, 10)))
}
// CapitalFullNameEncoder serializes the logger name to an all-caps string. For
// example, gossip is serialized to "GOSSIP".
func CapitalFullNameEncoder(loggerName string, enc zapcore.PrimitiveArrayEncoder) {
loggerName = strings.ToUpper(loggerName)
enc.AppendString(loggerName)
}
// CapitalColorFullNameEncoder serializes the logger name to an all-caps string
// and adds color. For example, gossip is serialized to "GOSSIP" and colored
// white.
func CapitalColorFullNameEncoder(loggerName string, enc zapcore.PrimitiveArrayEncoder) {
loggerName = color.HiWhiteString(strings.ToUpper(loggerName))
enc.AppendString(loggerName)
}
func NewDefaultEncoderConfig() zapcore.EncoderConfig {
return zapcore.EncoderConfig{
TimeKey: "T",
LevelKey: "L",
NameKey: "N",
CallerKey: "C",
MessageKey: "M",
StacktraceKey: "S",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalColorLevelEncoder,
EncodeTime: ColorEpochTimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
EncodeName: CapitalColorFullNameEncoder,
}
}