-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmultilogger.go
100 lines (83 loc) · 2.08 KB
/
multilogger.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
package go2chef
/*
Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
*/
import (
"fmt"
"runtime"
)
// MultiLogger is a fan-out logger for use as the central
// logging broker in go2chef
type MultiLogger struct {
loggers []Logger
debug int
level int
}
// NewMultiLogger returns a MultiLogger with the provided list
// of loggers set up to receive logs.
func NewMultiLogger(loggers []Logger) *MultiLogger {
return &MultiLogger{
loggers: loggers,
debug: int(^uint(0) >> 1),
level: LogLevelDebug,
}
}
func (m *MultiLogger) String() string {
return "MultiLogger"
}
// Name returns the name of this logger
func (m *MultiLogger) Name() string {
return "MultiLogger"
}
// SetName is a no-op for this logger
func (m *MultiLogger) SetName(string) {}
// Type returns the type of this logger
func (m *MultiLogger) Type() string {
return "go2chef.logger.multi"
}
// Errorf logs a formatted message at ERROR level
func (m *MultiLogger) Errorf(s string, v ...interface{}) {
for _, l := range m.loggers {
l.Errorf(stack2()+s, v...)
}
}
// Infof logs a formatted message at INFO level
func (m *MultiLogger) Infof(s string, v ...interface{}) {
for _, l := range m.loggers {
l.Infof(stack2()+s, v...)
}
}
// Debugf logs a formatted message at DEBUG level
func (m *MultiLogger) Debugf(dbg int, s string, v ...interface{}) {
for _, l := range m.loggers {
l.Debugf(dbg, stack2()+s, v...)
}
}
// SetLevel sets the logger's overall level threshold
func (m *MultiLogger) SetLevel(l int) {
m.level = l
}
// SetDebug sets the logger's debug level threshold
func (m *MultiLogger) SetDebug(d int) {
m.debug = d
}
// WriteEvent writes an event to all loggers on this MultiLogger
func (m *MultiLogger) WriteEvent(e *Event) {
for _, l := range m.loggers {
l.WriteEvent(e)
}
}
// Shutdown shuts down all loggers on this MultiLogger
func (m *MultiLogger) Shutdown() {
for _, l := range m.loggers {
l.Shutdown()
}
}
func stack2() string {
_, f, l, ok := runtime.Caller(2)
if ok {
return fmt.Sprintf("%s:%d::", f, l)
}
return "runtime-caller-err::"
}
var _ Logger = &MultiLogger{}