-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogbridge.go
More file actions
77 lines (65 loc) · 1.79 KB
/
Copy pathlogbridge.go
File metadata and controls
77 lines (65 loc) · 1.79 KB
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
package main
import (
"cmp"
"context"
"fmt"
"slices"
"strings"
"github.com/go-logr/logr"
"github.com/projecteru2/core/log"
)
// crSink forwards controller-runtime's logr output to core/log; reconcile errors surface nowhere else.
type crSink struct {
ctx context.Context
name string
kv []any
}
// The root name stays empty; controller-runtime adds its own via WithName.
func newCRLogger(ctx context.Context) logr.Logger {
return logr.New(&crSink{ctx: ctx})
}
func (s *crSink) Init(logr.RuntimeInfo) {}
// Errors bypass this gate entirely (logr contract).
func (s *crSink) Enabled(level int) bool { return level == 0 }
func (s *crSink) Info(_ int, msg string, kvs ...any) {
log.WithFunc(s.funcName()).Info(s.ctx, s.line(msg, kvs))
}
func (s *crSink) Error(err error, msg string, kvs ...any) {
if err == nil {
// logr allows Error(nil, ...) for anomaly reports, but core/log drops
// nil-err Error lines entirely; keep them visible as warnings.
log.WithFunc(s.funcName()).Warn(s.ctx, s.line(msg, kvs))
return
}
log.WithFunc(s.funcName()).Error(s.ctx, err, s.line(msg, kvs))
}
func (s *crSink) WithValues(kvs ...any) logr.LogSink {
next := *s
next.kv = slices.Concat(s.kv, kvs)
return &next
}
func (s *crSink) WithName(name string) logr.LogSink {
next := *s
if s.name == "" {
next.name = name
} else {
next.name = s.name + "." + name
}
return &next
}
// funcName labels unnamed root output so its origin stays identifiable.
func (s *crSink) funcName() string {
return cmp.Or(s.name, "controller-runtime")
}
func (s *crSink) line(msg string, kvs []any) string {
pairs := slices.Concat(s.kv, kvs)
if len(pairs) == 0 {
return msg
}
var b strings.Builder
b.WriteString(msg)
for i := 0; i+1 < len(pairs); i += 2 {
fmt.Fprintf(&b, " %v=%v", pairs[i], pairs[i+1])
}
return b.String()
}