-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream-stdout.go
179 lines (164 loc) · 4.58 KB
/
stream-stdout.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package logger
import (
"bufio"
"fmt"
"io"
"os"
"sync"
"time"
"github.com/gildas/go-errors"
)
// StdoutStream is the Stream that writes to the standard output
type StdoutStream struct {
Converter Converter
FilterLevels LevelSet
Unbuffered bool
SourceInfo bool
output *bufio.Writer
writer io.Writer
flushFrequency time.Duration
mutex sync.Mutex
}
// GetFilterLevels gets the filter levels
//
// implements logger.Streamer
func (stream *StdoutStream) GetFilterLevels() LevelSet {
return stream.FilterLevels
}
// SetFilterLevel sets the filter level
//
// If present, the first parameter is the topic.
//
// If present, the second parameter is the scope.
//
// implements logger.FilterSetter
func (stream *StdoutStream) SetFilterLevel(level Level, parameters ...string) {
stream.mutex.Lock()
defer stream.mutex.Unlock()
if len(parameters) == 0 {
stream.FilterLevels.SetDefault(level)
} else if len(parameters) == 1 {
stream.FilterLevels.Set(level, parameters[0], "")
} else {
stream.FilterLevels.Set(level, parameters[0], parameters[1])
}
}
// 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 *StdoutStream) FilterMore() {
stream.mutex.Lock()
defer stream.mutex.Unlock()
stream.FilterLevels.SetDefault(stream.FilterLevels.GetDefault().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 *StdoutStream) FilterLess() {
stream.mutex.Lock()
defer stream.mutex.Unlock()
stream.FilterLevels.SetDefault(stream.FilterLevels.GetDefault().Previous())
}
// Write writes the given Record
//
// implements logger.Streamer
func (stream *StdoutStream) Write(record Record) error {
stream.mutex.Lock()
defer stream.mutex.Unlock()
if stream.writer == nil {
if stream.Converter == nil {
stream.Converter = GetConverterFromEnvironment()
}
if len(stream.FilterLevels) == 0 {
stream.FilterLevels = ParseLevelsFromEnvironment()
}
if stream.Unbuffered {
stream.output = nil
stream.writer = os.Stdout
} else {
stream.output = bufio.NewWriter(os.Stdout)
stream.writer = stream.output
stream.flushFrequency = GetFlushFrequencyFromEnvironment()
go stream.flushJob()
}
}
payload, err := stream.Converter.Convert(record).MarshalJSON()
if errors.Is(err, errors.JSONMarshalError) {
return err
} else if err != nil {
return errors.JSONMarshalError.Wrap(err)
}
if _, err = stream.writer.Write(payload); err != nil {
return errors.WithStack(err)
}
if _, err = stream.writer.Write([]byte("\n")); err != nil {
return errors.WithStack(err)
}
if GetLevelFromRecord(record) >= ERROR && stream.output != nil {
stream.output.Flush() // calling stream.Flush would Lock the mutex again and end up with a dead-lock
}
return nil
}
// ShouldLogSourceInfo tells if the source info should be logged
//
// implements logger.Streamer
func (stream *StdoutStream) ShouldLogSourceInfo() bool {
return stream.SourceInfo
}
// ShouldWrite tells if the given level should be written to this stream
//
// implements logger.Streamer
func (stream *StdoutStream) ShouldWrite(level Level, topic, scope string) bool {
return level.ShouldWrite(stream.FilterLevels.Get(topic, scope))
}
// Flush flushes the stream (makes sure records are actually written)
//
// implements logger.Streamer
func (stream *StdoutStream) Flush() {
if stream.output != nil {
stream.mutex.Lock()
defer stream.mutex.Unlock()
stream.output.Flush()
}
}
// Close closes the stream
//
// implements logger.Streamer
func (stream *StdoutStream) Close() {
if stream.output != nil {
stream.mutex.Lock()
defer stream.mutex.Unlock()
stream.output.Flush()
}
}
// String gets a string version
//
// implements fmt.Stringer
func (stream *StdoutStream) String() string {
format := bufferPool.Get()
defer bufferPool.Put(format)
if stream.Unbuffered {
format.WriteString("Unbuffered ")
}
format.WriteString("Stream to stdout")
if len(stream.FilterLevels) > 0 {
format.WriteString(", Filter: %s")
return fmt.Sprintf(format.String(), stream.FilterLevels)
}
return format.String()
}
func (stream *StdoutStream) flushJob() {
for range time.Tick(stream.flushFrequency) {
stream.Flush()
}
}