mirror of https://github.com/knative/pkg.git
Remove caching in test logger setup (#661)
* Remove caching in test logger setup * Revert "Remove caching in test logger setup" This reverts commite3c5349e3c
. * Don't create new logger in SetupFakeContextWithCancel * Revert "Don't create new logger in SetupFakeContextWithCancel" This reverts commit2d57ccf028
. * Remove test logger cache, and leave an empty shell for ClearAll()
This commit is contained in:
parent
b55b842259
commit
06918f4c69
|
@ -18,7 +18,6 @@ package testing
|
|||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
@ -27,40 +26,19 @@ import (
|
|||
"knative.dev/pkg/logging"
|
||||
)
|
||||
|
||||
var (
|
||||
loggers = make(map[string]*zap.SugaredLogger)
|
||||
m sync.Mutex
|
||||
)
|
||||
|
||||
// TestLogger gets a logger to use in unit and end to end tests
|
||||
func TestLogger(t *testing.T) *zap.SugaredLogger {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
|
||||
logger, ok := loggers[t.Name()]
|
||||
|
||||
if ok {
|
||||
return logger
|
||||
}
|
||||
|
||||
opts := zaptest.WrapOptions(
|
||||
zap.AddCaller(),
|
||||
zap.Development(),
|
||||
)
|
||||
|
||||
logger = zaptest.NewLogger(t, opts).Sugar().Named(t.Name())
|
||||
loggers[t.Name()] = logger
|
||||
|
||||
return logger
|
||||
return zaptest.NewLogger(t, opts).Sugar()
|
||||
}
|
||||
|
||||
// ClearAll removes all the testing loggers.
|
||||
// `go test -count=X` executes runs in the same process, thus the map
|
||||
// persists between the runs, but the `t` will no longer be valid and will
|
||||
// cause a panic deep inside testing code.
|
||||
func ClearAll() {
|
||||
loggers = make(map[string]*zap.SugaredLogger)
|
||||
}
|
||||
// TODO(taragu) remove this after removing all ClearAll() calls from serving and eventing
|
||||
func ClearAll() {}
|
||||
|
||||
// TestContextWithLogger returns a context with a logger to be used in tests
|
||||
func TestContextWithLogger(t *testing.T) context.Context {
|
||||
|
|
Loading…
Reference in New Issue