This repository has been archived by the owner on Apr 25, 2024. It is now read-only.
/
log.go
141 lines (123 loc) 路 4.16 KB
/
log.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package log
import (
"fmt"
"github.com/go-logr/logr"
"k8s.io/klog"
"strings"
)
func NewLog(level int) logr.Logger {
return &log{
level: level,
}
}
type log struct {
current int
level int
prefixes []string
}
// Info logs a non-error message with the given key/value pairs as context.
//
// The msg argument should be used to add some constant description to
// the log line. The key/value pairs can then be used to add additional
// variable information. The key/value pairs should alternate string
// keys and arbitrary values.
func (l *log) Info(msg string, keysAndValues ...interface{}) {
klog.InfoDepth(1, l.formatMsg(msg, keysAndValues...))
}
// Enabled tests whether this InfoLogger is enabled. For example,
// commandline flags might be used to set the logging verbosity and disable
// some info logs.
func (l *log) Enabled() bool {
return true
}
// Error logs an error, with the given message and key/value pairs as context.
// It functions similarly to calling Info with the "error" named value, but may
// have unique behavior, and should be preferred for logging errors (see the
// package documentations for more information).
//
// The msg field should be used to add context to any underlying error,
// while the err field should be used to attach the actual error that
// triggered this log line, if present.
func (l *log) Error(err error, msg string, keysAndValues ...interface{}) {
newKeysAndValues := []interface{}{err}
newKeysAndValues = append(newKeysAndValues, keysAndValues...)
klog.ErrorDepth(1, l.formatMsg(msg, newKeysAndValues...))
}
// V returns an InfoLogger value for a specific verbosity level. A higher
// verbosity level means a log message is less important. It's illegal to
// pass a log level less than zero.
func (l *log) V(level int) logr.Logger {
if level < l.level {
return &silent{}
}
prefixes := []string{}
prefixes = append(prefixes, l.prefixes...)
return &log{
level: l.level,
current: level,
prefixes: prefixes,
}
}
// WithValues adds some key-value pairs of context to a logger.
// See Info for documentation on how key/value pairs work.
func (l *log) WithValues(keysAndValues ...interface{}) logr.Logger {
prefixes := []string{}
prefixes = append(prefixes, l.prefixes...)
prefixes = append(prefixes, formatKeysAndValues(keysAndValues...))
return &log{
level: l.level,
current: l.current,
prefixes: prefixes,
}
}
// WithName adds a new element to the logger's name.
// Successive calls with WithName continue to append
// suffixes to the logger's name. It's strongly reccomended
// that name segments contain only letters, digits, and hyphens
// (see the package documentation for more information).
func (l *log) WithName(name string) logr.Logger {
prefixes := []string{}
prefixes = append(prefixes, l.prefixes...)
prefixes = append(prefixes, name)
return &log{
level: l.level,
current: l.current,
prefixes: prefixes,
}
}
func (l *log) formatMsg(msg string, keysAndValues ...interface{}) string {
prefixes := strings.Join(l.prefixes, ": ")
addString := formatKeysAndValues(keysAndValues...)
retString := msg
if prefixes != "" {
retString = prefixes + ": " + retString
}
if addString != "" {
retString += " " + addString
}
// if l.current != 0 {
// retString = "(" + strconv.Itoa(l.current) + ") " + retString
// }
return retString
}
func formatKeysAndValues(keysAndValues ...interface{}) string {
args := []string{}
for _, kv := range keysAndValues {
switch t := kv.(type) {
case string:
args = append(args, t)
case error:
args = append(args, t.Error())
default:
args = append(args, fmt.Sprintf("%#v", kv))
}
}
return strings.Join(args, " ")
}
type silent struct{}
func (s *silent) Info(msg string, keysAndValues ...interface{}) {}
func (s *silent) Enabled() bool { return false }
func (s *silent) Error(err error, msg string, keysAndValues ...interface{}) {}
func (s *silent) V(level int) logr.Logger { return s }
func (s *silent) WithValues(keysAndValues ...interface{}) logr.Logger { return s }
func (s *silent) WithName(name string) logr.Logger { return s }