-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
test_logger.go
51 lines (45 loc) · 1.55 KB
/
test_logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package logger
import (
"testing"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest"
"go.uber.org/zap/zaptest/observer"
"github.com/smartcontractkit/chainlink/core/config/envvar"
)
// TestLogger creates a logger that directs output to PrettyConsole configured
// for test output, and to the buffer testMemoryLog. t is optional.
// Log level is derived from the LOG_LEVEL env var.
func TestLogger(tb testing.TB) SugaredLogger {
return testLogger(tb, nil)
}
// TestLoggerObserved creates a logger with an observer that can be used to
// test emitted logs at the given level or above
func TestLoggerObserved(tb testing.TB, lvl zapcore.Level) (Logger, *observer.ObservedLogs) {
observedZapCore, observedLogs := observer.New(lvl)
return testLogger(tb, observedZapCore), observedLogs
}
// testLogger returns a new SugaredLogger for tests. core is optional.
func testLogger(tb testing.TB, core zapcore.Core) SugaredLogger {
ll, invalid := envvar.LogLevel.Parse()
a := zap.NewAtomicLevelAt(ll)
opts := []zaptest.LoggerOption{zaptest.Level(a)}
zapOpts := []zap.Option{zap.AddCaller(), zap.AddStacktrace(zapcore.ErrorLevel)}
if core != nil {
zapOpts = append(zapOpts, zap.WrapCore(func(c zapcore.Core) zapcore.Core {
return zapcore.NewTee(c, core)
}))
}
opts = append(opts, zaptest.WrapOptions(zapOpts...))
l := &zapLogger{
level: a,
SugaredLogger: zaptest.NewLogger(tb, opts...).Sugar(),
}
if invalid != "" {
l.Error(invalid)
}
if tb == nil {
return Sugared(l)
}
return Sugared(l.Named(verShaNameStatic()).Named(tb.Name()))
}