-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
61 lines (53 loc) · 1.1 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
package glo
import (
"io"
"time"
)
// Handler logs to stdout
type Handler interface {
Logger
SetFormatter(Formatter) Handler
ClearFilters() Handler
PushFilter(Filter) Handler
}
// NewHandler creates handler that prints to an io.Writer
func NewHandler(writer io.Writer) Handler {
return &handler{
writer,
NewFormatter(DefaultFormat),
[]Filter{},
}
}
type handler struct {
writer io.Writer
formatter Formatter
filters []Filter
}
// Log logs a line with a specific level
func (h *handler) Log(level Level, line string, params ...interface{}) error {
valid := true
for _, f := range h.filters {
if !f.Check(level, line, params) {
valid = false
break
}
}
if !valid {
return nil
}
l := h.formatter.Format(time.Now(), level, line, params...) + "\n"
_, err := h.writer.Write([]byte(l))
return err
}
func (h *handler) SetFormatter(formatter Formatter) Handler {
h.formatter = formatter
return h
}
func (h *handler) ClearFilters() Handler {
h.filters = []Filter{}
return h
}
func (h *handler) PushFilter(filter Filter) Handler {
h.filters = append(h.filters, filter)
return h
}