-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream-stderr.go
134 lines (120 loc) · 3.48 KB
/
stream-stderr.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
134
package logger
import (
"encoding/json"
"os"
"sync"
"github.com/gildas/go-errors"
)
// StderrStream is the Stream that writes to the standard error
type StderrStream struct {
*json.Encoder
Converter Converter
FilterLevel Level
FilterLevels TopicScopeLevels
mutex sync.Mutex
}
// SetFilterLevel sets the filter level
//
// implements logger.FilterSetter
func (stream *StderrStream) SetFilterLevel(level Level) {
stream.mutex.Lock()
defer stream.mutex.Unlock()
stream.FilterLevel = level
}
// SetFilterLevelIfUnset sets the filter level if not set already
//
// implements logger.FilterSetter
func (stream *StderrStream) SetFilterLevelIfUnset(level Level) {
stream.mutex.Lock()
defer stream.mutex.Unlock()
if stream.FilterLevel == UNSET {
stream.FilterLevel = level
}
}
// SetFilterLevelForTopic sets the filter level for a given topic
//
// implements logger.FilterSetter
func (stream *StderrStream) SetFilterLevelForTopic(level Level, topic string) {
stream.mutex.Lock()
defer stream.mutex.Unlock()
stream.FilterLevels.Set(topic, "", level)
}
// SetFilterLevelForTopicAndScope sets the filter level for a given topic
//
// implements logger.FilterSetter
func (stream *StderrStream) SetFilterLevelForTopicAndScope(level Level, topic, scope string) {
stream.mutex.Lock()
defer stream.mutex.Unlock()
stream.FilterLevels.Set(topic, scope, level)
}
// FilterMore tells the stream to filter more
//
// The stream will filter more if it is not already at the highest level.
// Which means less log messages will be written to the stream
//
// Example: if the stream is at DEBUG, it will be filtering at INFO
//
// implements logger.FilterModifier
func (stream *StderrStream) FilterMore() {
stream.mutex.Lock()
defer stream.mutex.Unlock()
stream.FilterLevel = stream.FilterLevel.Next()
}
// FilterLess tells the stream to filter less
//
// The stream will filter less if it is not already at the lowest level.
// Which means more log messages will be written to the stream
//
// Example: if the stream is at INFO, it will be filtering at DEBUG
//
// implements logger.FilterModifier
func (stream *StderrStream) FilterLess() {
stream.mutex.Lock()
defer stream.mutex.Unlock()
stream.FilterLevel = stream.FilterLevel.Previous()
}
// Write writes the given Record
//
// implements logger.Streamer
func (stream *StderrStream) Write(record Record) error {
stream.mutex.Lock()
defer stream.mutex.Unlock()
if stream.Encoder == nil {
stream.Encoder = json.NewEncoder(os.Stderr)
if stream.FilterLevel == UNSET {
stream.FilterLevel = GetLevelFromEnvironment()
}
}
if stream.Converter == nil {
stream.Converter = GetConverterFromEnvironment()
}
if err := stream.Encoder.Encode(stream.Converter.Convert(record)); err != nil {
return errors.JSONMarshalError.Wrap(err)
}
return nil
}
// ShouldWrite tells if the given level should be written to this stream
//
// implements logger.Streamer
func (stream *StderrStream) ShouldWrite(level Level, topic, scope string) bool {
if _level, found := stream.FilterLevels.Get(topic, scope); found {
return level.ShouldWrite(_level)
}
return level.ShouldWrite(stream.FilterLevel)
}
// Flush flushes the stream (makes sure records are actually written)
//
// implements logger.Streamer
func (stream *StderrStream) Flush() {
}
// Close closes the stream
//
// implements logger.Streamer
func (stream *StderrStream) Close() {
}
// String gets a string version
//
// implements fmt.Stringer
func (stream *StderrStream) String() string {
return "Stream to stderr"
}