Skip to content

Commit 8876868

Browse files
Roberto Bayardolightclient
andauthored
log: default JSON log handler should log all verbosity levels (#29471)
Co-authored-by: lightclient <[email protected]>
1 parent ccb76c0 commit 8876868

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

internal/debug/flags.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ func Setup(ctx *cli.Context) error {
231231
case ctx.Bool(logjsonFlag.Name):
232232
// Retain backwards compatibility with `--log.json` flag if `--log.format` not set
233233
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)
235235
case logFmtFlag == "json":
236-
handler = log.JSONHandler(output)
236+
handler = log.JSONHandlerWithLevel(output, log.LevelInfo)
237237
case logFmtFlag == "logfmt":
238238
handler = log.LogfmtHandler(output)
239239
case logFmtFlag == "", logFmtFlag == "terminal":

log/handler.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,15 @@ func (l *leveler) Level() slog.Level {
115115

116116
// JSONHandler returns a handler which prints records in JSON format.
117117
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 {
118124
return slog.NewJSONHandler(wr, &slog.HandlerOptions{
119125
ReplaceAttr: builtinReplaceJSON,
126+
Level: &leveler{level},
120127
})
121128
}
122129

log/logger_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,25 @@ func TestTerminalHandlerWithAttrs(t *testing.T) {
5050
}
5151
}
5252

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+
5372
func BenchmarkTraceLogging(b *testing.B) {
5473
SetDefault(NewLogger(NewTerminalHandler(os.Stderr, true)))
5574
b.ResetTimer()

0 commit comments

Comments
 (0)