| 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 funcr |
| 20 |
|
| 21 |
import ( |
| 22 |
"context" |
| 23 |
"log/slog" |
| 24 |
|
| 25 |
"github.com/go-logr/logr" |
| 26 |
) |
| 27 |
|
| 28 |
var _ logr.SlogSink = &fnlogger{} |
| 29 |
|
| 30 |
const extraSlogSinkDepth = 3 // 2 for slog, 1 for SlogSink |
| 31 |
|
| 32 |
func (l fnlogger) Handle(_ context.Context, record slog.Record) error { |
| 33 |
kvList := make([]any, 0, 2*record.NumAttrs()) |
| 34 |
record.Attrs(func(attr slog.Attr) bool { |
| 35 |
kvList = attrToKVs(attr, kvList, l.opts.MaxLogDepth) |
| 36 |
return true |
| 37 |
}) |
| 38 |
|
| 39 |
if record.Level >= slog.LevelError { |
| 40 |
l.WithCallDepth(extraSlogSinkDepth).Error(nil, record.Message, kvList...) |
| 41 |
} else { |
| 42 |
level := l.levelFromSlog(record.Level) |
| 43 |
l.WithCallDepth(extraSlogSinkDepth).Info(level, record.Message, kvList...) |
| 44 |
} |
| 45 |
return nil |
| 46 |
} |
| 47 |
|
| 48 |
func (l fnlogger) WithAttrs(attrs []slog.Attr) logr.SlogSink { |
| 49 |
kvList := make([]any, 0, 2*len(attrs)) |
| 50 |
for _, attr := range attrs { |
| 51 |
kvList = attrToKVs(attr, kvList, l.opts.MaxLogDepth) |
| 52 |
} |
| 53 |
l.AddValues(kvList) |
| 54 |
return &l |
| 55 |
} |
| 56 |
|
| 57 |
func (l fnlogger) WithGroup(name string) logr.SlogSink { |
| 58 |
l.startGroup(name) |
| 59 |
return &l |
| 60 |
} |
| 61 |
|
| 62 |
// attrToKVs appends a slog.Attr to a logr-style kvList. It handle slog Groups |
| 63 |
// and other details of slog. maxDepth bounds recursion into nested groups so a |
| 64 |
// deeply-nested slog.Group cannot exhaust the stack; it is decremented per group |
| 65 |
// level and starts at the Formatter's MaxLogDepth (past which the formatter would |
| 66 |
// truncate the rendering anyway). |
| 67 |
func attrToKVs(attr slog.Attr, kvList []any, maxDepth int) []any { |
| 68 |
attrVal := attr.Value.Resolve() |
| 69 |
if attrVal.Kind() == slog.KindGroup { |
| 70 |
if maxDepth <= 0 { |
| 71 |
// Nesting is too deep to build without risking a stack overflow. |
| 72 |
// Stop here; the formatter truncates below MaxLogDepth regardless. |
| 73 |
if attr.Key != "" { |
| 74 |
kvList = append(kvList, attr.Key, "<max-log-depth-exceeded>") |
| 75 |
} |
| 76 |
return kvList |
| 77 |
} |
| 78 |
groupVal := attrVal.Group() |
| 79 |
grpKVs := make([]any, 0, 2*len(groupVal)) |
| 80 |
for _, attr := range groupVal { |
| 81 |
grpKVs = attrToKVs(attr, grpKVs, maxDepth-1) |
| 82 |
} |
| 83 |
if attr.Key == "" { |
| 84 |
// slog says we have to inline these |
| 85 |
kvList = append(kvList, grpKVs...) |
| 86 |
} else { |
| 87 |
kvList = append(kvList, attr.Key, PseudoStruct(grpKVs)) |
| 88 |
} |
| 89 |
} else if attr.Key != "" { |
| 90 |
kvList = append(kvList, attr.Key, attrVal.Any()) |
| 91 |
} |
| 92 |
|
| 93 |
return kvList |
| 94 |
} |
| 95 |
|
| 96 |
// levelFromSlog adjusts the level by the logger's verbosity and negates it. |
| 97 |
// It ensures that the result is >= 0. This is necessary because the result is |
| 98 |
// passed to a LogSink and that API did not historically document whether |
| 99 |
// levels could be negative or what that meant. |
| 100 |
// |
| 101 |
// Some example usage: |
| 102 |
// |
| 103 |
// logrV0 := getMyLogger() |
| 104 |
// logrV2 := logrV0.V(2) |
| 105 |
// slogV2 := slog.New(logr.ToSlogHandler(logrV2)) |
| 106 |
// slogV2.Debug("msg") // =~ logrV2.V(4) =~ logrV0.V(6) |
| 107 |
// slogV2.Info("msg") // =~ logrV2.V(0) =~ logrV0.V(2) |
| 108 |
// slogv2.Warn("msg") // =~ logrV2.V(-4) =~ logrV0.V(0) |
| 109 |
func (l fnlogger) levelFromSlog(level slog.Level) int { |
| 110 |
result := -level |
| 111 |
if result < 0 { |
| 112 |
result = 0 // because LogSink doesn't expect negative V levels |
| 113 |
} |
| 114 |
return int(result) |
| 115 |
} |