-
Notifications
You must be signed in to change notification settings - Fork 1
/
target.go
44 lines (35 loc) · 1.11 KB
/
target.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
package logrus4logr
import (
"fmt"
"github.com/wiggin77/logr"
"github.com/sirupsen/logrus"
)
// TAdapter wraps a Logrus hook allowing the hook be used as a Logr target.
// Create instances with `NewAdapterTarget`.
type TAdapter struct {
logr.Basic
hook logrus.Hook
// Logger is an optional logrus.Logger instance to use instead of the default.
Logger *logrus.Logger
}
// NewAdapterTarget creates a target wrapper for a Logrus hook.
// If filter and/or formatter are nil then defaults will be used (Panic level; Plain formatter).
func NewAdapterTarget(filter logr.Filter, formatter logr.Formatter, hook logrus.Hook, maxQueue int) *TAdapter {
a := &TAdapter{hook: hook}
a.Basic.Start(a, a, filter, formatter, maxQueue)
return a
}
// Write converts a log record to a Logrus entry and
// passes it to the Logrus hook.
func (a *TAdapter) Write(rec *logr.LogRec) error {
rus := a.Logger
if rus == nil {
rus = logrus.StandardLogger()
}
entry := convertLogRec(rec, rus)
return a.hook.Fire(entry)
}
// String returns the type name of the Logrus hook.
func (a *TAdapter) String() string {
return fmt.Sprintf("%T", a.hook)
}