-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_observer.go
114 lines (95 loc) · 2.36 KB
/
log_observer.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
package apilog
import (
"bytes"
"encoding/json"
"io"
"strings"
"sync"
"time"
)
// ObservedLog is a concurrency-safe, ordered collection of observed Log(s).
type ObservedLog struct {
mu sync.RWMutex
logs []loggedLog
lvl Level
out Output
}
func (o *ObservedLog) Writer() io.Writer { return o }
func (o *ObservedLog) Output() Output { return o.out }
func (o *ObservedLog) Level() Level { return o.lvl }
func (o *ObservedLog) Wait(_ time.Duration) {}
func (o *ObservedLog) Flush(_ time.Duration) {}
func (o *ObservedLog) Write(p []byte) (n int, err error) {
m := make(map[string]any)
_ = json.Unmarshal(bytes.TrimSpace(p), &m)
var l loggedLog
// grab level if possible
if v, ok := m["level"].(string); ok {
l.level = ParseLevel(v)
}
// grab message if possible
if v, ok := m["msg"].(string); ok {
l.msg = v
}
// put the rest to context
l.context = m
o.mu.Lock()
o.logs = append(o.logs, l)
o.mu.Unlock()
return len(p), nil
}
// Len returns the number of items in the collection.
func (o *ObservedLog) Len() int {
o.mu.RLock()
n := len(o.logs)
o.mu.RUnlock()
return n
}
// All returns a copy of all the observed logs.
func (o *ObservedLog) All() []loggedLog {
o.mu.RLock()
ret := make([]loggedLog, len(o.logs))
copy(ret, o.logs)
o.mu.RUnlock()
return ret
}
// TakeAll returns a copy of all the observed logs, and truncates the observed
// slice.
func (o *ObservedLog) TakeAll() []loggedLog {
o.mu.Lock()
ret := o.logs
o.logs = nil
o.mu.Unlock()
return ret
}
// NewObserverWriter return new Writer implementer that write logs to memory
// and also return ObservedLog to help assert and check logged Log(s).
func NewObserverWriter(lvl Level, out Output) (Writer, *ObservedLog) {
ol := &ObservedLog{
lvl: lvl,
out: out,
mu: sync.RWMutex{},
}
return ol, ol
}
type loggedLog struct {
level Level
msg string
context map[string]any
}
// EqualLevel return true if given lvl is equal with level.
func (l *loggedLog) EqualLevel(lvl Level) bool {
return l.level == lvl
}
// EqualMsg return true if given s is equal with message.
func (l *loggedLog) EqualMsg(s string) bool {
return l.msg == s
}
// ContainMsg return true if message contain given s.
func (l *loggedLog) ContainMsg(s string) bool {
return strings.Contains(l.msg, s)
}
// Get grab a data from context using given key.
func (l *loggedLog) Get(k string) any {
return l.context[k]
}