-
Notifications
You must be signed in to change notification settings - Fork 51
/
zap.go
158 lines (132 loc) 路 3.41 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
package zap
import (
"encoding/json"
"time"
"github.com/bricks-cloud/bricksllm/internal/logger"
"github.com/fatih/color"
"github.com/mattn/go-colorable"
"go.uber.org/zap"
"go.uber.org/zap/buffer"
"go.uber.org/zap/zapcore"
)
type prependEncoder struct {
// embed a zapcore encoder
// this makes prependEncoder implement the interface without extra work
zapcore.Encoder
cfg zapcore.EncoderConfig
// zap buffer pool
pool buffer.Pool
}
func (e *prependEncoder) Clone() zapcore.Encoder {
return &prependEncoder{
// cloning the encoder with the base config
Encoder: zapcore.NewConsoleEncoder(e.cfg),
pool: buffer.NewPool(),
cfg: e.cfg,
}
}
// implementing only EncodeEntry
func (e *prependEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) {
// new log buffer
buf := e.pool.Get()
blue := color.New(color.BgBlue)
red := color.New(color.BgRed)
coloredPrefix := blue.Sprint("[BRICKSLLM]")
if entry.Level != zap.InfoLevel {
coloredPrefix = red.Sprint("[BRICKSLLM]")
}
buf.AppendString(coloredPrefix)
buf.AppendString(" ")
buf.AppendString(e.toAtalasPrefix(entry.Level))
buf.AppendString(" | ")
buf.AppendString(time.Now().Format(time.RFC3339))
buf.AppendString(" | ")
// calling the embedded encoder's EncodeEntry to keep the original encoding format
consolebuf, err := e.Encoder.EncodeEntry(entry, fields)
if err != nil {
return nil, err
}
// just write the output into your own buffer
_, err = buf.Write(consolebuf.Bytes())
if err != nil {
return nil, err
}
return buf, nil
}
func (e *prependEncoder) toAtalasPrefix(lvl zapcore.Level) string {
switch lvl {
case zapcore.DebugLevel:
return "DEBUG"
case zapcore.InfoLevel:
return "INFO "
case zapcore.FatalLevel:
return "FATAL"
}
return ""
}
func NewLogger(mode string) logger.Logger {
rawJSON := []byte(`{
"level": "debug",
"encoding": "json",
"outputPaths": ["stdout"],
"errorOutputPaths": ["stderr"],
"encoderConfig": {
"messageKey": "message",
"levelKey": "level",
"levelEncoder": "lowercase"
}
}`)
var cfg zap.Config
if err := json.Unmarshal(rawJSON, &cfg); err != nil {
panic(err)
}
if mode == "production" {
cfg.Level = zap.NewAtomicLevelAt(zapcore.InfoLevel)
return zap.Must(cfg.Build()).Sugar()
}
cfg.EncoderConfig.LevelKey = zapcore.OmitKey
enc := &prependEncoder{
Encoder: zapcore.NewConsoleEncoder(cfg.EncoderConfig),
pool: buffer.NewPool(),
cfg: cfg.EncoderConfig,
}
zapLogger := zap.New(zapcore.NewCore(
enc,
zapcore.AddSync(colorable.NewColorableStdout()),
zapcore.DebugLevel,
))
return zapLogger.Sugar()
}
func NewZapLogger(mode string) *zap.Logger {
rawJSON := []byte(`{
"level": "debug",
"encoding": "json",
"outputPaths": ["stdout"],
"errorOutputPaths": ["stderr"],
"encoderConfig": {
"messageKey": "message",
"levelKey": "level",
"levelEncoder": "lowercase"
}
}`)
var cfg zap.Config
if err := json.Unmarshal(rawJSON, &cfg); err != nil {
panic(err)
}
if mode == "production" {
// cfg.Level = zap.NewAtomicLevelAt(zapcore.InfoLevel)
return zap.Must(cfg.Build())
}
cfg.EncoderConfig.LevelKey = zapcore.OmitKey
enc := &prependEncoder{
Encoder: zapcore.NewConsoleEncoder(cfg.EncoderConfig),
pool: buffer.NewPool(),
cfg: cfg.EncoderConfig,
}
zapLogger := zap.New(zapcore.NewCore(
enc,
zapcore.AddSync(colorable.NewColorableStdout()),
zapcore.DebugLevel,
))
return zapLogger
}