@@ -26,6 +26,53 @@ const (
26
26
FatalLevel
27
27
)
28
28
29
+ // MarshalText implements [encoding.TextMarshaler] for Level.
30
+ func (l Level ) MarshalText () ([]byte , error ) {
31
+ return []byte (l .String ()), nil
32
+ }
33
+
34
+ // String implements [fmt.Stringer] for Level.
35
+ func (l Level ) String () string {
36
+ switch l {
37
+ case DebugLevel :
38
+ return "debug"
39
+ case InfoLevel :
40
+ return "info"
41
+ case WarnLevel :
42
+ return "warn"
43
+ case ErrorLevel :
44
+ return "error"
45
+ case FatalLevel :
46
+ return "fatal"
47
+ default :
48
+ return fmt .Sprintf ("Level(%d)" , l )
49
+ }
50
+ }
51
+
52
+ func (l Level ) toZapCoreLevel () zapcore.Level {
53
+ return zapcore .Level (l )
54
+ }
55
+
56
+ // UnmarshalText implements [encoding.TextUnmarshaler] for Level.
57
+ func (l * Level ) UnmarshalText (text []byte ) error {
58
+ switch lit := strings .ToLower (string (text )); lit {
59
+ default :
60
+ return fmt .Errorf ("invalid level: %q" , lit )
61
+ case "debug" :
62
+ * l = DebugLevel
63
+ case "info" , "" :
64
+ * l = InfoLevel
65
+ case "warn" :
66
+ * l = WarnLevel
67
+ case "error" :
68
+ * l = ErrorLevel
69
+ case "fatal" :
70
+ * l = FatalLevel
71
+ }
72
+
73
+ return nil
74
+ }
75
+
29
76
// DefaultTraceHeader is the default header used as a trace id.
30
77
const DefaultTraceHeader = "Traceparent"
31
78
@@ -97,16 +144,18 @@ func New(name string, opts ...Option) (*Logger, error) {
97
144
return nil , errors .Errorf ("unsupported logger.format '%s'" , o .Format )
98
145
}
99
146
147
+ minLogLevel := o .Level .toZapCoreLevel ()
148
+
100
149
// Logs info and debug to stdout
101
150
outWriter := zapcore .Lock (os .Stdout )
102
151
outLevel := zap .LevelEnablerFunc (func (lvl zapcore.Level ) bool {
103
- return lvl >= o . Level && lvl < zapcore .WarnLevel
152
+ return lvl >= minLogLevel && lvl < zapcore .WarnLevel
104
153
})
105
154
106
155
// Logs warning and errors to stderr
107
156
errWriter := zapcore .Lock (os .Stderr )
108
157
errLevel := zap .LevelEnablerFunc (func (lvl zapcore.Level ) bool {
109
- return lvl >= o . Level && lvl >= zapcore .WarnLevel
158
+ return lvl >= minLogLevel && lvl >= zapcore .WarnLevel
110
159
})
111
160
112
161
// Create zap.Logger
0 commit comments