-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
50 lines (38 loc) · 897 Bytes
/
writer.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
package loki
import (
"context"
"fmt"
"github.com/trea/loki-sink-for-zap"
"go.uber.org/zap"
"io"
"log"
)
func NewLokiWriter(endpoint string, labels map[string]interface{}, logger *zap.Logger) *LokiWriter {
ctx, cancel := context.WithCancel(context.Background())
ws := loki_sink_for_zap.NewLokiWriteSyncer(ctx)
ws.Url = endpoint
ws.Tags = labels
defer cancel()
return &LokiWriter{
rs: ws,
logger: logger,
}
}
type LokiWriter struct {
rs *loki_sink_for_zap.LokiWriteSyncer
logger *zap.Logger
}
func (l LokiWriter) Write(p []byte) (n int, err error) {
written, err := l.rs.Write(p)
if err != nil {
return written, err
}
if err := l.rs.Sync(); err != nil {
log.Printf(fmt.Sprintf("Writing log entry to Loki failed: %+v", err))
}
return written, nil
}
func (l LokiWriter) Close() error {
return l.rs.Close()
}
var _ io.WriteCloser = (*LokiWriter)(nil)