-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream_stderr.go
67 lines (57 loc) · 1.55 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
package logger
import (
"encoding/json"
"os"
"sync"
"github.com/pkg/errors"
)
// StderrStream is the Stream that writes to the standard output
type StderrStream struct {
*json.Encoder
Converter Converter
FilterLevel Level
mutex sync.Mutex
}
// SetFilterLevel sets the filter level
func (stream *StderrStream) SetFilterLevel(level Level) Streamer {
stream.mutex.Lock()
defer stream.mutex.Unlock()
stream.FilterLevel = level
return stream
}
// Write writes the given Record
// implements logger.Stream
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.WithStack(err)
}
return nil
}
// ShouldWrite tells if the given level should be written to this stream
// implements logger.Stream
func (stream *StderrStream) ShouldWrite(level Level) bool {
return level.ShouldWrite(stream.FilterLevel)
}
// Flush flushes the stream (makes sure records are actually written)
// implements logger.Stream
func (stream *StderrStream) Flush() {
}
// Close closes the stream
func (stream *StderrStream) Close() {
}
// String gets a string version
// implements the fmt.Stringer interface
func (stream *StderrStream) String() string {
return "Stream to stderr"
}