-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
60 lines (47 loc) · 1.08 KB
/
logging.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
package nsqworker
import (
"github.com/sirupsen/logrus"
"io/ioutil"
"os"
)
type logWrapper struct {
log *logrus.Logger
*logrus.Entry
}
func newLogWrapper(topic, channel string) *logWrapper {
log := logrus.New()
log.Out = ioutil.Discard
formatter := new(logrus.TextFormatter)
formatter.DisableColors = true
log.Formatter = formatter
entry := log.WithFields(logrus.Fields{
"topic": topic,
"channel": channel,
})
return &logWrapper{log, entry}
}
// Capture nsq.Consumer logging messages
func (lw *logWrapper) Output(calldepth int, s string) error {
lw.Debug(s)
return nil
}
func (nw *NsqWorker) ToggleLogging(enable bool) {
if enable {
nw.internalLogger.log.Out = os.Stderr
} else {
nw.internalLogger.log.Out = ioutil.Discard
}
}
func (nw *NsqWorker) ToggleDebug(enable bool) {
if enable {
nw.internalLogger.log.Level = logrus.DebugLevel
} else {
nw.internalLogger.log.Level = logrus.InfoLevel
}
}
func (nw *NsqWorker) UseExternalLogger(extLogger logrus.FieldLogger) {
nw.log = extLogger
}
func (nw *NsqWorker) UseInternalLogger() {
nw.log = nw.internalLogger
}