-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
73 lines (62 loc) · 1.87 KB
/
Copy pathoptions.go
File metadata and controls
73 lines (62 loc) · 1.87 KB
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
package ecslog
import (
"context"
"log/slog"
)
// LogLevelFunc is used in WithLogLevelFunc to control which log entry is going to
// be logged. For basic control based on slog.Level use WithLogLevel().
type LogLevelFunc func(ctx context.Context, level slog.Level) bool
type handlerOptions struct {
hideTimestamp bool
addSource bool
levelF LogLevelFunc
}
type Option func(*handlerOptions)
func getOptions(options []Option) *handlerOptions {
hOptions := &handlerOptions{
levelF: func(_ context.Context, level slog.Level) bool {
return level >= slog.LevelInfo
},
}
for _, option := range options {
option(hOptions)
}
return hOptions
}
// WithTimestamp option can be used to disable timestamp
// ("@timestamp" field) in generated logs.
func WithTimestamp(showTimestamp bool) Option {
return func(h *handlerOptions) {
h.hideTimestamp = !showTimestamp
}
}
// WithSource option can be used to add log source information to logs.
//
// Keys "log.origin.function", "log.origin.file" and "log.origin.line" will
// filled by information received from [log/slog].
func WithSource(addSource bool) Option {
return func(h *handlerOptions) {
h.addSource = addSource
}
}
// WithLogLevel options sets minimum log level to be outputted.
// Default value is slog.LevelInfo.
//
// This option is exclusive with WithLogLevelFunc.
func WithLogLevel(minLevel slog.Level) Option {
return func(h *handlerOptions) {
h.levelF = func(ctx context.Context, level slog.Level) bool {
return level >= minLevel
}
}
}
// WithLogLevelFunc options sets log level decision function.
// Given function will be called for each log entry and if it returns false,
// no log will be produced. Context passed to this function comes from slog.
//
// This option is exclusive with WithLogLevel.
func WithLogLevelFunc(logLevelF LogLevelFunc) Option {
return func(h *handlerOptions) {
h.levelF = logLevelF
}
}