Skip to content

Commit 984f826

Browse files
authored
cmd/geth: more special cases logging tests (#28527)
adds logging tests for errors and custom fmt.Stringer-types which output strings that needs to be quoted/escaped.
1 parent c5b7cfa commit 984f826

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

cmd/geth/logtestcmd_active.go

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package main
2020

2121
import (
22+
"errors"
2223
"fmt"
2324
"math"
2425
"math/big"
@@ -39,6 +40,12 @@ var logTestCommand = &cli.Command{
3940
This command is only meant for testing.
4041
`}
4142

43+
type customQuotedStringer struct {
44+
}
45+
func (c customQuotedStringer) String() string {
46+
return "output with 'quotes'"
47+
}
48+
4249
// logTest is an entry point which spits out some logs. This is used by testing
4350
// to verify expected outputs
4451
func logTest(ctx *cli.Context) error {
@@ -70,6 +77,8 @@ func logTest(ctx *cli.Context) error {
7077
log.Info("uint64", "18,446,744,073,709,551,615", uint64(math.MaxUint64))
7178
}
7279
{ // Special characters
80+
81+
7382
log.Info("Special chars in value", "key", "special \r\n\t chars")
7483
log.Info("Special chars in key", "special \n\t chars", "value")
7584

@@ -83,9 +92,13 @@ func logTest(ctx *cli.Context) error {
8392

8493
colored := fmt.Sprintf("\u001B[%dmColored\u001B[0m[", 35)
8594
log.Info(colored, colored, colored)
95+
err := errors.New("this is an 'error'")
96+
log.Info("an error message with quotes", "error", err)
8697
}
8798
{ // Custom Stringer() - type
8899
log.Info("Custom Stringer value", "2562047h47m16.854s", common.PrettyDuration(time.Duration(9223372036854775807)))
100+
var c customQuotedStringer
101+
log.Info("a custom stringer that emits quoted text", "output", c)
89102
}
90103
{ // Lazy eval
91104
log.Info("Lazy evaluation of value", "key", log.Lazy{Fn: func() interface{} { return "lazy value" }})

cmd/geth/testdata/logging/logtest-logfmt.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ t=xxxxxxxxxxxxxxxxxxxxxxxx lvl=info msg="Bash escapes in value" key="\x1b[1G\x1
1818
t=xxxxxxxxxxxxxxxxxxxxxxxx lvl=info msg="Bash escapes in key" "\x1b[1G\x1b[K\x1b[1A"=value
1919
t=xxxxxxxxxxxxxxxxxxxxxxxx lvl=info msg="Bash escapes in message \x1b[1G\x1b[K\x1b[1A end" key=value
2020
t=xxxxxxxxxxxxxxxxxxxxxxxx lvl=info msg="\x1b[35mColored\x1b[0m[" "\x1b[35mColored\x1b[0m["="\x1b[35mColored\x1b[0m["
21-
t=xxxxxxxxxxxxxxxxxxxxxxxx lvl=info msg="Custom Stringer value" 2562047h47m16.854s=2562047h47m16.854s
22-
t=xxxxxxxxxxxxxxxxxxxxxxxx lvl=info msg="Lazy evaluation of value" key="lazy value"
21+
t=xxxxxxxxxxxxxxxxxxxxxxxx lvl=info msg="an error message with quotes" error="this is an 'error'"
22+
t=xxxxxxxxxxxxxxxxxxxxxxxx lvl=info msg="Custom Stringer value" 2562047h47m16.854s=2562047h47m16.854s
23+
t=xxxxxxxxxxxxxxxxxxxxxxxx lvl=info msg="a custom stringer that emits quoted text" output="output with 'quotes'"
24+
t=xxxxxxxxxxxxxxxxxxxxxxxx lvl=info msg="Lazy evaluation of value" key="lazy value"
2325
t=xxxxxxxxxxxxxxxxxxxxxxxx lvl=info msg="A message with wonky 💩 characters"
2426
t=xxxxxxxxxxxxxxxxxxxxxxxx lvl=info msg="A multiline message \nINFO [10-18|14:11:31.106] with wonky characters 💩"
2527
t=xxxxxxxxxxxxxxxxxxxxxxxx lvl=info msg="A multiline message \nLALA [ZZZZZZZZZZZZZZZZZZ] Actually part of message above"

cmd/geth/testdata/logging/logtest-terminal.txt

+10-8
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,24 @@ INFO [XX-XX|XX:XX:XX.XXX] Bash escapes in value key="\x1b[1G\
1818
INFO [XX-XX|XX:XX:XX.XXX] Bash escapes in key "\x1b[1G\x1b[K\x1b[1A"=value
1919
INFO [XX-XX|XX:XX:XX.XXX] "Bash escapes in message \x1b[1G\x1b[K\x1b[1A end" key=value
2020
INFO [XX-XX|XX:XX:XX.XXX] "\x1b[35mColored\x1b[0m[" "\x1b[35mColored\x1b[0m["="\x1b[35mColored\x1b[0m["
21+
INFO [XX-XX|XX:XX:XX.XXX] an error message with quotes error="this is an 'error'"
2122
INFO [XX-XX|XX:XX:XX.XXX] Custom Stringer value 2562047h47m16.854s=2562047h47m16.854s
23+
INFO [XX-XX|XX:XX:XX.XXX] a custom stringer that emits quoted text output="output with 'quotes'"
2224
INFO [XX-XX|XX:XX:XX.XXX] Lazy evaluation of value key="lazy value"
23-
INFO [XX-XX|XX:XX:XX.XXX] "A message with wonky 💩 characters"
24-
INFO [XX-XX|XX:XX:XX.XXX] "A multiline message \nINFO [10-18|14:11:31.106] with wonky characters 💩"
25-
INFO [XX-XX|XX:XX:XX.XXX] A multiline message
26-
LALA [XXZXXZXXZXXZXXZXXX] Actually part of message above
25+
INFO [XX-XX|XX:XX:XX.XXX] "A message with wonky 💩 characters"
26+
INFO [XX-XX|XX:XX:XX.XXX] "A multiline message \nINFO [10-18|14:11:31.106] with wonky characters 💩"
27+
INFO [XX-XX|XX:XX:XX.XXX] A multiline message
28+
LALA [ZZZZZZZZZZZZZZZZZZ] Actually part of message above
2729
INFO [XX-XX|XX:XX:XX.XXX] boolean true=true false=false
2830
INFO [XX-XX|XX:XX:XX.XXX] repeated-key 1 foo=alpha foo=beta
2931
INFO [XX-XX|XX:XX:XX.XXX] repeated-key 2 xx=short xx=longer
30-
INFO [XX-XX|XX:XX:XX.XXX] log at level info
31-
WARN [XX-XX|XX:XX:XX.XXX] log at level warn
32-
ERROR[XX-XX|XX:XX:XX.XXX] log at level error
32+
INFO [XX-XX|XX:XX:XX.XXX] log at level info
33+
WARN [XX-XX|XX:XX:XX.XXX] log at level warn
34+
ERROR[XX-XX|XX:XX:XX.XXX] log at level error
3335
INFO [XX-XX|XX:XX:XX.XXX] test bar=short a="aligned left"
3436
INFO [XX-XX|XX:XX:XX.XXX] test bar="a long message" a=1
3537
INFO [XX-XX|XX:XX:XX.XXX] test bar=short a="aligned right"
36-
INFO [XX-XX|XX:XX:XX.XXX] The following logs should align so that the key-fields make 5 columns
38+
INFO [XX-XX|XX:XX:XX.XXX] The following logs should align so that the key-fields make 5 columns
3739
INFO [XX-XX|XX:XX:XX.XXX] Inserted known block number=1012 hash=000000..001234 txs=200 gas=1,123,123 other=first
3840
INFO [XX-XX|XX:XX:XX.XXX] Inserted new block number=1 hash=000000..001235 txs=2 gas=1123 other=second
3941
INFO [XX-XX|XX:XX:XX.XXX] Inserted known block number=99 hash=000000..012322 txs=10 gas=1 other=third

0 commit comments

Comments
 (0)