forked from cerc-io/plugeth
28e7371701
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
118 lines
2.8 KiB
Go
118 lines
2.8 KiB
Go
package log
|
|
|
|
import (
|
|
"os"
|
|
"sync/atomic"
|
|
|
|
"golang.org/x/exp/slog"
|
|
)
|
|
|
|
var root atomic.Value
|
|
|
|
func init() {
|
|
defaultLogger := &logger{slog.New(DiscardHandler())}
|
|
SetDefault(defaultLogger)
|
|
}
|
|
|
|
// SetDefault sets the default global logger
|
|
func SetDefault(l Logger) {
|
|
root.Store(l)
|
|
if lg, ok := l.(*logger); ok {
|
|
slog.SetDefault(lg.inner)
|
|
}
|
|
}
|
|
|
|
// Root returns the root logger
|
|
func Root() Logger {
|
|
return root.Load().(Logger)
|
|
}
|
|
|
|
// The following functions bypass the exported logger methods (logger.Debug,
|
|
// etc.) to keep the call depth the same for all paths to logger.Write so
|
|
// runtime.Caller(2) always refers to the call site in client code.
|
|
|
|
// Trace is a convenient alias for Root().Trace
|
|
//
|
|
// Log a message at the trace level with context key/value pairs
|
|
//
|
|
// # Usage
|
|
//
|
|
// log.Trace("msg")
|
|
// log.Trace("msg", "key1", val1)
|
|
// log.Trace("msg", "key1", val1, "key2", val2)
|
|
func Trace(msg string, ctx ...interface{}) {
|
|
Root().Write(LevelTrace, msg, ctx...)
|
|
}
|
|
|
|
// Debug is a convenient alias for Root().Debug
|
|
//
|
|
// Log a message at the debug level with context key/value pairs
|
|
//
|
|
// # Usage Examples
|
|
//
|
|
// log.Debug("msg")
|
|
// log.Debug("msg", "key1", val1)
|
|
// log.Debug("msg", "key1", val1, "key2", val2)
|
|
func Debug(msg string, ctx ...interface{}) {
|
|
Root().Write(slog.LevelDebug, msg, ctx...)
|
|
}
|
|
|
|
// Info is a convenient alias for Root().Info
|
|
//
|
|
// Log a message at the info level with context key/value pairs
|
|
//
|
|
// # Usage Examples
|
|
//
|
|
// log.Info("msg")
|
|
// log.Info("msg", "key1", val1)
|
|
// log.Info("msg", "key1", val1, "key2", val2)
|
|
func Info(msg string, ctx ...interface{}) {
|
|
Root().Write(slog.LevelInfo, msg, ctx...)
|
|
}
|
|
|
|
// Warn is a convenient alias for Root().Warn
|
|
//
|
|
// Log a message at the warn level with context key/value pairs
|
|
//
|
|
// # Usage Examples
|
|
//
|
|
// log.Warn("msg")
|
|
// log.Warn("msg", "key1", val1)
|
|
// log.Warn("msg", "key1", val1, "key2", val2)
|
|
func Warn(msg string, ctx ...interface{}) {
|
|
Root().Write(slog.LevelWarn, msg, ctx...)
|
|
}
|
|
|
|
// Error is a convenient alias for Root().Error
|
|
//
|
|
// Log a message at the error level with context key/value pairs
|
|
//
|
|
// # Usage Examples
|
|
//
|
|
// log.Error("msg")
|
|
// log.Error("msg", "key1", val1)
|
|
// log.Error("msg", "key1", val1, "key2", val2)
|
|
func Error(msg string, ctx ...interface{}) {
|
|
Root().Write(slog.LevelError, msg, ctx...)
|
|
}
|
|
|
|
// Crit is a convenient alias for Root().Crit
|
|
//
|
|
// Log a message at the crit level with context key/value pairs, and then exit.
|
|
//
|
|
// # Usage Examples
|
|
//
|
|
// log.Crit("msg")
|
|
// log.Crit("msg", "key1", val1)
|
|
// log.Crit("msg", "key1", val1, "key2", val2)
|
|
func Crit(msg string, ctx ...interface{}) {
|
|
Root().Write(LevelCrit, msg, ctx...)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// New returns a new logger with the given context.
|
|
// New is a convenient alias for Root().New
|
|
func New(ctx ...interface{}) Logger {
|
|
return Root().With(ctx...)
|
|
}
|