-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream_stderr.go
47 lines (39 loc) · 1.07 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
package logger
import (
"encoding/json"
"os"
"github.com/pkg/errors"
)
// StderrStream is the Stream that writes to the standard output
type StderrStream struct {
*json.Encoder
FilterLevel Level
}
// Write writes the given Record
// implements logger.Stream
func (stream *StderrStream) Write(record Record) error {
if stream.Encoder == nil {
stream.Encoder = json.NewEncoder(os.Stderr)
if stream.FilterLevel == 0 {
stream.FilterLevel = GetLevelFromEnvironment()
}
}
if err := stream.Encoder.Encode(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() {
}
// String gets a string version
// implements the fmt.Stringer interface
func (stream StderrStream) String() string {
return "Stream to stderr"
}