forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
format.go
133 lines (114 loc) · 3 KB
/
format.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package logging
import (
"errors"
"fmt"
"strings"
"go.uber.org/zap/zapcore"
"golang.org/x/term"
)
// Format modes available
const (
Plain Format = iota
Colors
JSON
termTimeFormat = "[01-02|15:04:05.000]"
)
var (
errUnknownFormat = errors.New("unknown format")
defaultEncoderConfig = zapcore.EncoderConfig{
TimeKey: "timestamp",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "msg",
StacktraceKey: "stacktrace",
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
jsonEncoderConfig zapcore.EncoderConfig
termTimeEncoder = zapcore.TimeEncoderOfLayout(termTimeFormat)
)
func init() {
jsonEncoderConfig = defaultEncoderConfig
jsonEncoderConfig.EncodeLevel = jsonLevelEncoder
jsonEncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
jsonEncoderConfig.EncodeDuration = zapcore.NanosDurationEncoder
}
// Highlight mode to apply to displayed logs
type Format int
// ToFormat chooses a highlighting mode
func ToFormat(h string, fd uintptr) (Format, error) {
switch strings.ToUpper(h) {
case "PLAIN":
return Plain, nil
case "COLORS":
return Colors, nil
case "JSON":
return JSON, nil
case "AUTO":
if !term.IsTerminal(int(fd)) {
return Plain, nil
}
return Colors, nil
default:
return Plain, fmt.Errorf("unknown format mode: %s", h)
}
}
func (f Format) MarshalJSON() ([]byte, error) {
switch f {
case Plain:
return []byte(`"PLAIN"`), nil
case Colors:
return []byte(`"COLORS"`), nil
case JSON:
return []byte(`"JSON"`), nil
default:
return nil, errUnknownFormat
}
}
func (f Format) WrapPrefix(prefix string) string {
if prefix == "" || f == JSON {
return prefix
}
return fmt.Sprintf("<%s>", prefix)
}
func (f Format) ConsoleEncoder() zapcore.Encoder {
switch f {
case Colors:
return zapcore.NewConsoleEncoder(newTermEncoderConfig(consoleColorLevelEncoder))
case JSON:
return zapcore.NewJSONEncoder(jsonEncoderConfig)
default:
return zapcore.NewConsoleEncoder(newTermEncoderConfig(levelEncoder))
}
}
func (f Format) FileEncoder() zapcore.Encoder {
switch f {
case JSON:
return zapcore.NewJSONEncoder(jsonEncoderConfig)
default:
return zapcore.NewConsoleEncoder(newTermEncoderConfig(levelEncoder))
}
}
func newTermEncoderConfig(lvlEncoder zapcore.LevelEncoder) zapcore.EncoderConfig {
config := defaultEncoderConfig
config.EncodeLevel = lvlEncoder
config.EncodeTime = termTimeEncoder
config.ConsoleSeparator = " "
return config
}
func levelEncoder(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(Level(l).String())
}
func jsonLevelEncoder(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(Level(l).LowerString())
}
func consoleColorLevelEncoder(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
s, ok := levelToCapitalColorString[Level(l)]
if !ok {
s = unknownLevelColor.Wrap(l.String())
}
enc.AppendString(s)
}