/
logging_syslog.go
154 lines (137 loc) · 3.96 KB
/
logging_syslog.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
//go:build !windows
package logging
import (
"log/syslog"
"github.com/sirupsen/logrus"
logrus_syslog "github.com/sirupsen/logrus/hooks/syslog"
)
const (
SLevel = "syslog.level"
SNetwork = "syslog.network"
SAddress = "syslog.address"
SSeverity = "syslog.severity"
SFacility = "syslog.facility"
STag = "syslog.tag"
)
var (
// syslogOpts is the set of supported options for syslog configuration.
syslogOpts = map[string]bool{
SLevel: true,
SNetwork: true,
SAddress: true,
SSeverity: true,
SFacility: true,
STag: true,
}
// From /usr/include/sys/syslog.h.
syslogSeverityMap = map[string]syslog.Priority{
"emerg": syslog.LOG_EMERG,
"panic": syslog.LOG_EMERG,
"alert": syslog.LOG_ALERT,
"crit": syslog.LOG_CRIT,
"err": syslog.LOG_ERR,
"error": syslog.LOG_ERR,
"warn": syslog.LOG_WARNING,
"warning": syslog.LOG_WARNING,
"notice": syslog.LOG_NOTICE,
"info": syslog.LOG_INFO,
"debug": syslog.LOG_DEBUG,
}
// From /usr/include/sys/syslog.h.
syslogFacilityMap = map[string]syslog.Priority{
"kern": syslog.LOG_KERN,
"user": syslog.LOG_USER,
"mail": syslog.LOG_MAIL,
"daemon": syslog.LOG_DAEMON,
"auth": syslog.LOG_AUTH,
"syslog": syslog.LOG_SYSLOG,
"lpr": syslog.LOG_LPR,
"news": syslog.LOG_NEWS,
"uucp": syslog.LOG_UUCP,
"cron": syslog.LOG_CRON,
"authpriv": syslog.LOG_AUTHPRIV,
"ftp": syslog.LOG_FTP,
"local0": syslog.LOG_LOCAL0,
"local1": syslog.LOG_LOCAL1,
"local2": syslog.LOG_LOCAL2,
"local3": syslog.LOG_LOCAL3,
"local4": syslog.LOG_LOCAL4,
"local5": syslog.LOG_LOCAL5,
"local6": syslog.LOG_LOCAL6,
"local7": syslog.LOG_LOCAL7,
}
// syslogLevelMap maps logrus.Level values to syslog.Priority levels.
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,
logrus.TraceLevel: syslog.LOG_DEBUG,
}
)
func mapStringPriorityToSlice(m map[string]syslog.Priority) []string {
s := make([]string, 0, len(m))
for k := range m {
s = append(s, k)
}
return s
}
// 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 LogOptions, tag string, debug bool) error {
opts := getLogDriverConfig(Syslog, logOpts)
syslogOptValues := make(map[string][]string)
syslogOptValues[SSeverity] = mapStringPriorityToSlice(syslogSeverityMap)
syslogOptValues[SFacility] = mapStringPriorityToSlice(syslogFacilityMap)
if err := opts.validateOpts(Syslog, syslogOpts, syslogOptValues); err != nil {
return err
}
if stag, ok := opts[STag]; ok {
tag = stag
}
logLevel, ok := opts[SLevel]
if !ok {
if debug {
logLevel = "debug"
} else {
logLevel = "info"
}
}
// Validate provided log level.
level, err := logrus.ParseLevel(logLevel)
if err != nil {
DefaultLogger.Fatal(err)
}
SetLogLevel(level)
network := ""
address := ""
// Inherit severity from log level if syslog.severity is not specified explicitly
severity := syslogLevelMap[level]
// Default values for facility if not specified
facility := syslog.LOG_KERN
if networkStr, ok := opts[SNetwork]; ok {
network = networkStr
}
if addressStr, ok := opts[SAddress]; ok {
address = addressStr
}
if severityStr, ok := opts[SSeverity]; ok {
severity = syslogSeverityMap[severityStr]
}
if facilityStr, ok := opts[SFacility]; ok {
facility = syslogFacilityMap[facilityStr]
}
// Create syslog hook.
h, err := logrus_syslog.NewSyslogHook(network, address, severity|facility, tag)
if err != nil {
DefaultLogger.Fatal(err)
}
// TODO: switch to a per-logger version when we upgrade to logrus >1.0.3
logrus.AddHook(h)
DefaultLogger.AddHook(h)
return nil
}