-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_loggly.go
109 lines (88 loc) · 2.19 KB
/
log_loggly.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
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
106
107
108
109
// Copyright 2021-2022, the SS project owners. All rights reserved.
// Please see the OWNERS and LICENSE files for details.
package ss
import (
"log"
logglylib "github.com/segmentio/go-loggly"
)
func newLogglyIfSet(
projectPackage,
module string,
config Config,
sentry sentry,
) (logDestination, error) {
if config.SS.Log.Loggly == "" {
return nil, nil
}
result := loggly{
messageChan: make(chan *LogMsg, 10),
syncChan: make(chan struct{}),
client: logglylib.New(config.SS.Log.Loggly),
sentry: sentry,
}
go result.runWriter()
return result, nil
}
type loggly struct {
messageChan chan *LogMsg
syncChan chan struct{}
client *logglylib.Client
sentry sentry
}
func (loggly) GetName() string { return "Loggly" }
func (l loggly) WriteDebug(message *LogMsg) error {
l.write(message)
return nil
}
func (l loggly) WriteInfo(message *LogMsg) error {
l.write(message)
return nil
}
func (l loggly) WriteWarn(message *LogMsg) error {
l.write(message)
return nil
}
func (l loggly) WriteError(message *LogMsg) error {
l.write(message)
return nil
}
func (l loggly) WritePanic(message *LogMsg) error {
l.write(message)
return nil
}
func (l loggly) Sync() error {
l.messageChan <- nil
<-l.syncChan
return nil
}
func (l loggly) write(message *LogMsg) { l.messageChan <- message }
func (l loggly) runWriter() {
defer func() {
if err := l.client.Flush(); err != nil {
log.Printf(
`Error: Failed to flush log %q record: %v`, l.GetName(), err)
l.sentry.CaptureMessage(
NewLogMsg(`failed to flush log %q record`, l.GetName()).AddErr(err))
}
}()
for {
message, isOpen := <-l.messageChan
if !isOpen {
break
}
if message == nil {
if err := l.client.Flush(); err != nil {
log.Printf(`Error: Failed to sync log %q record: %v`, l.GetName(), err)
l.sentry.CaptureMessage(
NewLogMsg(`failed to sync log %q record`, l.GetName()).AddErr(err))
}
l.syncChan <- struct{}{}
continue
}
if err := l.client.Send(message.MarshalMap()); err != nil {
log.Printf(`Error: Failed to write log %q record: %v`, l.GetName(), err)
l.sentry.CaptureMessage(
NewLogMsg(`failed to write log %q record`, l.GetName()).AddErr(err))
}
}
}