-
Notifications
You must be signed in to change notification settings - Fork 2
/
log.go
133 lines (116 loc) · 2.4 KB
/
log.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package testing
import (
"bytes"
"sync"
"testing"
"github.com/deixis/spine/log"
)
const (
// TC is the TRACE log constant
TC = "TRACE"
// WN is the WARNING log constant
WN = "WARN"
// ER is the ERROR log constant
ER = "ERRR"
)
// Logger is a simple Logger interface useful for tests
type Logger struct {
mu sync.RWMutex
t *testing.T
calldepth int
lines *counter
fields []log.Field
strict bool
}
// NewLogger creates a new logger
func NewLogger(t *testing.T, strict bool) log.Logger {
return &Logger{
t: t,
calldepth: 1,
lines: &counter{},
strict: strict,
}
}
func (l *Logger) l(s, tag, msg string, args ...log.Field) {
func() {
defer func() {
recover()
}()
l.t.Log(s, format(tag, msg, args...))
}()
l.inc(s)
}
func (l *Logger) inc(s string) {
l.lines.inc(s)
}
// Lines returns the number of log lines for the given severity
func (l *Logger) Lines(s string) int {
return l.lines.count(s)
}
func (l *Logger) Trace(tag, msg string, fields ...log.Field) { l.l(TC, tag, msg, fields...) }
func (l *Logger) Warning(tag, msg string, fields ...log.Field) { l.l(WN, tag, msg, fields...) }
func (l *Logger) Error(tag, msg string, fields ...log.Field) {
l.l(ER, tag, msg, fields...)
if l.strict {
l.t.Error(format(tag, msg, fields...)) // Make the tests fail
}
}
func (l *Logger) With(fields ...log.Field) log.Logger {
return &Logger{
t: l.t,
calldepth: l.calldepth,
lines: l.lines,
fields: append(l.fields, fields...),
strict: l.strict,
}
}
func (l *Logger) AddCalldepth(n int) log.Logger {
return &Logger{
t: l.t,
calldepth: l.calldepth + n,
lines: l.lines,
fields: l.fields,
strict: l.strict,
}
}
func (l *Logger) Close() error {
return nil
}
func format(tag, msg string, fields ...log.Field) string {
var b bytes.Buffer
b.WriteString(tag)
b.WriteString(" ")
b.WriteString(msg)
b.WriteString(" ")
for _, f := range fields {
k, v := f.KV()
b.WriteString(k)
b.WriteString("=")
b.WriteString(v)
b.WriteString(" ")
}
return b.String()
}
type counter struct {
mu sync.RWMutex
smap sync.Map
}
func (c *counter) inc(s string) {
c.mu.Lock()
defer c.mu.Unlock()
i, ok := c.smap.Load(s)
if ok {
c.smap.Store(s, i.(int)+1)
} else {
c.smap.Store(s, 1)
}
}
func (c *counter) count(s string) int {
c.mu.RLock()
defer c.mu.RUnlock()
v, ok := c.smap.Load(s)
if !ok {
return 0
}
return v.(int)
}