forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
286 lines (254 loc) · 8 KB
/
logging.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright 2016-2017 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package common
import (
"fmt"
"log/syslog"
"net"
"os"
"regexp"
"strconv"
"time"
"github.com/Sirupsen/logrus"
"github.com/Sirupsen/logrus/hooks/syslog"
"github.com/bshuster-repo/logrus-logstash-hook"
"github.com/evalphobia/logrus_fluent"
)
const (
fAddr = "fluentd.address"
fTag = "fluentd.tag"
fLevel = "fluentd.level"
SLevel = "syslog.level"
lAddr = "logstash.address"
lLevel = "logstash.level"
lProtocol = "logstash.protocol"
Syslog = "syslog"
Fluentd = "fluentd"
Logstash = "logstash"
)
// syslogOpts is the set of supported options for syslog configuration.
var syslogOpts = map[string]bool{
"syslog.level": true,
}
// fluentDOpts is the set of supported options for fluentD configuration.
var fluentDOpts = map[string]bool{
fAddr: true,
fTag: true,
fLevel: true,
}
// logstashOpts is the set of supported options for logstash configuration.
var logstashOpts = map[string]bool{
lAddr: true,
lLevel: true,
lProtocol: true,
}
// syslogLevelMap maps logrus.Level values to syslog.Priority levels.
var syslogLevelMap = map[logrus.Level]syslog.Priority{
logrus.PanicLevel: syslog.LOG_ALERT,
logrus.FatalLevel: syslog.LOG_CRIT,
logrus.ErrorLevel: syslog.LOG_ERR,
logrus.WarnLevel: syslog.LOG_WARNING,
logrus.InfoLevel: syslog.LOG_INFO,
logrus.DebugLevel: syslog.LOG_DEBUG,
}
// setFireLevels returns a slice of logrus.Level values higher in priority
// and including level, excluding any levels lower in priority.
func setFireLevels(level logrus.Level) []logrus.Level {
switch level {
case logrus.PanicLevel:
return []logrus.Level{logrus.PanicLevel}
case logrus.FatalLevel:
return []logrus.Level{logrus.PanicLevel, logrus.FatalLevel}
case logrus.ErrorLevel:
return []logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel}
case logrus.WarnLevel:
return []logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel, logrus.WarnLevel}
case logrus.InfoLevel:
return []logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel, logrus.WarnLevel, logrus.InfoLevel}
case logrus.DebugLevel:
return []logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel, logrus.WarnLevel, logrus.InfoLevel, logrus.DebugLevel}
default:
logrus.Infof("logrus level %v is not supported at this time; defaulting to info level", level)
return []logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel, logrus.WarnLevel, logrus.InfoLevel}
}
}
// SetupLogging sets up each logging service provided in loggers and configures
// each logger with the provided logOpts.
func SetupLogging(loggers []string, logOpts map[string]string, tag string, debug bool) error {
// FIXME: Disabled for now
//setupFormatter()
// Set default logger to output to stdout if no loggers are provided.
if len(loggers) == 0 {
logrus.SetOutput(os.Stdout)
}
if debug {
logrus.SetLevel(logrus.DebugLevel)
} else {
logrus.SetLevel(logrus.InfoLevel)
}
// Iterate through all provided loggers and configure them according
// to user-provided settings.
for _, logger := range loggers {
valuesToValidate := getLogDriverConfig(logger, logOpts)
switch logger {
case Syslog:
valuesToValidate := getLogDriverConfig(Syslog, logOpts)
err := validateOpts(Syslog, valuesToValidate, syslogOpts)
if err != nil {
return err
}
setupSyslog(valuesToValidate, tag, debug)
case Fluentd:
err := validateOpts(logger, valuesToValidate, fluentDOpts)
if err != nil {
return err
}
setupFluentD(valuesToValidate, debug)
//TODO - need to finish logstash integration.
/*case Logstash:
fmt.Printf("SetupLogging: in logstash case\n")
err := validateOpts(logger, valuesToValidate, logstashOpts)
fmt.Printf("SetupLogging: validating options for logstash complete\n")
if err != nil {
fmt.Printf("SetupLogging: error validating logstash opts %v\n", err.Error())
return err
}
fmt.Printf("SetupLogging: about to setup logstash\n")
setupLogstash(valuesToValidate)
*/
default:
return fmt.Errorf("provided log driver %q is not a supported log driver", logger)
}
}
return nil
}
// setupSyslog sets up and configures syslog with the provided options in
// logOpts. If some options are not provided, sensible defaults are used.
func setupSyslog(logOpts map[string]string, tag string, debug bool) {
logLevel, ok := logOpts[SLevel]
if !ok {
if debug {
logLevel = "debug"
} else {
logLevel = "info"
}
}
//Validate provided log level.
level, err := logrus.ParseLevel(logLevel)
if err != nil {
logrus.Fatal(err)
}
logrus.SetLevel(level)
// Create syslog hook.
h, err := logrus_syslog.NewSyslogHook("", "", syslogLevelMap[level], tag)
if err != nil {
logrus.Fatal(err)
}
logrus.AddHook(h)
}
// setupFormatter sets up the text formatting for logs output by logrus.
func setupFormatter() {
fileFormat := new(logrus.TextFormatter)
fileFormat.DisableColors = true
switch os.Getenv("INITSYSTEM") {
case "SYSTEMD":
fileFormat.DisableTimestamp = true
fileFormat.FullTimestamp = true
default:
fileFormat.TimestampFormat = time.RFC3339
}
logrus.SetFormatter(fileFormat)
}
// setupFluentD sets up and configures FluentD with the provided options in
// logOpts. If some options are not provided, sensible defaults are used.
func setupFluentD(logOpts map[string]string, debug bool) {
//If no logging level set for fluentd, use debug value if it is set.
// Logging level set for fluentd takes precedence over debug flag
// fluent.level provided.
logLevel, ok := logOpts[fLevel]
if !ok {
if debug {
logLevel = "debug"
} else {
logLevel = "info"
}
}
level, err := logrus.ParseLevel(logLevel)
if err != nil {
logrus.Fatal(err)
}
hostAndPort, ok := logOpts[fAddr]
if !ok {
hostAndPort = "localhost:24224"
}
host, strPort, err := net.SplitHostPort(hostAndPort)
port, err := strconv.Atoi(strPort)
if err != nil {
logrus.Fatal(err)
}
h, err := logrus_fluent.New(host, port)
if err != nil {
logrus.Fatal(err)
}
tag, ok := logOpts[fTag]
if ok {
h.SetTag(tag)
}
// set custom fire level
h.SetLevels(setFireLevels(level))
logrus.AddHook(h)
}
// setupLogstash sets up and configures Logstash with the provided options in
// logOpts. If some options are not provided, sensible defaults are used.
/// TODO fix me later - needs to be tested with a working logstash setup.
func setupLogstash(logOpts map[string]string) {
hostAndPort, ok := logOpts[lAddr]
if !ok {
hostAndPort = "172.17.0.2:999"
}
protocol, ok := logOpts[lProtocol]
if !ok {
protocol = "tcp"
}
h, err := logrustash.NewHook(protocol, hostAndPort, "cilium")
if err != nil {
logrus.Fatal(err)
}
logrus.AddHook(h)
}
// validateOpts iterates through all of the keys in logOpts, and errors out if
// the key in logOpts is not a key in supportedOpts.
func validateOpts(logDriver string, logOpts map[string]string, supportedOpts map[string]bool) error {
for k := range logOpts {
if !supportedOpts[k] {
return fmt.Errorf("provided configuration value %q is not supported as a logging option for log driver %s", k, logDriver)
}
}
return nil
}
// getLogDriverConfig returns a map containing the key-value pairs that start
// with string logDriver from map logOpts.
func getLogDriverConfig(logDriver string, logOpts map[string]string) map[string]string {
keysToValidate := make(map[string]string)
for k, v := range logOpts {
ok, err := regexp.MatchString(logDriver+".*", k)
if err != nil {
logrus.Fatal(err)
}
if ok {
keysToValidate[k] = v
}
}
return keysToValidate
}