/
test_logger.go
92 lines (78 loc) · 2.22 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package logger
// Based on https://stackoverflow.com/a/52737940
import (
"bytes"
"log"
"net/url"
"sync"
"github.com/fatih/color"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// MemorySink implements zap.Sink by writing all messages to a buffer.
type MemorySink struct {
m sync.Mutex
b bytes.Buffer
}
var _ zap.Sink = &MemorySink{}
func (s *MemorySink) Write(p []byte) (n int, err error) {
s.m.Lock()
defer s.m.Unlock()
return s.b.Write(p)
}
// Close is a dummy method to satisfy the zap.Sink interface
func (s *MemorySink) Close() error { return nil }
// Sync is a dummy method to satisfy the zap.Sink interface
func (s *MemorySink) Sync() error { return nil }
// String returns the full log contents, as a string
func (s *MemorySink) String() string {
s.m.Lock()
defer s.m.Unlock()
return s.b.String()
}
var testMemoryLog *MemorySink
var createSinkOnce sync.Once
func registerMemorySink() {
testMemoryLog = &MemorySink{m: sync.Mutex{}, b: bytes.Buffer{}}
if err := zap.RegisterSink("memory", func(*url.URL) (zap.Sink, error) {
return PrettyConsole{Sink: testMemoryLog}, nil
}); err != nil {
panic(err)
}
}
func MemoryLogTestingOnly() *MemorySink {
createSinkOnce.Do(registerMemorySink)
return testMemoryLog
}
// CreateTestLogger creates a logger that directs output to PrettyConsole
// configured for test output, and to the buffer testMemoryLog.
func CreateTestLogger(lvl zapcore.Level) *Logger {
_ = MemoryLogTestingOnly() // Make sure memory log is created
color.NoColor = false
config := zap.NewProductionConfig()
config.Level.SetLevel(lvl)
config.OutputPaths = []string{"pretty://console", "memory://"}
zl, err := config.Build(zap.AddCallerSkip(1))
if err != nil {
log.Fatal(err)
}
return &Logger{
SugaredLogger: zl.Sugar(),
}
}
// CreateMemoryTestLogger creates a logger that only directs output to the
// buffer testMemoryLog.
func CreateMemoryTestLogger(lvl zapcore.Level) *Logger {
_ = MemoryLogTestingOnly() // Make sure memory log is created
color.NoColor = true
config := zap.NewProductionConfig()
config.Level.SetLevel(lvl)
config.OutputPaths = []string{"memory://"}
zl, err := config.Build(zap.AddCallerSkip(1))
if err != nil {
log.Fatal(err)
}
return &Logger{
SugaredLogger: zl.Sugar(),
}
}