-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream-multistream.go
59 lines (50 loc) · 1.36 KB
/
stream-multistream.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
package logger
import "github.com/gildas/go-errors"
// MultiStream is the Stream that writes to several streams
type MultiStream struct {
streams []Streamer
}
// SetFilterLevel sets the filter level of all streams
func (stream *MultiStream) SetFilterLevel(level Level) Streamer {
for _, s := range stream.streams {
s.SetFilterLevel(level)
}
return stream
}
// Write writes the given Record
func (stream *MultiStream) Write(record Record) error {
// implements logger.Stream
errs := errors.MultiError{}
for _, s := range stream.streams {
if err := s.Write(record); err != nil {
_ = errs.Append(errors.WithStack(err))
}
}
return errs.AsError()
}
// ShouldWrite tells if the given level should be written to this stream
func (stream *MultiStream) ShouldWrite(level Level) bool {
// implements logger.Stream
return true
}
// Flush flushes the stream (makes sure records are actually written)
func (stream *MultiStream) Flush() {
// implements logger.Stream
for _, s := range stream.streams {
s.Flush()
}
}
// Close closes the stream
func (stream *MultiStream) Close() {
for _, s := range stream.streams {
s.Close()
}
}
// String gets a string version
func (stream MultiStream) String() string {
// implements the fmt.Stringer interface
return "MultiStream"
}
func CreateMultiStream(streams ...Streamer) Streamer {
return &MultiStream{streams: streams}
}