-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathencoder.go
33 lines (28 loc) · 971 Bytes
/
encoder.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
package logger
import (
"strings"
"github.com/fatih/color"
"go.uber.org/zap/zapcore"
)
// ColoredLevelEncoder colorizes log levels.
func ColoredLevelEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
switch level {
case zapcore.DebugLevel:
enc.AppendString(color.HiWhiteString(level.CapitalString()))
case zapcore.InfoLevel:
enc.AppendString(color.HiCyanString(level.CapitalString()))
case zapcore.WarnLevel:
enc.AppendString(color.HiYellowString(level.CapitalString()))
case zapcore.ErrorLevel, zapcore.DPanicLevel:
enc.AppendString(color.HiRedString(level.CapitalString()))
case zapcore.PanicLevel, zapcore.FatalLevel, zapcore.InvalidLevel:
enc.AppendString(color.HiMagentaString(level.CapitalString()))
}
}
// ColoredNameEncoder colorizes service names.
func ColoredNameEncoder(s string, enc zapcore.PrimitiveArrayEncoder) {
if len(s) < 12 {
s += strings.Repeat(" ", 12-len(s))
}
enc.AppendString(color.HiGreenString(s))
}