-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
98 lines (78 loc) · 1.71 KB
/
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
93
94
95
96
97
98
package clogger
import (
"os"
"sync"
)
var (
log Logger = NewDefaultLogger()
)
type Logger interface {
StreamLogEntry(e *LogEntry)
SetLogEntryOptions(opts ...LogEntryOption)
LogEntryOptions() []LogEntryOption
StreamEvent(ev *Event)
SetEventOptions(opts ...EventOption)
EventOptions() []EventOption
SetEncoder(enc Encoder)
}
type DefaultLogger struct {
eventOpts []EventOption
logEntryOpts []LogEntryOption
mu sync.Mutex
enc Encoder
}
func (l *DefaultLogger) SetEncoder(enc Encoder) {
l.enc = enc
}
func NewDefaultLogger() Logger {
return &DefaultLogger{
eventOpts: make([]EventOption, 0),
logEntryOpts: make([]LogEntryOption, 0),
mu: sync.Mutex{},
enc: &JSONEncoder{},
}
}
func (l *DefaultLogger) SetEventOptions(opts ...EventOption) {
l.mu.Lock()
defer l.mu.Unlock()
l.eventOpts = opts
}
func (l *DefaultLogger) SetLogEntryOptions(opts ...LogEntryOption) {
l.mu.Lock()
defer l.mu.Unlock()
l.logEntryOpts = opts
}
func (l *DefaultLogger) EventOptions() []EventOption {
l.mu.Lock()
defer l.mu.Unlock()
return l.eventOpts
}
func (l *DefaultLogger) LogEntryOptions() []LogEntryOption {
l.mu.Lock()
defer l.mu.Unlock()
return l.logEntryOpts
}
func (l *DefaultLogger) StreamLogEntry(entry *LogEntry) {
output, err := l.enc.EncodeLogEntry(entry)
if err != nil {
return
}
switch {
case entry.severity > SeverityInfo:
_, _ = os.Stderr.Write(output)
default:
_, _ = os.Stdout.Write(output)
}
}
func (l *DefaultLogger) StreamEvent(event *Event) {
output, err := l.enc.EncodeEvent(event)
if err != nil {
return
}
switch {
case event.severity > SeverityInfo:
_, _ = os.Stderr.Write(output)
default:
_, _ = os.Stdout.Write(output)
}
}