File tree Expand file tree Collapse file tree 3 files changed +28
-2
lines changed Expand file tree Collapse file tree 3 files changed +28
-2
lines changed Original file line number Diff line number Diff line change @@ -231,9 +231,9 @@ func Setup(ctx *cli.Context) error {
231
231
case ctx .Bool (logjsonFlag .Name ):
232
232
// Retain backwards compatibility with `--log.json` flag if `--log.format` not set
233
233
defer log .Warn ("The flag '--log.json' is deprecated, please use '--log.format=json' instead" )
234
- handler = log .JSONHandler (output )
234
+ handler = log .JSONHandlerWithLevel (output , log . LevelInfo )
235
235
case logFmtFlag == "json" :
236
- handler = log .JSONHandler (output )
236
+ handler = log .JSONHandlerWithLevel (output , log . LevelInfo )
237
237
case logFmtFlag == "logfmt" :
238
238
handler = log .LogfmtHandler (output )
239
239
case logFmtFlag == "" , logFmtFlag == "terminal" :
Original file line number Diff line number Diff line change @@ -115,8 +115,15 @@ func (l *leveler) Level() slog.Level {
115
115
116
116
// JSONHandler returns a handler which prints records in JSON format.
117
117
func JSONHandler (wr io.Writer ) slog.Handler {
118
+ return JSONHandlerWithLevel (wr , levelMaxVerbosity )
119
+ }
120
+
121
+ // JSONHandler returns a handler which prints records in JSON format that are less than or equal to
122
+ // the specified verbosity level.
123
+ func JSONHandlerWithLevel (wr io.Writer , level slog.Level ) slog.Handler {
118
124
return slog .NewJSONHandler (wr , & slog.HandlerOptions {
119
125
ReplaceAttr : builtinReplaceJSON ,
126
+ Level : & leveler {level },
120
127
})
121
128
}
122
129
Original file line number Diff line number Diff line change @@ -50,6 +50,25 @@ func TestTerminalHandlerWithAttrs(t *testing.T) {
50
50
}
51
51
}
52
52
53
+ // Make sure the default json handler outputs debug log lines
54
+ func TestJSONHandler (t * testing.T ) {
55
+ out := new (bytes.Buffer )
56
+ handler := JSONHandler (out )
57
+ logger := slog .New (handler )
58
+ logger .Debug ("hi there" )
59
+ if len (out .String ()) == 0 {
60
+ t .Error ("expected non-empty debug log output from default JSON Handler" )
61
+ }
62
+
63
+ out .Reset ()
64
+ handler = JSONHandlerWithLevel (out , slog .LevelInfo )
65
+ logger = slog .New (handler )
66
+ logger .Debug ("hi there" )
67
+ if len (out .String ()) != 0 {
68
+ t .Errorf ("expected empty debug log output, but got: %v" , out .String ())
69
+ }
70
+ }
71
+
53
72
func BenchmarkTraceLogging (b * testing.B ) {
54
73
SetDefault (NewLogger (NewTerminalHandler (os .Stderr , true )))
55
74
b .ResetTimer ()
You can’t perform that action at this time.
0 commit comments