-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_logger.go
38 lines (31 loc) · 1003 Bytes
/
mqtt_logger.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
package main
import (
mqtt "github.com/eclipse/paho.mqtt.golang"
log "github.com/sirupsen/logrus"
)
// mqttLogger wrapps mqtt's Logger interface to a wrapper for logrus.
type mqttLogger struct {
ln func(v ...interface{})
f func(format string, v ...interface{})
}
func (l *mqttLogger) Println(v ...interface{}) {
l.ln("MQTT:", v)
}
func (l *mqttLogger) Printf(format string, v ...interface{}) {
l.f("MQTT: "+format, v)
}
// newLogger creates a new mqttLogger, wrapping the two required methods.
func newLogger(ln func(v ...interface{}), f func(format string, v ...interface{})) *mqttLogger {
return &mqttLogger{
ln: ln,
f: f,
}
}
// setupMqttLogger wraps mqtt's different Loggers to their logrus pedant.
func setupMqttLogger() {
logger := log.StandardLogger()
mqtt.CRITICAL = newLogger(logger.Panicln, logger.Panicf)
mqtt.ERROR = newLogger(logger.Errorln, logger.Errorf)
mqtt.WARN = newLogger(logger.Warnln, logger.Warnf)
mqtt.DEBUG = newLogger(logger.Debugln, logger.Debugf)
}