-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter_chain_handler.go
78 lines (68 loc) · 1.96 KB
/
filter_chain_handler.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
package clog
// LevelFilterChain is a log middleware that is only passing log entries of level >= minimum level
type LevelFilterChain struct {
handler Handler
minLevel LogLevel
next Handler
}
// NewLevelFilterChain creates a new LevelFilterChain handler
// passing log entries to destination handler if level >= minimum level,
// and passing them to next handler if level < minimum level
func NewLevelFilterChain(minLevel LogLevel, destination, next Handler) *LevelFilterChain {
return &LevelFilterChain{
minLevel: minLevel,
handler: destination,
next: next,
}
}
// SetLevel changes the minimum level the log entries are going to be sent to the destination logger
func (h *LevelFilterChain) SetLevel(minLevel LogLevel) *LevelFilterChain {
h.minLevel = minLevel
return h
}
// SetHandler sets a new handler for the filter
func (h *LevelFilterChain) SetHandler(handler Handler) {
h.handler = handler
}
// GetHandler returns the current handler used by the filter
func (h *LevelFilterChain) GetHandler() Handler {
return h.handler
}
// SetNextHandler sets the next handler in chain
func (h *LevelFilterChain) SetNextHandler(handler Handler) {
h.next = handler
}
// GetNextHandler returns the next handler in chain
func (h *LevelFilterChain) GetNextHandler() Handler {
return h.next
}
// SetPrefix sets a prefix on every log message
func (h *LevelFilterChain) SetPrefix(prefix string) Handler {
if h.handler == nil {
return h
}
prefixer, ok := h.handler.(Prefixer)
if ok {
prefixer.SetPrefix(prefix)
}
return h
}
// LogEntry the LogEntry
func (h *LevelFilterChain) LogEntry(logEntry LogEntry) error {
if logEntry.Level < h.minLevel {
if h.next == nil {
return ErrNoRegisteredHandler
}
return h.next.LogEntry(logEntry)
}
if h.handler == nil {
return ErrNoRegisteredHandler
}
logEntry.Calldepth++
return h.handler.LogEntry(logEntry)
}
// Verify interface
var (
_ Handler = &LevelFilterChain{}
_ MiddlewareHandler = &LevelFilterChain{}
)