Skip to content

Commit 28e7371

Browse files
authored
all: replace log15 with slog (#28187)
This PR replaces Geth's logger package (a fork of [log15](https://github.com/inconshreveable/log15)) with an implementation using slog, a logging library included as part of the Go standard library as of Go1.21. Main changes are as follows: * removes any log handlers that were unused in the Geth codebase. * Json, logfmt, and terminal formatters are now slog handlers. * Verbosity level constants are changed to match slog constant values. Internal translation is done to make this opaque to the user and backwards compatible with existing `--verbosity` and `--vmodule` options. * `--log.backtraceat` and `--log.debug` are removed. The external-facing API is largely the same as the existing Geth logger. Logger method signatures remain unchanged. A small semantic difference is that a `Handler` can only be set once per `Logger` and not changed dynamically. This just means that a new logger must be instantiated every time the handler of the root logger is changed. ---- For users of the `go-ethereum/log` module. If you were using this module for your own project, you will need to change the initialization. If you previously did ```golang log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StreamHandler(os.Stderr, log.TerminalFormat(true)))) ``` You now instead need to do ```golang log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelInfo, true))) ``` See more about reasoning here: #28558 (comment)
1 parent 61b844f commit 28e7371

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+932
-1765
lines changed

cmd/abigen/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func abigen(c *cli.Context) error {
232232
}
233233

234234
func main() {
235-
log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StreamHandler(os.Stderr, log.TerminalFormat(true))))
235+
log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelInfo, true)))
236236

237237
if err := app.Run(os.Args); err != nil {
238238
fmt.Fprintln(os.Stderr, err)

cmd/bootnode/main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/ethereum/go-ethereum/p2p/enode"
3333
"github.com/ethereum/go-ethereum/p2p/nat"
3434
"github.com/ethereum/go-ethereum/p2p/netutil"
35+
"golang.org/x/exp/slog"
3536
)
3637

3738
func main() {
@@ -52,10 +53,10 @@ func main() {
5253
)
5354
flag.Parse()
5455

55-
glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
56-
glogger.Verbosity(log.Lvl(*verbosity))
56+
glogger := log.NewGlogHandler(log.NewTerminalHandler(os.Stderr, false))
57+
glogger.Verbosity(slog.Level(*verbosity))
5758
glogger.Vmodule(*vmodule)
58-
log.Root().SetHandler(glogger)
59+
log.SetDefault(log.NewLogger(glogger))
5960

6061
natm, err := nat.Parse(*natdesc)
6162
if err != nil {

cmd/clef/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import (
5757
"github.com/mattn/go-colorable"
5858
"github.com/mattn/go-isatty"
5959
"github.com/urfave/cli/v2"
60+
"golang.org/x/exp/slog"
6061
)
6162

6263
const legalWarning = `
@@ -492,7 +493,7 @@ func initialize(c *cli.Context) error {
492493
if usecolor {
493494
output = colorable.NewColorable(logOutput)
494495
}
495-
log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(c.Int(logLevelFlag.Name)), log.StreamHandler(output, log.TerminalFormat(usecolor))))
496+
log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(output, slog.Level(c.Int(logLevelFlag.Name)), usecolor)))
496497

497498
return nil
498499
}

cmd/devp2p/runtest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func runTests(ctx *cli.Context, tests []utesting.Test) error {
5454
}
5555
// Disable logging unless explicitly enabled.
5656
if !ctx.IsSet("verbosity") && !ctx.IsSet("vmodule") {
57-
log.Root().SetHandler(log.DiscardHandler())
57+
log.SetDefault(log.NewLogger(log.DiscardHandler()))
5858
}
5959
// Run the tests.
6060
var run = utesting.RunTests

cmd/evm/internal/t8ntool/block.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/ethereum/go-ethereum/log"
3434
"github.com/ethereum/go-ethereum/rlp"
3535
"github.com/urfave/cli/v2"
36+
"golang.org/x/exp/slog"
3637
)
3738

3839
//go:generate go run github.com/fjl/gencodec -type header -field-override headerMarshaling -out gen_header.go
@@ -216,9 +217,9 @@ func (i *bbInput) sealClique(block *types.Block) (*types.Block, error) {
216217
// BuildBlock constructs a block from the given inputs.
217218
func BuildBlock(ctx *cli.Context) error {
218219
// Configure the go-ethereum logger
219-
glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
220-
glogger.Verbosity(log.Lvl(ctx.Int(VerbosityFlag.Name)))
221-
log.Root().SetHandler(glogger)
220+
glogger := log.NewGlogHandler(log.NewTerminalHandler(os.Stderr, false))
221+
glogger.Verbosity(slog.Level(ctx.Int(VerbosityFlag.Name)))
222+
log.SetDefault(log.NewLogger(glogger))
222223

223224
baseDir, err := createBasedir(ctx)
224225
if err != nil {

cmd/evm/internal/t8ntool/transaction.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/ethereum/go-ethereum/rlp"
3434
"github.com/ethereum/go-ethereum/tests"
3535
"github.com/urfave/cli/v2"
36+
"golang.org/x/exp/slog"
3637
)
3738

3839
type result struct {
@@ -66,9 +67,9 @@ func (r *result) MarshalJSON() ([]byte, error) {
6667

6768
func Transaction(ctx *cli.Context) error {
6869
// Configure the go-ethereum logger
69-
glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
70-
glogger.Verbosity(log.Lvl(ctx.Int(VerbosityFlag.Name)))
71-
log.Root().SetHandler(glogger)
70+
glogger := log.NewGlogHandler(log.NewTerminalHandler(os.Stderr, false))
71+
glogger.Verbosity(slog.Level(ctx.Int(VerbosityFlag.Name)))
72+
log.SetDefault(log.NewLogger(glogger))
7273

7374
var (
7475
err error

cmd/evm/internal/t8ntool/transition.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"os"
2525
"path"
2626

27+
"golang.org/x/exp/slog"
28+
2729
"github.com/ethereum/go-ethereum/common"
2830
"github.com/ethereum/go-ethereum/common/hexutil"
2931
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
@@ -81,9 +83,9 @@ type input struct {
8183

8284
func Transition(ctx *cli.Context) error {
8385
// Configure the go-ethereum logger
84-
glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
85-
glogger.Verbosity(log.Lvl(ctx.Int(VerbosityFlag.Name)))
86-
log.Root().SetHandler(glogger)
86+
glogger := log.NewGlogHandler(log.NewTerminalHandler(os.Stderr, false))
87+
glogger.Verbosity(slog.Level(ctx.Int(VerbosityFlag.Name)))
88+
log.SetDefault(log.NewLogger(glogger))
8789

8890
var (
8991
err error

cmd/geth/logging_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"os/exec"
2929
"strings"
3030
"testing"
31+
"encoding/json"
3132

3233
"github.com/ethereum/go-ethereum/internal/reexec"
3334
)
@@ -98,6 +99,53 @@ func testConsoleLogging(t *testing.T, format string, tStart, tEnd int) {
9899
}
99100
}
100101

102+
func TestJsonLogging(t *testing.T) {
103+
t.Parallel()
104+
haveB, err := runSelf("--log.format", "json", "logtest")
105+
if err != nil {
106+
t.Fatal(err)
107+
}
108+
readFile, err := os.Open("testdata/logging/logtest-json.txt")
109+
if err != nil {
110+
t.Fatal(err)
111+
}
112+
wantLines := split(readFile)
113+
haveLines := split(bytes.NewBuffer(haveB))
114+
for i, wantLine := range wantLines {
115+
if i > len(haveLines)-1 {
116+
t.Fatalf("format %v, line %d missing, want:%v", "json", i, wantLine)
117+
}
118+
haveLine := haveLines[i]
119+
for strings.Contains(haveLine, "Unknown config environment variable") {
120+
// This can happen on CI runs. Drop it.
121+
haveLines = append(haveLines[:i], haveLines[i+1:]...)
122+
haveLine = haveLines[i]
123+
}
124+
var have, want []byte
125+
{
126+
var h map[string]any
127+
if err := json.Unmarshal([]byte(haveLine), &h); err != nil {
128+
t.Fatal(err)
129+
}
130+
h["t"] = "xxx"
131+
have, _ = json.Marshal(h)
132+
}
133+
{
134+
var w map[string]any
135+
if err := json.Unmarshal([]byte(wantLine), &w); err != nil {
136+
t.Fatal(err)
137+
}
138+
w["t"] = "xxx"
139+
want, _ = json.Marshal(w)
140+
}
141+
if !bytes.Equal(have, want) {
142+
// show an intelligent diff
143+
t.Logf(nicediff(have, want))
144+
t.Errorf("file content wrong")
145+
}
146+
}
147+
}
148+
101149
func TestVmodule(t *testing.T) {
102150
t.Parallel()
103151
checkOutput := func(level int, want, wantNot string) {

cmd/geth/logtestcmd_active.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"time"
2727

2828
"github.com/ethereum/go-ethereum/common"
29+
"github.com/ethereum/go-ethereum/internal/debug"
2930
"github.com/ethereum/go-ethereum/log"
3031
"github.com/holiman/uint256"
3132
"github.com/urfave/cli/v2"
@@ -49,7 +50,9 @@ func (c customQuotedStringer) String() string {
4950
// logTest is an entry point which spits out some logs. This is used by testing
5051
// to verify expected outputs
5152
func logTest(ctx *cli.Context) error {
52-
log.ResetGlobalState()
53+
// clear field padding map
54+
debug.ResetLogging()
55+
5356
{ // big.Int
5457
ba, _ := new(big.Int).SetString("111222333444555678999", 10) // "111,222,333,444,555,678,999"
5558
bb, _ := new(big.Int).SetString("-111222333444555678999", 10) // "-111,222,333,444,555,678,999"

cmd/geth/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ var (
144144
utils.GpoMaxGasPriceFlag,
145145
utils.GpoIgnoreGasPriceFlag,
146146
configFileFlag,
147+
utils.LogDebugFlag,
148+
utils.LogBacktraceAtFlag,
147149
}, utils.NetworkFlags, utils.DatabaseFlags)
148150

149151
rpcFlags = []cli.Flag{

0 commit comments

Comments
 (0)