-
Notifications
You must be signed in to change notification settings - Fork 17
/
senders.go
103 lines (86 loc) · 2.97 KB
/
senders.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
package logkeeper
import (
"context"
"os"
"strings"
"time"
"github.com/mongodb/amboy/logger"
"github.com/mongodb/amboy/queue"
"github.com/mongodb/grip/level"
"github.com/mongodb/grip/send"
"github.com/pkg/errors"
)
func GetSender(ctx context.Context, fn string) (send.Sender, error) {
const (
name = "logkeeper"
interval = 20 * time.Second
bufferCount = 100
)
var (
err error
sender send.Sender
senders []send.Sender
baseLevel = send.LevelInfo{Default: level.Info, Threshold: level.Debug}
)
// configure remote logging services first if the environment
// variables are specified.
if splunk := send.GetSplunkConnectionInfo(); splunk.Populated() {
sender, err = send.NewSplunkLogger(name, splunk, send.LevelInfo{Default: level.Info, Threshold: level.Info})
if err != nil {
return nil, errors.Wrap(err, "creating the Splunk logger")
}
bufferedSender, err := send.NewBufferedSender(ctx, sender, send.BufferedSenderOptions{FlushInterval: interval, BufferSize: bufferCount})
if err != nil {
return nil, errors.Wrap(err, "creating the buffered Splunk logger")
}
senders = append(senders, bufferedSender)
}
// configure slack logger for alerts and panics
if token := os.Getenv("GRIP_SLACK_CLIENT_TOKEN"); token != "" {
channel := os.Getenv("GRIP_SLACK_CHANNEL")
if channel == "" {
channel = "#evergreen-ops-alerts"
}
if !strings.HasPrefix(channel, "#") {
channel = "#" + channel
}
opts := &send.SlackOptions{
Name: name,
Channel: channel,
AllFields: true,
BasicMetadata: true,
}
sender, err = send.NewSlackLogger(opts, token, send.LevelInfo{Default: level.Error, Threshold: level.Critical})
if err != nil {
return nil, errors.Wrap(err, "problem creating the slack sender")
}
localQueue := queue.NewLocalLimitedSize(4, 2048)
if err = localQueue.Start(ctx); err != nil {
return nil, errors.Wrap(err, "starting local queue for Splunk logger")
}
senders = append(senders, logger.MakeQueueSender(ctx, localQueue, sender))
}
// setup file logger, defaulting first to the system logger,
// to standard output (or not at all) if specified, and
// finally to the file as specified.
if fn == "" {
sender = getSystemLogger()
if err = sender.SetLevel(send.LevelInfo{Default: level.Info, Threshold: level.Debug}); err != nil {
return nil, errors.Wrap(err, "problem setting level for local system sender")
}
senders = append(senders, sender)
} else if fn == "LOCAL" || fn == "--" || fn == "stdout" {
sender, err = send.NewNativeLogger(name, baseLevel)
if err != nil {
return nil, errors.Wrap(err, "problem creating a native console logger")
}
senders = append(senders, sender)
} else if (fn != "NONE") && (fn != "SKIP") {
sender, err = send.NewFileLogger("logkeeper", fn, baseLevel)
if err != nil {
return nil, errors.Wrap(err, "problem creating a file logger")
}
senders = append(senders, sender)
}
return send.NewConfiguredMultiSender(senders...), nil
}