-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazy.go
145 lines (118 loc) · 3.53 KB
/
lazy.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
142
143
144
145
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright 2023 Andy Bursavich. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zapr
import (
"sync"
"sync/atomic"
"github.com/go-logr/logr"
"go.uber.org/zap"
)
var discard = LogSink(noopLogSink{})
type noopLogSink struct{}
func (noopLogSink) Init(info logr.RuntimeInfo) {}
func (noopLogSink) Enabled(level int) bool { return false }
func (noopLogSink) Info(level int, msg string, keysAndValues ...any) {}
func (noopLogSink) Error(err error, msg string, keysAndValues ...any) {}
func (noopLogSink) WithValues(keysAndValues ...any) logr.LogSink { return discard }
func (noopLogSink) WithName(name string) logr.LogSink { return discard }
func (noopLogSink) WithCallDepth(depth int) logr.LogSink { return discard }
func (noopLogSink) Underlying() *zap.Logger { return nil }
func (noopLogSink) Flush() error { return nil }
// LazyLogSink is a LogSink whose underlying implementation
// can be updated after it's been used to create log.Loggers.
type LazyLogSink interface {
LogSink
// SetSink sets the underlying LogSink for the LazyLogSink and all of
// its descendants created by WithDepth, WithName, or WithValues.
SetSink(LogSink)
}
// NewLazyLogSink returns a new Sink that discards logs until SetSink is called.
func NewLazyLogSink() LazyLogSink {
return newLazySink()
}
type lazySink struct {
sink atomic.Pointer[LogSink]
info logr.RuntimeInfo
mu sync.Mutex
set bool
name string
depth int
values []any
children []*lazySink
}
func newLazySink() *lazySink {
var s lazySink
s.sink.Store(&discard)
return &s
}
func (s *lazySink) Init(info logr.RuntimeInfo) {
info.CallDepth++
s.info = info
(*s.sink.Load()).Init(info)
}
func (s *lazySink) Enabled(level int) bool {
return (*s.sink.Load()).Enabled(level)
}
func (s *lazySink) Info(level int, msg string, keysAndValues ...any) {
(*s.sink.Load()).Info(level, msg, keysAndValues...)
}
func (s *lazySink) Error(err error, msg string, keysAndValues ...any) {
(*s.sink.Load()).Error(err, msg, keysAndValues...)
}
func (s *lazySink) WithValues(keysAndValues ...any) logr.LogSink {
s.mu.Lock()
defer s.mu.Unlock()
child := newLazySink()
child.values = append([]any(nil), keysAndValues...)
child.Init(s.info)
child.SetSink(*s.sink.Load())
s.children = append(s.children, child)
return child
}
func (s *lazySink) WithName(name string) logr.LogSink {
s.mu.Lock()
defer s.mu.Unlock()
child := newLazySink()
child.name = name
child.Init(s.info)
child.SetSink(*s.sink.Load())
s.children = append(s.children, child)
return child
}
func (s *lazySink) WithCallDepth(depth int) logr.LogSink {
s.mu.Lock()
defer s.mu.Unlock()
child := newLazySink()
child.depth = depth
child.Init(s.info)
child.SetSink(*s.sink.Load())
s.children = append(s.children, child)
return child
}
func (s *lazySink) Underlying() *zap.Logger {
return (*s.sink.Load()).Underlying()
}
func (s *lazySink) Flush() error {
return (*s.sink.Load()).Flush()
}
func (s *lazySink) SetSink(sink LogSink) {
s.mu.Lock()
defer s.mu.Unlock()
sink.Init(s.info)
if s.name != "" {
sink = sink.WithName(s.name).(LogSink)
}
if len(s.values) > 0 {
sink = sink.WithValues(s.values...).(LogSink)
}
if s.depth > 0 {
sink = sink.WithCallDepth(s.depth).(LogSink)
}
s.sink.Store(&sink)
for _, c := range s.children {
c.SetSink(sink)
}
}