| 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 |
"runtime" |
| 25 |
"time" |
| 26 |
) |
| 27 |
|
| 28 |
var ( |
| 29 |
_ LogSink = &slogSink{} |
| 30 |
_ CallDepthLogSink = &slogSink{} |
| 31 |
_ Underlier = &slogSink{} |
| 32 |
) |
| 33 |
|
| 34 |
// Underlier is implemented by the LogSink returned by NewFromLogHandler. |
| 35 |
type Underlier interface { |
| 36 |
// GetUnderlying returns the Handler used by the LogSink. |
| 37 |
GetUnderlying() slog.Handler |
| 38 |
} |
| 39 |
|
| 40 |
const ( |
| 41 |
// nameKey is used to log the `WithName` values as an additional attribute. |
| 42 |
nameKey = "logger" |
| 43 |
|
| 44 |
// errKey is used to log the error parameter of Error as an additional attribute. |
| 45 |
errKey = "err" |
| 46 |
) |
| 47 |
|
| 48 |
type slogSink struct { |
| 49 |
callDepth int |
| 50 |
name string |
| 51 |
handler slog.Handler |
| 52 |
} |
| 53 |
|
| 54 |
func (l *slogSink) Init(info RuntimeInfo) { |
| 55 |
l.callDepth = info.CallDepth |
| 56 |
} |
| 57 |
|
| 58 |
func (l *slogSink) GetUnderlying() slog.Handler { |
| 59 |
return l.handler |
| 60 |
} |
| 61 |
|
| 62 |
func (l *slogSink) WithCallDepth(depth int) LogSink { |
| 63 |
newLogger := *l |
| 64 |
newLogger.callDepth += depth |
| 65 |
return &newLogger |
| 66 |
} |
| 67 |
|
| 68 |
func (l *slogSink) Enabled(level int) bool { |
| 69 |
return l.handler.Enabled(context.Background(), slog.Level(-level)) |
| 70 |
} |
| 71 |
|
| 72 |
func (l *slogSink) Info(level int, msg string, kvList ...interface{}) { |
| 73 |
l.log(nil, msg, slog.Level(-level), kvList...) |
| 74 |
} |
| 75 |
|
| 76 |
func (l *slogSink) Error(err error, msg string, kvList ...interface{}) { |
| 77 |
l.log(err, msg, slog.LevelError, kvList...) |
| 78 |
} |
| 79 |
|
| 80 |
func (l *slogSink) log(err error, msg string, level slog.Level, kvList ...interface{}) { |
| 81 |
var pcs [1]uintptr |
| 82 |
// skip runtime.Callers, this function, Info/Error, and all helper functions above that. |
| 83 |
runtime.Callers(3+l.callDepth, pcs[:]) |
| 84 |
|
| 85 |
record := slog.NewRecord(time.Now(), level, msg, pcs[0]) |
| 86 |
if l.name != "" { |
| 87 |
record.AddAttrs(slog.String(nameKey, l.name)) |
| 88 |
} |
| 89 |
if err != nil { |
| 90 |
record.AddAttrs(slog.Any(errKey, err)) |
| 91 |
} |
| 92 |
record.Add(kvList...) |
| 93 |
_ = l.handler.Handle(context.Background(), record) |
| 94 |
} |
| 95 |
|
| 96 |
func (l slogSink) WithName(name string) LogSink { |
| 97 |
if l.name != "" { |
| 98 |
l.name += "/" |
| 99 |
} |
| 100 |
l.name += name |
| 101 |
return &l |
| 102 |
} |
| 103 |
|
| 104 |
func (l slogSink) WithValues(kvList ...interface{}) LogSink { |
| 105 |
l.handler = l.handler.WithAttrs(kvListToAttrs(kvList...)) |
| 106 |
return &l |
| 107 |
} |
| 108 |
|
| 109 |
func kvListToAttrs(kvList ...interface{}) []slog.Attr { |
| 110 |
// We don't need the record itself, only its Add method. |
| 111 |
record := slog.NewRecord(time.Time{}, 0, "", 0) |
| 112 |
record.Add(kvList...) |
| 113 |
attrs := make([]slog.Attr, 0, record.NumAttrs()) |
| 114 |
record.Attrs(func(attr slog.Attr) bool { |
| 115 |
attrs = append(attrs, attr) |
| 116 |
return true |
| 117 |
}) |
| 118 |
return attrs |
| 119 |
} |