-
Notifications
You must be signed in to change notification settings - Fork 117
/
sink.go
55 lines (44 loc) · 1023 Bytes
/
sink.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
package activity
import (
"context"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Sink is used by a bufferedClient to flush collected Event-s.
type Sink interface {
Sink(ctx context.Context, events []Event) error
Close() error
}
type NoopSink struct{}
func NewNoopSink() *NoopSink {
return &NoopSink{}
}
func (n *NoopSink) Sink(_ context.Context, _ []Event) error {
return nil
}
func (n *NoopSink) Close() error {
return nil
}
type ConsoleSink struct {
logger *zap.Logger
level zapcore.Level
}
// NewConsoleSink might be used for a local run
func NewConsoleSink(logger *zap.Logger, level zapcore.Level) *ConsoleSink {
return &ConsoleSink{logger: logger, level: level}
}
func (s *ConsoleSink) Sink(_ context.Context, events []Event) error {
if s.logger.Core().Enabled(s.level) {
for _, e := range events {
jsonEvent, err := e.Marshal()
if err != nil {
return err
}
s.logger.Log(s.level, string(jsonEvent))
}
}
return nil
}
func (s *ConsoleSink) Close() error {
return nil
}