internal/testlog: when instantiating new logger via With, set logger handler reference as the bufHandler in the newly-instantiated logger instead of copying from parent

This commit is contained in:
Jared Wasinger 2025-04-01 22:28:54 +02:00
parent 22c0605b68
commit fbe86e3494
2 changed files with 23 additions and 1 deletions

View file

@ -170,7 +170,8 @@ func (l *logger) Crit(msg string, ctx ...interface{}) {
} }
func (l *logger) With(ctx ...interface{}) log.Logger { func (l *logger) With(ctx ...interface{}) log.Logger {
return &logger{l.t, l.l.With(ctx...), l.mu, l.h} newLogger := l.l.With(ctx...)
return &logger{l.t, newLogger, l.mu, newLogger.Handler().(*bufHandler)}
} }
func (l *logger) New(ctx ...interface{}) log.Logger { func (l *logger) New(ctx ...interface{}) log.Logger {

View file

@ -0,0 +1,21 @@
package testlog
import (
"github.com/ethereum/go-ethereum/log"
"testing"
)
func TestLogging(t *testing.T) {
l := Logger(t, log.LevelInfo)
subLogger := l.New("foobar", 123)
l.Info("Visible")
subLogger.Info("Hide and seek") // not visible due to sub buffer never being flushed
l.Info("Also visible")
t.Log("flushed: ", l.Handler().(*bufHandler).buf)
t.Log("remaining: ", subLogger.Handler().(*bufHandler).buf)
// horrible hack to manually bring back the expected log data
l.Handler().(*bufHandler).buf = subLogger.Handler().(*bufHandler).buf
l.(*logger).flush()
}