Skip to content

Commit 9e03447

Browse files
gzliudanlightclient
andcommitted
log: default JSON log handler should log all verbosity levels (ethereum#29471)
Co-authored-by: lightclient <[email protected]>
1 parent 368b278 commit 9e03447

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

internal/debug/flags.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ func Setup(ctx *cli.Context) error {
226226
case ctx.Bool(logjsonFlag.Name):
227227
// Retain backwards compatibility with `--log-json` flag if `--log-format` not set
228228
defer log.Warn("The flag '--log-json' is deprecated, please use '--log-format=json' instead")
229-
handler = log.JSONHandler(output)
229+
handler = log.JSONHandlerWithLevel(output, log.LevelInfo)
230230
case logFmtFlag == "json":
231-
handler = log.JSONHandler(output)
231+
handler = log.JSONHandlerWithLevel(output, log.LevelInfo)
232232
case logFmtFlag == "logfmt":
233233
handler = log.LogfmtHandler(output)
234234
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+
// JSONHandlerWithLevel 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: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestLoggingWithVmodule(t *testing.T) {
2626
logger.Trace("a message", "foo", "bar")
2727
have := out.String()
2828
// The timestamp is locale-dependent, so we want to trim that off
29-
// "INFO [01-01|00:00:00.000] a messag ..." -> "a messag..."
29+
// "INFO [01-01|00:00:00.000] a message ..." -> "a message..."
3030
have = strings.Split(have, "]")[1]
3131
want := " a message foo=bar\n"
3232
if have != want {
@@ -42,14 +42,33 @@ func TestTerminalHandlerWithAttrs(t *testing.T) {
4242
logger.Trace("a message", "foo", "bar")
4343
have := out.String()
4444
// The timestamp is locale-dependent, so we want to trim that off
45-
// "INFO [01-01|00:00:00.000] a messag ..." -> "a messag..."
45+
// "INFO [01-01|00:00:00.000] a message ..." -> "a message..."
4646
have = strings.Split(have, "]")[1]
4747
want := " a message baz=bat foo=bar\n"
4848
if have != want {
4949
t.Errorf("\nhave: %q\nwant: %q\n", have, want)
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()
@@ -78,7 +97,7 @@ func benchmarkLogger(b *testing.B, l Logger) {
7897
tt = time.Now()
7998
bigint = big.NewInt(100)
8099
nilbig *big.Int
81-
err = errors.New("Oh nooes it's crap")
100+
err = errors.New("oh nooes it's crap")
82101
)
83102
b.ReportAllocs()
84103
b.ResetTimer()
@@ -107,7 +126,7 @@ func TestLoggerOutput(t *testing.T) {
107126
tt = time.Time{}
108127
bigint = big.NewInt(100)
109128
nilbig *big.Int
110-
err = errors.New("Oh nooes it's crap")
129+
err = errors.New("oh nooes it's crap")
111130
smallUint = uint256.NewInt(500_000)
112131
bigUint = &uint256.Int{0xff, 0xff, 0xff, 0xff}
113132
)
@@ -131,7 +150,7 @@ func TestLoggerOutput(t *testing.T) {
131150

132151
have := out.String()
133152
t.Logf("output %v", out.String())
134-
want := `INFO [11-07|19:14:33.821] This is a message foo=123 bytes="[0 0 0 0 0 0 0 0 0 0]" bonk="a string with text" time=0001-01-01T00:00:00+0000 bigint=100 nilbig=<nil> err="Oh nooes it's crap" struct="{A:Foo B:12}" struct="{A:Foo\nLinebreak B:122}" ptrstruct="&{A:Foo B:12}" smalluint=500,000 bigUint=1,600,660,942,523,603,594,864,898,306,482,794,244,293,965,082,972,225,630,372,095
153+
want := `INFO [11-07|19:14:33.821] This is a message foo=123 bytes="[0 0 0 0 0 0 0 0 0 0]" bonk="a string with text" time=0001-01-01T00:00:00+0000 bigint=100 nilbig=<nil> err="oh nooes it's crap" struct="{A:Foo B:12}" struct="{A:Foo\nLinebreak B:122}" ptrstruct="&{A:Foo B:12}" smalluint=500,000 bigUint=1,600,660,942,523,603,594,864,898,306,482,794,244,293,965,082,972,225,630,372,095
135154
`
136155
if !bytes.Equal([]byte(have)[25:], []byte(want)[25:]) {
137156
t.Errorf("Error\nhave: %q\nwant: %q", have, want)

0 commit comments

Comments
 (0)