-
Notifications
You must be signed in to change notification settings - Fork 1
/
facility.go
103 lines (87 loc) · 2.51 KB
/
facility.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
package glo
import "os"
// Facility is the main entry when used
type Facility interface {
Logger
LoggerDebug
LoggerInfo
LoggerNotice
LoggerWarning
LoggerError
LoggerCritical
LoggerAlert
LoggerEmergency
ClearHandlers() Facility
PushHandler(Handler) Facility
}
// NewFacility creates a logger facility, this is the main entry
func NewFacility() Facility {
return &facility{
[]Handler{},
}
}
// NewStdFacility creates a logger with two handlers, pointing to os.Stdout and os.Stderr
// anything below Error gets sent to Stdout, anything above Warning gets sent to Stderr
func NewStdFacility() Facility {
f := NewFacility()
ho := NewHandler(os.Stdout).PushFilter(NewFilterLevelRange(Debug, Warning))
he := NewHandler(os.Stderr).PushFilter(NewFilterLevel(Error))
f.PushHandler(ho).PushHandler(he)
return f
}
type facility struct {
handlers []Handler
}
// ClearHandlers removes all registered handlers
func (f *facility) ClearHandlers() Facility {
f.handlers = []Handler{}
return f
}
// PushHandlers adds a new handler
func (f *facility) PushHandler(h Handler) Facility {
f.handlers = append(f.handlers, h)
return f
}
// Log sends output to all registered handlers
func (f *facility) Log(level Level, line string, params ...interface{}) error {
var err1 error
for _, hndl := range f.handlers {
err := hndl.Log(level, line, params...)
if err != nil && err1 == nil {
err1 = err
}
}
return err1
}
// Debug logs a debug line
func (f *facility) Debug(line string, params ...interface{}) error {
return f.Log(Debug, line, params...)
}
// Info logs an info line
func (f *facility) Info(line string, params ...interface{}) error {
return f.Log(Info, line, params...)
}
// Notice logs a notice line
func (f *facility) Notice(line string, params ...interface{}) error {
return f.Log(Notice, line, params...)
}
// Warning logs a warning line
func (f *facility) Warning(line string, params ...interface{}) error {
return f.Log(Warning, line, params...)
}
// Error logs an error line
func (f *facility) Error(line string, params ...interface{}) error {
return f.Log(Error, line, params...)
}
// Critical logs a critical line
func (f *facility) Critical(line string, params ...interface{}) error {
return f.Log(Critical, line, params...)
}
// Alert logs an alert line
func (f *facility) Alert(line string, params ...interface{}) error {
return f.Log(Alert, line, params...)
}
// Emergency logs an emergency line
func (f *facility) Emergency(line string, params ...interface{}) error {
return f.Log(Emergency, line, params...)
}