-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler.go
77 lines (62 loc) · 1.94 KB
/
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
// Copyright 2013 Marc Weistroff. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package log
// HandlerInterface represents a type that sends log to a destination
type HandlerInterface interface {
S(Severity) bool // Returns true if the handler accepts this severity
SetSeverity(Severity) // Changes the severity threshold of the handler
Handle(Record) // Handle the log record
PushProcessor(Processor) // Push a new processor to the handler's stack
PopProcessor() // Removes a processor from the handler's stack
SetFormatter(Formatter) // Set the formatter for this handler
GetFormatter() Formatter // Returns the formatter used by this handler
Close() // Close the handler
}
// Handler is a somewhat a "abstract" type you can embed in your own handler.
// It provides easiness in writing your own handlers
type Handler struct {
Level Severity
Formatter Formatter
Processors []Processor
}
func (h *Handler) SetSeverity(level Severity) {
h.Level = level
}
func (h *Handler) S(level Severity) bool {
return level <= h.Level
}
func (h *Handler) SetFormatter(f Formatter) {
h.Formatter = f
}
func (h *Handler) GetFormatter() Formatter {
return h.Formatter
}
func (h *Handler) Prepare(r *Record) {
h.Process(r)
if h.Formatter == nil {
h.Formatter = NewSimpleLineFormatter()
}
h.Formatter.Format(r)
}
func (h *Handler) Process(r *Record) {
for k := range h.Processors {
h.Processors[k].Process(r)
}
}
func (h *Handler) PushProcessor(p Processor) {
processors := make([]Processor, len(h.Processors))
copy(processors, h.Processors)
h.Processors = []Processor{p}
h.Processors = append(h.Processors, processors...)
}
func (h *Handler) PopProcessor() {
if len(h.Processors) > 0 {
h.Processors = h.Processors[1:len(h.Processors)]
return
}
panic("Processors stack is empty")
}
func (h *Handler) Write() {
// NO OP
}