forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
346 lines (292 loc) · 9.4 KB
/
log.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-2019 Datadog, Inc.
package config
import (
"crypto/tls"
"errors"
"fmt"
"net"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/ninnemana/datadog-agent/pkg/util/log"
"github.com/cihub/seelog"
)
// LoggerName specifies the name of an instantiated logger.
type LoggerName string
const logDateFormat = "2006-01-02 15:04:05 MST" // see time.Format for format syntax
var syslogTLSConfig *tls.Config
// BuildCommonFormat returns the log common format seelog string
func BuildCommonFormat(loggerName LoggerName) string {
return fmt.Sprintf("%%Date(%s) | %s | %%LEVEL | (%%ShortFilePath:%%Line in %%FuncShort) | %%Msg%%n", logDateFormat, loggerName)
}
// BuildJSONFormat returns the log JSON format seelog string
func BuildJSONFormat(loggerName LoggerName) string {
return fmt.Sprintf("{"agent":"%s","time":"%%Date(%s)","level":"%%LEVEL","file":"%%ShortFilePath","line":"%%Line","func":"%%FuncShort","msg":"%%Msg"}%%n", strings.ToLower(string(loggerName)), logDateFormat)
}
func getSyslogTLSKeyPair() (*tls.Certificate, error) {
var syslogTLSKeyPair *tls.Certificate
if Datadog.IsSet("syslog_pem") && Datadog.IsSet("syslog_key") {
cert := Datadog.GetString("syslog_pem")
key := Datadog.GetString("syslog_key")
if cert == "" && key == "" {
return nil, nil
} else if cert == "" || key == "" {
return nil, fmt.Errorf("Both a PEM certificate and key must be specified to enable TLS")
}
keypair, err := tls.LoadX509KeyPair(cert, key)
if err != nil {
return nil, err
}
syslogTLSKeyPair = &keypair
}
return syslogTLSKeyPair, nil
}
// SetupLogger sets up a logger with the specified logger name and log level
// if a non empty logFile is provided, it will also log to the file
// a non empty syslogURI will enable syslog, and format them following RFC 5424 if specified
// you can also specify to log to the console and in JSON format
func SetupLogger(loggerName LoggerName, logLevel, logFile, syslogURI string, syslogRFC, logToConsole, jsonFormat bool) error {
var syslog bool
var useTLS bool
if syslogURI != "" { // non-blank uri enables syslog
syslog = true
syslogTLSKeyPair, err := getSyslogTLSKeyPair()
if err != nil {
return err
}
if syslogTLSKeyPair != nil {
useTLS = true
syslogTLSConfig = &tls.Config{
Certificates: []tls.Certificate{*syslogTLSKeyPair},
InsecureSkipVerify: Datadog.GetBool("syslog_tls_verify"),
}
}
}
seelogLogLevel := strings.ToLower(logLevel)
if seelogLogLevel == "warning" { // Common gotcha when used to agent5
seelogLogLevel = "warn"
}
configTemplate := fmt.Sprintf(`<seelog minlevel="%s">`, seelogLogLevel)
formatID := "common"
if jsonFormat {
formatID = "json"
}
configTemplate += fmt.Sprintf(`<outputs formatid="%s">`, formatID)
if logToConsole {
configTemplate += `<console />`
}
if logFile != "" {
configTemplate += fmt.Sprintf(`<rollingfile type="size" filename="%s" maxsize="%d" maxrolls="%d" />`, logFile, Datadog.GetSizeInBytes("log_file_max_size"), Datadog.GetInt("log_file_max_rolls"))
}
if syslog {
var syslogTemplate string
if syslogURI != "" {
syslogTemplate = fmt.Sprintf(
`<custom name="syslog" formatid="syslog-%s" data-uri="%s" data-tls="%v" />`,
formatID,
syslogURI,
useTLS,
)
} else {
syslogTemplate = fmt.Sprintf(`<custom name="syslog" formatid="syslog-%s" />`, formatID)
}
configTemplate += syslogTemplate
}
configTemplate += fmt.Sprintf(`</outputs>
<formats>
<format id="json" format="%s"/>
<format id="common" format="%s"/>
<format id="syslog-json" format="%%CustomSyslogHeader(20,`+strconv.FormatBool(syslogRFC)+`){"agent":"%s","level":"%%LEVEL","relfile":"%%ShortFilePath","line":"%%Line","msg":"%%Msg"}%%n"/>
<format id="syslog-common" format="%%CustomSyslogHeader(20,`+strconv.FormatBool(syslogRFC)+`) %s | %%LEVEL | (%%ShortFilePath:%%Line in %%FuncShort) | %%Msg%%n" />
</formats>
</seelog>`,
BuildJSONFormat(loggerName),
BuildCommonFormat(loggerName),
strings.ToLower(string(loggerName)),
loggerName,
)
logger, err := seelog.LoggerFromConfigAsString(configTemplate)
if err != nil {
return err
}
seelog.ReplaceLogger(logger)
log.SetupDatadogLogger(logger, seelogLogLevel)
return nil
}
// ErrorLogWriter is a Writer that logs all written messages with the global seelog logger
// at an error level
type ErrorLogWriter struct {
AdditionalDepth int
}
func (s *ErrorLogWriter) Write(p []byte) (n int, err error) {
log.ErrorStackDepth(s.AdditionalDepth, strings.TrimSpace(string(p)))
return len(p), nil
}
var levelToSyslogSeverity = map[seelog.LogLevel]int{
// Mapping to RFC 5424 where possible
seelog.TraceLvl: 7,
seelog.DebugLvl: 7,
seelog.InfoLvl: 6,
seelog.WarnLvl: 4,
seelog.ErrorLvl: 3,
seelog.CriticalLvl: 2,
seelog.Off: 7,
}
func createSyslogHeaderFormatter(params string) seelog.FormatterFunc {
facility := 20
rfc := false
ps := strings.Split(params, ",")
if len(ps) == 2 {
i, err := strconv.Atoi(ps[0])
if err == nil && i >= 0 && i <= 23 {
facility = i
}
rfc = (ps[1] == "true")
} else {
fmt.Printf("badly formatted syslog header parameters - using defaults")
}
pid := os.Getpid()
appName := filepath.Base(os.Args[0])
if rfc { // RFC 5424
return func(message string, level seelog.LogLevel, context seelog.LogContextInterface) interface{} {
return fmt.Sprintf("<%d>1 %s %d - -", facility*8+levelToSyslogSeverity[level], appName, pid)
}
}
// otherwise old-school logging
return func(message string, level seelog.LogLevel, context seelog.LogContextInterface) interface{} {
return fmt.Sprintf("<%d>%s[%d]:", facility*8+levelToSyslogSeverity[level], appName, pid)
}
}
// SyslogReceiver implements seelog.CustomReceiver
type SyslogReceiver struct {
enabled bool
uri *url.URL
tls bool
conn net.Conn
}
func getSyslogConnection(uri *url.URL, secure bool) (net.Conn, error) {
var conn net.Conn
var err error
// local
localNetNames := []string{"unixgram", "unix"}
if uri == nil {
addrs := []string{"/dev/log", "/var/run/syslog", "/var/run/log"}
for _, netName := range localNetNames {
for _, addr := range addrs {
conn, err = net.Dial(netName, addr)
if err == nil { // on success
return conn, nil
}
}
}
} else {
switch uri.Scheme {
case "unix", "unixgram":
fmt.Printf("Trying to connecto to: %s", uri.Path)
for _, netName := range localNetNames {
conn, err = net.Dial(netName, uri.Path)
if err == nil {
break
}
}
case "udp":
conn, err = net.Dial(uri.Scheme, uri.Host)
case "tcp":
if secure {
conn, err = tls.Dial("tcp", uri.Host, syslogTLSConfig)
} else {
conn, err = net.Dial("tcp", uri.Host)
}
}
if err == nil {
return conn, nil
}
}
return nil, errors.New("Unable to connect to syslog")
}
// ReceiveMessage process current log message
func (s *SyslogReceiver) ReceiveMessage(message string, level seelog.LogLevel, context seelog.LogContextInterface) error {
if !s.enabled {
return nil
}
if s.conn != nil {
_, err := s.conn.Write([]byte(message))
if err == nil {
return nil
}
}
// try to reconnect - close the connection first just in case
// we don't want fd leaks here.
if s.conn != nil {
s.conn.Close()
}
conn, err := getSyslogConnection(s.uri, s.tls)
if err != nil {
return err
}
s.conn = conn
_, err = s.conn.Write([]byte(message))
fmt.Printf("Retried: %v\n", message)
return err
}
// AfterParse parses the receiver configuration
func (s *SyslogReceiver) AfterParse(initArgs seelog.CustomReceiverInitArgs) error {
var conn net.Conn
var ok bool
var err error
s.enabled = true
uri, ok := initArgs.XmlCustomAttrs["uri"]
if ok && uri != "" {
url, err := url.ParseRequestURI(uri)
if err != nil {
s.enabled = false
}
s.uri = url
}
tls, ok := initArgs.XmlCustomAttrs["tls"]
if ok {
// if certificate specified it should already be in pool
if tls == "true" {
s.tls = true
}
}
if !s.enabled {
return errors.New("bad syslog receiver configuration - disabling")
}
conn, err = getSyslogConnection(s.uri, s.tls)
if err != nil {
fmt.Printf("%v\n", err)
return nil
}
s.conn = conn
return nil
}
// Flush is a NOP in current implementation
func (s *SyslogReceiver) Flush() {
// Nothing to do here...
}
// Close is a NOP in current implementation
func (s *SyslogReceiver) Close() error {
return nil
}
func parseShortFilePath(params string) seelog.FormatterFunc {
return func(message string, level seelog.LogLevel, context seelog.LogContextInterface) interface{} {
return extractShortPathFromFullPath(context.FullPath())
}
}
func extractShortPathFromFullPath(fullPath string) string {
// We want to trim the part containing the path of the project
// ie DataDog/datadog-agent/ or DataDog/datadog-process-agent/
slices := strings.Split(fullPath, "-agent/")
return slices[len(slices)-1]
}
func init() {
seelog.RegisterCustomFormatter("CustomSyslogHeader", createSyslogHeaderFormatter)
seelog.RegisterCustomFormatter("ShortFilePath", parseShortFilePath)
seelog.RegisterReceiver("syslog", &SyslogReceiver{})
}