generated from logur/integration-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
87 lines (68 loc) · 1.87 KB
/
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
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
// Package logr provides Logur integration for Logr interface.
package logr
import (
"fmt"
"github.com/go-logr/logr"
"logur.dev/logur"
"logur.dev/integration/logr/internal/keyvals"
)
// Logger is a Logr logger.
type Logger struct {
logger logur.Logger
levelEnabler logur.LevelEnabler
name string
}
// New returns a new Logr logger.
func New(logger logur.Logger) logr.Logger {
l := &Logger{
logger: logger,
}
if levelEnabler, ok := logger.(logur.LevelEnabler); ok {
l.levelEnabler = levelEnabler
}
return logr.New(l)
}
// Info logs a non-error message with the given key/value pairs as context.
func (l *Logger) Info(level int, msg string, keysAndValues ...interface{}) {
if len(keysAndValues) > 0 {
l.logger.Info(msg, keyvals.ToMap(keysAndValues))
return
}
l.logger.Info(msg)
}
// Enabled tests whether this InfoLogger is enabled.
func (l *Logger) Enabled(level int) bool {
if l.levelEnabler == nil {
return true
}
return l.levelEnabler.LevelEnabled(logur.Info)
}
// Error logs an error, with the given message and key/value pairs as context.
func (l *Logger) Error(err error, msg string, keysAndValues ...interface{}) {
if len(keysAndValues) > 0 {
l.logger.Error(msg, keyvals.ToMap(keysAndValues))
return
}
l.logger.Error(msg)
}
// WithValues adds some key-value pairs of context to a logger.
func (l *Logger) WithValues(keysAndValues ...interface{}) logr.LogSink {
if len(keysAndValues) == 0 {
return l
}
return &Logger{
logger: logur.WithFields(l.logger, keyvals.ToMap(keysAndValues)),
levelEnabler: l.levelEnabler,
name: l.name,
}
}
// WithName adds a new element to the logger's name.
func (l *Logger) WithName(name string) logr.LogSink {
return &Logger{
logger: l.logger,
levelEnabler: l.levelEnabler,
name: fmt.Sprintf("%s-%s", l.name, name),
}
}
func (l *Logger) Init(info logr.RuntimeInfo) {
}