Repository navigation
Expand file tree
/
Copy pathsink.go
More file actions
105 lines (93 loc) · 2.34 KB
/
Copy pathsink.go
File metadata and controls
105 lines (93 loc) · 2.34 KB
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
package logger
import (
"io"
"sync"
)
// Sink is a terminal destination. Each sink owns its level + encoder so a
// fan-out can send the same record to console(debug,pretty) and
// file(info,json) simultaneously. A sink that errors must not block or kill
// sibling sinks (see Fanout).
type Sink interface {
// Emit writes r if r.Level >= the sink's level.
Emit(r *Record) error
// Sync flushes buffered data.
Sync() error
// Close flushes and releases resources.
Close() error
}
// WriterSink adapts any io.Writer + Encoder into a Sink with its own level.
type WriterSink struct {
W io.Writer
Enc Encoder
MinLvl Leveler
mu sync.Mutex
}
// NewWriterSink builds a WriterSink. minLevel may be a *LevelVar for runtime
// control or a constant Level.
func NewWriterSink(w io.Writer, enc Encoder, minLevel Level) *WriterSink {
return &WriterSink{W: w, Enc: enc, MinLvl: staticLevel(minLevel)}
}
// Emit implements Sink.
func (s *WriterSink) Emit(r *Record) error {
if r.Level < s.MinLvl.Level() {
return nil
}
buf := getBuffer()
s.Enc.Encode(buf, r)
s.mu.Lock()
_, err := s.W.Write(buf.b)
s.mu.Unlock()
putBuffer(buf)
return err
}
// Sync implements Sink (best-effort flush for Syncer writers).
func (s *WriterSink) Sync() error {
if sy, ok := s.W.(interface{ Sync() error }); ok {
return sy.Sync()
}
return nil
}
// Close implements Sink.
func (s *WriterSink) Close() error {
if c, ok := s.W.(io.Closer); ok {
return c.Close()
}
return nil
}
// Fanout broadcasts a record to N sinks with per-sink failure isolation: one
// sink returning an error never prevents the others from receiving the record.
type Fanout struct {
sinks []Sink
OnError func(Sink, error)
}
// NewFanout groups sinks.
func NewFanout(sinks ...Sink) *Fanout { return &Fanout{sinks: sinks} }
// Emit implements Sink.
func (f *Fanout) Emit(r *Record) error {
for _, s := range f.sinks {
if err := s.Emit(r); err != nil && f.OnError != nil {
f.OnError(s, err)
}
}
return nil
}
// Sync implements Sink.
func (f *Fanout) Sync() error {
var first error
for _, s := range f.sinks {
if err := s.Sync(); err != nil && first == nil {
first = err
}
}
return first
}
// Close implements Sink.
func (f *Fanout) Close() error {
var first error
for _, s := range f.sinks {
if err := s.Close(); err != nil && first == nil {
first = err
}
}
return first
}