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: https://github.com/ethereum/go-ethereum/issues/28558#issuecomment-1820606613
This commit is contained in:
@@ -41,6 +41,7 @@ import (
|
||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"github.com/gorilla/websocket"
|
||||
"golang.org/x/exp/slog"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -375,9 +376,11 @@ type execNodeConfig struct {
|
||||
|
||||
func initLogging() {
|
||||
// Initialize the logging by default first.
|
||||
glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.LogfmtFormat()))
|
||||
glogger.Verbosity(log.LvlInfo)
|
||||
log.Root().SetHandler(glogger)
|
||||
var innerHandler slog.Handler
|
||||
innerHandler = slog.NewTextHandler(os.Stderr, nil)
|
||||
glogger := log.NewGlogHandler(innerHandler)
|
||||
glogger.Verbosity(log.LevelInfo)
|
||||
log.SetDefault(log.NewLogger(glogger))
|
||||
|
||||
confEnv := os.Getenv(envNodeConfig)
|
||||
if confEnv == "" {
|
||||
@@ -395,14 +398,15 @@ func initLogging() {
|
||||
}
|
||||
writer = logWriter
|
||||
}
|
||||
var verbosity = log.LvlInfo
|
||||
if conf.Node.LogVerbosity <= log.LvlTrace && conf.Node.LogVerbosity >= log.LvlCrit {
|
||||
verbosity = conf.Node.LogVerbosity
|
||||
var verbosity = log.LevelInfo
|
||||
if conf.Node.LogVerbosity <= log.LevelTrace && conf.Node.LogVerbosity >= log.LevelCrit {
|
||||
verbosity = log.FromLegacyLevel(int(conf.Node.LogVerbosity))
|
||||
}
|
||||
// Reinitialize the logger
|
||||
glogger = log.NewGlogHandler(log.StreamHandler(writer, log.TerminalFormat(true)))
|
||||
innerHandler = log.NewTerminalHandler(writer, true)
|
||||
glogger = log.NewGlogHandler(innerHandler)
|
||||
glogger.Verbosity(verbosity)
|
||||
log.Root().SetHandler(glogger)
|
||||
log.SetDefault(log.NewLogger(glogger))
|
||||
}
|
||||
|
||||
// execP2PNode starts a simulation node when the current binary is executed with
|
||||
|
||||
@@ -34,6 +34,7 @@ import (
|
||||
"github.com/ethereum/go-ethereum/p2p/enr"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"github.com/gorilla/websocket"
|
||||
"golang.org/x/exp/slog"
|
||||
)
|
||||
|
||||
// Node represents a node in a simulation network which is created by a
|
||||
@@ -129,7 +130,7 @@ type NodeConfig struct {
|
||||
// LogVerbosity is the log verbosity of the p2p node at runtime.
|
||||
//
|
||||
// The default verbosity is INFO.
|
||||
LogVerbosity log.Lvl
|
||||
LogVerbosity slog.Level
|
||||
}
|
||||
|
||||
// nodeConfigJSON is used to encode and decode NodeConfig as JSON by encoding
|
||||
@@ -197,7 +198,7 @@ func (n *NodeConfig) UnmarshalJSON(data []byte) error {
|
||||
n.Port = confJSON.Port
|
||||
n.EnableMsgEvents = confJSON.EnableMsgEvents
|
||||
n.LogFile = confJSON.LogFile
|
||||
n.LogVerbosity = log.Lvl(confJSON.LogVerbosity)
|
||||
n.LogVerbosity = slog.Level(confJSON.LogVerbosity)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ func main() {
|
||||
flag.Parse()
|
||||
|
||||
// set the log level to Trace
|
||||
log.Root().SetHandler(log.LvlFilterHandler(log.LvlTrace, log.StreamHandler(os.Stderr, log.TerminalFormat(false))))
|
||||
log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelTrace, false)))
|
||||
|
||||
// register a single ping-pong service
|
||||
services := map[string]adapters.LifecycleConstructor{
|
||||
|
||||
@@ -37,14 +37,14 @@ import (
|
||||
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"github.com/mattn/go-colorable"
|
||||
"golang.org/x/exp/slog"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
loglevel := flag.Int("loglevel", 2, "verbosity of logs")
|
||||
|
||||
flag.Parse()
|
||||
log.PrintOrigins(true)
|
||||
log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*loglevel), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true))))
|
||||
log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(colorable.NewColorableStderr(), slog.Level(*loglevel), true)))
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user