| 1 |
//go:build go1.21 |
| 2 |
|
| 3 |
/* |
| 4 |
Copyright 2023 The logr Authors. |
| 5 |
|
| 6 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
you may not use this file except in compliance with the License. |
| 8 |
You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, software |
| 13 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
See the License for the specific language governing permissions and |
| 16 |
limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
package logr |
| 20 |
|
| 21 |
import ( |
| 22 |
"context" |
| 23 |
"log/slog" |
| 24 |
"time" |
| 25 |
) |
| 26 |
|
| 27 |
var _ SlogSink = &testSlogSink{} |
| 28 |
|
| 29 |
// testSlogSink is a trivial SlogSink implementation, just for testing, which |
| 30 |
// calls (optional) hooks on each method. |
| 31 |
type testSlogSink struct { |
| 32 |
// embed a plain LogSink |
| 33 |
testLogSink |
| 34 |
|
| 35 |
attrs []slog.Attr |
| 36 |
groups []string |
| 37 |
|
| 38 |
fnHandle func(ss *testSlogSink, ctx context.Context, record slog.Record) |
| 39 |
fnWithAttrs func(ss *testSlogSink, attrs []slog.Attr) |
| 40 |
fnWithGroup func(ss *testSlogSink, name string) |
| 41 |
} |
| 42 |
|
| 43 |
func (ss *testSlogSink) Handle(ctx context.Context, record slog.Record) error { |
| 44 |
if ss.fnHandle != nil { |
| 45 |
ss.fnHandle(ss, ctx, record) |
| 46 |
} |
| 47 |
return nil |
| 48 |
} |
| 49 |
|
| 50 |
func (ss *testSlogSink) WithAttrs(attrs []slog.Attr) SlogSink { |
| 51 |
if ss.fnWithAttrs != nil { |
| 52 |
ss.fnWithAttrs(ss, attrs) |
| 53 |
} |
| 54 |
out := *ss |
| 55 |
n := len(out.attrs) |
| 56 |
out.attrs = append(out.attrs[:n:n], attrs...) |
| 57 |
return &out |
| 58 |
} |
| 59 |
|
| 60 |
func (ss *testSlogSink) WithGroup(name string) SlogSink { |
| 61 |
if ss.fnWithGroup != nil { |
| 62 |
ss.fnWithGroup(ss, name) |
| 63 |
} |
| 64 |
out := *ss |
| 65 |
n := len(out.groups) |
| 66 |
out.groups = append(out.groups[:n:n], name) |
| 67 |
return &out |
| 68 |
} |
| 69 |
|
| 70 |
// passthruLogSink is a trivial LogSink implementation, which implements the |
| 71 |
// logr.LogSink methods in terms of a slog.Handler. |
| 72 |
type passthruLogSink struct { |
| 73 |
handler slog.Handler |
| 74 |
} |
| 75 |
|
| 76 |
func (pl passthruLogSink) Init(RuntimeInfo) {} |
| 77 |
|
| 78 |
func (pl passthruLogSink) Enabled(int) bool { return true } |
| 79 |
|
| 80 |
func (pl passthruLogSink) Error(_ error, msg string, kvList ...interface{}) { |
| 81 |
var record slog.Record |
| 82 |
record.Message = msg |
| 83 |
record.Level = slog.LevelError |
| 84 |
record.Time = time.Now() |
| 85 |
record.Add(kvList...) |
| 86 |
_ = pl.handler.Handle(context.Background(), record) |
| 87 |
} |
| 88 |
|
| 89 |
func (pl passthruLogSink) Info(_ int, msg string, kvList ...interface{}) { |
| 90 |
var record slog.Record |
| 91 |
record.Message = msg |
| 92 |
record.Level = slog.LevelInfo |
| 93 |
record.Time = time.Now() |
| 94 |
record.Add(kvList...) |
| 95 |
_ = pl.handler.Handle(context.Background(), record) |
| 96 |
} |
| 97 |
|
| 98 |
func (pl passthruLogSink) WithName(string) LogSink { return &pl } |
| 99 |
|
| 100 |
func (pl passthruLogSink) WithValues(kvList ...interface{}) LogSink { |
| 101 |
var values slog.Record |
| 102 |
values.Add(kvList...) |
| 103 |
var attrs []slog.Attr |
| 104 |
add := func(attr slog.Attr) bool { |
| 105 |
attrs = append(attrs, attr) |
| 106 |
return true |
| 107 |
} |
| 108 |
values.Attrs(add) |
| 109 |
|
| 110 |
pl.handler = pl.handler.WithAttrs(attrs) |
| 111 |
return &pl |
| 112 |
} |
| 113 |
|
| 114 |
// passthruSlogSink is a trivial SlogSink implementation, which stubs out the |
| 115 |
// logr.LogSink methods and passes Logr.SlogSink thru to a slog.Handler. |
| 116 |
type passthruSlogSink struct { |
| 117 |
handler slog.Handler |
| 118 |
} |
| 119 |
|
| 120 |
func (ps passthruSlogSink) Init(RuntimeInfo) {} |
| 121 |
func (ps passthruSlogSink) Enabled(int) bool { return true } |
| 122 |
func (ps passthruSlogSink) Error(error, string, ...interface{}) {} |
| 123 |
func (ps passthruSlogSink) Info(int, string, ...interface{}) {} |
| 124 |
func (ps passthruSlogSink) WithName(string) LogSink { return &ps } |
| 125 |
func (ps passthruSlogSink) WithValues(...interface{}) LogSink { return &ps } |
| 126 |
|
| 127 |
func (ps *passthruSlogSink) Handle(ctx context.Context, record slog.Record) error { |
| 128 |
return ps.handler.Handle(ctx, record) |
| 129 |
} |
| 130 |
|
| 131 |
func (ps passthruSlogSink) WithAttrs(attrs []slog.Attr) SlogSink { |
| 132 |
ps.handler = ps.handler.WithAttrs(attrs) |
| 133 |
return &ps |
| 134 |
} |
| 135 |
|
| 136 |
func (ps passthruSlogSink) WithGroup(name string) SlogSink { |
| 137 |
ps.handler = ps.handler.WithGroup(name) |
| 138 |
return &ps |
| 139 |
} |