mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
internal/testlog, log: add Inner method to log.Logger to expose wrapped slog.Logger. fix log.SetDefault to also set slog default logger.
This commit is contained in:
parent
ec2e836aa3
commit
98166316b9
3 changed files with 20 additions and 13 deletions
|
|
@ -98,8 +98,8 @@ func LoggerWithHandler(t *testing.T, handler slog.Handler) log.Logger {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *logger) Handler() slog.Handler {
|
func (l *logger) Inner() *slog.Logger {
|
||||||
return l.l.Handler()
|
return l.l.Inner()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *logger) Trace(msg string, ctx ...interface{}) {
|
func (l *logger) Trace(msg string, ctx ...interface{}) {
|
||||||
|
|
|
||||||
|
|
@ -105,9 +105,6 @@ func LevelString(l slog.Level) string {
|
||||||
|
|
||||||
// A Logger writes key/value pairs to a Handler
|
// A Logger writes key/value pairs to a Handler
|
||||||
type Logger interface {
|
type Logger interface {
|
||||||
// Handler returns the handler associated with this logger
|
|
||||||
Handler() slog.Handler
|
|
||||||
|
|
||||||
// TODO: add WithGroup()?
|
// TODO: add WithGroup()?
|
||||||
|
|
||||||
// With returns a new Logger that has this logger's attributes plus the given attributes
|
// With returns a new Logger that has this logger's attributes plus the given attributes
|
||||||
|
|
@ -136,6 +133,9 @@ type Logger interface {
|
||||||
|
|
||||||
// Crit logs a message at the crit level with context key/value pairs, and exits
|
// Crit logs a message at the crit level with context key/value pairs, and exits
|
||||||
Crit(msg string, ctx ...interface{})
|
Crit(msg string, ctx ...interface{})
|
||||||
|
|
||||||
|
// Inner returns the underlying slog logger that is wrapped
|
||||||
|
Inner() *slog.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
type logger struct {
|
type logger struct {
|
||||||
|
|
@ -149,8 +149,9 @@ func NewLogger(h slog.Handler) Logger {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *logger) Handler() slog.Handler {
|
// Inner returns the underlying slog logger that
|
||||||
return l.inner.Handler()
|
func (l *logger) Inner() *slog.Logger {
|
||||||
|
return l.inner
|
||||||
}
|
}
|
||||||
|
|
||||||
// write logs a message at the specified level:
|
// write logs a message at the specified level:
|
||||||
|
|
|
||||||
18
log/root.go
18
log/root.go
|
|
@ -2,22 +2,26 @@ package log
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"sync/atomic"
|
"sync"
|
||||||
|
|
||||||
"golang.org/x/exp/slog"
|
"golang.org/x/exp/slog"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
root = new(atomic.Value)
|
rootMu sync.Mutex
|
||||||
|
root logger
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
defaultLogger := &logger{slog.New(DiscardHandler())}
|
defaultLogger := &logger{slog.New(DiscardHandler())}
|
||||||
root.Store(defaultLogger)
|
SetDefault(defaultLogger)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetDefault(l Logger) {
|
func SetDefault(l Logger) {
|
||||||
root.Store(l)
|
rootMu.Lock()
|
||||||
|
defer rootMu.Unlock()
|
||||||
|
root := l
|
||||||
|
slog.SetDefault(root.Inner())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Root returns the root logger
|
// Root returns the root logger
|
||||||
|
|
@ -26,8 +30,10 @@ func Root() Logger {
|
||||||
}
|
}
|
||||||
|
|
||||||
func rootLogger() *logger {
|
func rootLogger() *logger {
|
||||||
res, _ := root.Load().(*logger)
|
rootMu.Lock()
|
||||||
return res
|
defer rootMu.Unlock()
|
||||||
|
res := root
|
||||||
|
return &res
|
||||||
}
|
}
|
||||||
|
|
||||||
// The following functions bypass the exported logger methods (logger.Debug,
|
// The following functions bypass the exported logger methods (logger.Debug,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue