-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsqd.go
97 lines (76 loc) · 1.66 KB
/
nsqd.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
package log
import (
"strings"
"github.com/rs/zerolog"
)
type NSQLogger struct {
logger zerolog.Logger
}
func NewNSQLogger(logLevel string) NSQLogger {
l := logger.With().CallerWithSkipFrameCount(4).Logger() //nolint:gomnd // Frame count to set correctly NSQ log line
lvl, _ := zerolog.ParseLevel(logLevel)
l.Level(lvl)
return NSQLogger{logger: l}
}
func (nl NSQLogger) Output(calldepth int, s string) error {
output := strings.SplitN(s, " ", 2)
if len(output) <= 1 {
nl.defaultOutput(s)
return nil
}
logLvl := parseNSQLogLvl(output[0])
if logLvl == zerolog.NoLevel {
nl.defaultOutput(s)
return nil
}
nl.logger.WithLevel(logLvl).Msg(strings.TrimSpace(output[1]))
return nil
}
func (nl NSQLogger) defaultOutput(s string) {
nl.logger.Log().Msg(s)
}
func parseNSQLogLvl(s string) zerolog.Level {
var lvl zerolog.Level
switch s {
case "TRC":
lvl = zerolog.TraceLevel
case "DBG":
lvl = zerolog.DebugLevel
case "INF":
lvl = zerolog.InfoLevel
case "WRN":
lvl = zerolog.WarnLevel
case "ERR":
lvl = zerolog.ErrorLevel
case "FTL":
lvl = zerolog.FatalLevel
case "PNC":
lvl = zerolog.PanicLevel
default:
lvl = zerolog.NoLevel
}
return lvl
}
func (nl NSQLogger) LogLevel() int {
return nsqLogLvlFromZerologLogLvl(nl.logger.GetLevel())
}
func nsqLogLvlFromZerologLogLvl(level zerolog.Level) int {
var nsqlLL int
switch level {
case zerolog.TraceLevel:
case zerolog.DebugLevel:
nsqlLL = 0
case zerolog.InfoLevel:
nsqlLL = 1
case zerolog.WarnLevel:
nsqlLL = 2
case zerolog.ErrorLevel:
case zerolog.FatalLevel:
case zerolog.PanicLevel:
nsqlLL = 3
case zerolog.NoLevel:
case zerolog.Disabled:
nsqlLL = 4
}
return nsqlLL
}