/
logger.go
295 lines (257 loc) · 9.49 KB
/
logger.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
package flags
import (
"errors"
"os"
"strings"
"time"
"unicode"
"github.com/aquasecurity/tracee/pkg/errfmt"
"github.com/aquasecurity/tracee/pkg/logger"
)
func logHelp() string {
return `Control logger options - aggregation and level priority.
Possible options:
--log aggregate[:flush-interval] | turns log aggregation on, delaying output with an optional interval (s, m) (default: 3s)
--log <debug|info|warn|error|panic> | set log level, info is the default
--log file:/path/to/file | write the logs to a specified file. create/trim the file if exists (default: stderr)
--log filter:<option;...> | Filters in logs that match the specified option values.
--log filter-out:<option;...> | Filters out logs that match the specified option values.
Filter options:
msg=<value,...> | Filters logs that message contains a value.
regex=<value,...> | Filters logs that a regex matches the message.
pkg=<value,...> | Filters logs that originate from a package.
file=<value,...> | Filters logs that originate from a file.
lvl=<value,...> | Filters logs that are of a specific level.
libbpf | Filters logs that originate from libbpf.
Examples:
--log debug | outputs debug level logs
--log debug --log aggregate | outputs aggregated debug level logs every 3 seconds (default)
--log aggregate:5s | outputs aggregated logs every 5 seconds
--log debug --log file:/tmp/tracee.log | outputs debug level logs to /tmp/tracee.log
--log filter:'msg=foo,bar;pkg=core;lvl=error' | Filters in logs that have either 'foo' or 'bar' in the message, are from the 'core' package, and are of 'error' level.
--log filter-out:'msg=foo,bar;pkg=core;lvl=error' | Filters out logs that have either 'foo' or 'bar' in the message, are from the 'core' package, and are of 'error' level.
--log filter:msg=foo,bar --log filter-out:pkg=core | Filters in logs that have either 'foo' or 'bar' in the message, and based on that result, filters out logs that are from the 'core' package.
--log filter-out:file=/pkg/cmd/flags/logger.go | Filters out logs that are from the '/pkg/cmd/flags/logger.go' file.
--log filter:regex='^foo' | Filters in logs that messages match the regex '^foo'.
--log filter:libbpf | Filters in logs that originate from libbpf.
`
}
func invalidLogOption(err error, opt string, newBinary bool) error {
if err == nil {
// this is a hack to clear the previous two chars from the error message
err = errors.New("\b\b")
}
if newBinary {
return errfmt.Errorf("invalid log option: %s, %s, run 'man log' for more info", opt, err)
}
return errfmt.Errorf("invalid log option: %s, %s, use '--log help' for more info", opt, err)
}
func invalidLogOptionValue(err error, opt string, newBinary bool) error {
if err == nil {
// this is a hack to clear the previous two chars from the error message
err = errors.New("\b\b")
}
if newBinary {
return errfmt.Errorf("invalid log option value: %s, %s, use '--help' for more info", opt, err)
}
return errfmt.Errorf("invalid log option value: %s, %s, use '--log help' for more info", opt, err)
}
func parseLevel(level string) (logger.Level, error) {
switch level {
case "debug":
return logger.DebugLevel, nil
case "info":
return logger.InfoLevel, nil
case "warn":
return logger.WarnLevel, nil
case "error":
return logger.ErrorLevel, nil
case "fatal":
return logger.FatalLevel, nil
default:
return logger.DebugLevel, errors.New("invalid log level")
}
}
func validateLogOption(opt string) error {
switch {
case strings.HasPrefix(opt, "file"):
return nil
case strings.HasPrefix(opt, "aggregate"):
return nil
case strings.HasPrefix(opt, "filter-out"):
return nil
case strings.HasPrefix(opt, "filter"):
return nil
}
if _, err := parseLevel(opt); err == nil {
return nil
}
// don't pass the error, it's not relevant in this case
return invalidLogOption(nil, opt, false)
}
func PrepareLogger(logOptions []string, newBinary bool) (logger.LoggingConfig, error) {
var (
agg bool
filter = logger.NewLoggerFilter()
flushInterval = logger.DefaultFlushInterval
lvl = logger.DefaultLevel
err error
w = os.Stderr
)
for _, opt := range logOptions {
if err := validateLogOption(opt); err != nil {
return logger.LoggingConfig{}, err
}
// parse file option
if strings.HasPrefix(opt, "file") {
vals := strings.Split(opt, ":")
if len(vals) == 1 || vals[1] == "" {
return logger.LoggingConfig{}, invalidLogOptionValue(nil, opt, newBinary)
}
w, err = createFile(vals[1])
if err != nil {
return logger.LoggingConfig{}, err
}
continue
}
// parse aggregate option
if strings.HasPrefix(opt, "aggregate") {
if !strings.HasSuffix(opt, "aggregate") {
vals := strings.Split(opt, ":")
if len(vals) != 2 || len(vals[1]) <= 1 {
return logger.LoggingConfig{}, invalidLogOptionValue(nil, opt, newBinary)
}
// handle only seconds and minutes
timeSuffix := vals[1][len(vals[1])-1:][0]
if timeSuffix != 's' && timeSuffix != 'm' {
return logger.LoggingConfig{}, invalidLogOptionValue(nil, opt, newBinary)
}
prevByte := vals[1][len(vals[1])-2:][0]
if timeSuffix == 's' && !unicode.IsDigit(rune(prevByte)) {
return logger.LoggingConfig{}, invalidLogOptionValue(nil, opt, newBinary)
}
flushInterval, err = time.ParseDuration(vals[1])
if err != nil {
return logger.LoggingConfig{}, invalidLogOptionValue(nil, opt, newBinary)
}
}
agg = true
continue
}
// parse filter option
filterOpts := ""
var filterKind logger.FilterKind
if strings.HasPrefix(opt, "filter-out:") {
filterOpts = strings.TrimPrefix(opt, "filter-out:")
filterKind = logger.FilterOut
} else if strings.HasPrefix(opt, "filter:") {
filterOpts = strings.TrimPrefix(opt, "filter:")
filterKind = logger.FilterIn
}
if filterOpts != "" {
for _, filterOpt := range strings.Split(filterOpts, ";") {
optTypeVal := strings.SplitN(filterOpt, "=", 2)
optType := optTypeVal[0]
optVals := []string{}
if len(optTypeVal) == 1 && optType != "libbpf" {
return logger.LoggingConfig{}, invalidLogOption(nil, opt, newBinary)
}
if len(optTypeVal) == 2 {
if optTypeVal[1] == "" {
return logger.LoggingConfig{}, invalidLogOptionValue(nil, opt, newBinary)
}
optVals = strings.Split(optTypeVal[1], ",")
}
switch optType {
case "msg":
for _, val := range optVals {
if err := filter.AddMsg(val, filterKind); err != nil {
if errors.Is(err, logger.ErrFilterOutExistsForKey) {
logger.Warnw(err.Error(), "msg", val)
continue
}
return logger.LoggingConfig{}, invalidLogOption(err, opt, newBinary)
}
}
case "pkg":
for _, val := range optVals {
if err := filter.AddPkg(val, filterKind); err != nil {
if errors.Is(err, logger.ErrFilterOutExistsForKey) {
logger.Warnw(err.Error(), "pkg", val)
continue
}
return logger.LoggingConfig{}, invalidLogOption(err, opt, newBinary)
}
}
case "file":
for _, val := range optVals {
if err := filter.AddFile(val, filterKind); err != nil {
if errors.Is(err, logger.ErrFilterOutExistsForKey) {
logger.Warnw(err.Error(), "file", val)
continue
}
return logger.LoggingConfig{}, invalidLogOption(err, opt, newBinary)
}
}
case "lvl":
for _, val := range optVals {
filterLvl, err := parseLevel(val)
if err != nil {
return logger.LoggingConfig{}, invalidLogOptionValue(err, opt, newBinary)
}
if err := filter.AddLvl(int(filterLvl), filterKind); err != nil {
if errors.Is(err, logger.ErrFilterOutExistsForKey) {
logger.Warnw(err.Error(), "lvl", val)
continue
}
return logger.LoggingConfig{}, invalidLogOptionValue(err, opt, newBinary)
}
}
case "regex":
for _, val := range optVals {
if err := filter.AddMsgRegex(val, filterKind); err != nil {
if errors.Is(err, logger.ErrFilterOutExistsForKey) {
logger.Warnw(err.Error(), "regex", val)
continue
}
return logger.LoggingConfig{}, invalidLogOptionValue(err, opt, newBinary)
}
}
case "libbpf":
if err := filter.AddMsgRegex("^libbpf:", filterKind); err != nil {
if errors.Is(err, logger.ErrFilterOutExistsForKey) {
logger.Warnw(err.Error(), "regex", "^libbpf:")
continue
}
return logger.LoggingConfig{}, invalidLogOptionValue(err, opt, newBinary)
}
default:
return logger.LoggingConfig{}, invalidLogOption(nil, opt, newBinary)
}
}
continue
}
// parse level option
lvl, err = parseLevel(opt)
if err != nil {
return logger.LoggingConfig{}, invalidLogOption(err, opt, newBinary)
}
}
loggerCfg := logger.LoggerConfig{
Writer: w,
Level: logger.NewAtomicLevelAt(lvl),
}
if lvl == logger.DebugLevel {
loggerCfg.Encoder = logger.NewJSONEncoder(logger.NewDevelopmentEncoderConfig())
} else {
loggerCfg.Encoder = logger.NewJSONEncoder(logger.NewProductionEncoderConfig())
}
llogger := logger.NewLogger(loggerCfg)
return logger.LoggingConfig{
Logger: llogger,
LoggerConfig: loggerCfg,
Filter: filter,
Aggregate: agg,
FlushInterval: flushInterval,
}, nil
}