forked from sensu/sensu-go
/
logrus_formatter.go
58 lines (50 loc) · 1.26 KB
/
logrus_formatter.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
package etcd
import (
"fmt"
"github.com/coreos/pkg/capnslog"
"github.com/sirupsen/logrus"
)
type logrusFormatter struct {
logger *logrus.Entry
}
// NewLogrusFormatter creates a new LogrusFormatter
func NewLogrusFormatter() capnslog.Formatter {
logger := logrus.WithFields(logrus.Fields{
"component": "etcd",
})
return &logrusFormatter{
logger: logger,
}
}
func (s *logrusFormatter) logWithPkg(pkg string) *logrus.Entry {
return s.logger.WithFields(logrus.Fields{
"pkg": pkg,
})
}
// Format builds a log message for the LogrusFormatter.
func (s *logrusFormatter) Format(pkg string, l capnslog.LogLevel, _ int, entries ...interface{}) {
for _, entry := range entries {
str := fmt.Sprint(entry)
switch l {
case capnslog.CRITICAL:
s.logWithPkg(pkg).Fatal(str)
case capnslog.ERROR:
s.logWithPkg(pkg).Error(str)
case capnslog.WARNING:
s.logWithPkg(pkg).Warning(str)
case capnslog.NOTICE:
s.logWithPkg(pkg).Warning(str)
case capnslog.INFO:
s.logWithPkg(pkg).Info(str)
case capnslog.DEBUG:
s.logWithPkg(pkg).Debug(str)
case capnslog.TRACE:
s.logWithPkg(pkg).Debug(str)
default:
panic("Unhandled loglevel")
}
}
}
// Flush is included so that the interface is complete, but is a no-op.
func (s *logrusFormatter) Flush() {
}