Debian Codegraph @

slogr.go

raw
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 )
25
26 // FromSlogHandler returns a Logger which writes to the slog.Handler.
27 //
28 // The logr verbosity level is mapped to slog levels such that V(0) becomes
29 // slog.LevelInfo and V(4) becomes slog.LevelDebug.
30 func FromSlogHandler(handler slog.Handler) Logger {
31 if handler, ok := handler.(*slogHandler); ok {
32 if handler.sink == nil {
33 return Discard()
34 }
35 return New(handler.sink).V(int(handler.levelBias))
36 }
37 return New(&slogSink{handler: handler})
38 }
39
40 // ToSlogHandler returns a slog.Handler which writes to the same sink as the Logger.
41 //
42 // The returned logger writes all records with level >= slog.LevelError as
43 // error log entries with LogSink.Error, regardless of the verbosity level of
44 // the Logger:
45 //
46 // logger := <some Logger with 0 as verbosity level>
47 // slog.New(ToSlogHandler(logger.V(10))).Error(...) -> logSink.Error(...)
48 //
49 // The level of all other records gets reduced by the verbosity
50 // level of the Logger and the result is negated. If it happens
51 // to be negative, then it gets replaced by zero because a LogSink
52 // is not expected to handled negative levels:
53 //
54 // slog.New(ToSlogHandler(logger)).Debug(...) -> logger.GetSink().Info(level=4, ...)
55 // slog.New(ToSlogHandler(logger)).Warning(...) -> logger.GetSink().Info(level=0, ...)
56 // slog.New(ToSlogHandler(logger)).Info(...) -> logger.GetSink().Info(level=0, ...)
57 // slog.New(ToSlogHandler(logger.V(4))).Info(...) -> logger.GetSink().Info(level=4, ...)
58 func ToSlogHandler(logger Logger) slog.Handler {
59 if sink, ok := logger.GetSink().(*slogSink); ok && logger.GetV() == 0 {
60 return sink.handler
61 }
62
63 handler := &slogHandler{sink: logger.GetSink(), levelBias: slog.Level(logger.GetV())}
64 if slogSink, ok := handler.sink.(SlogSink); ok {
65 handler.slogSink = slogSink
66 }
67 return handler
68 }
69
70 // SlogSink is an optional interface that a LogSink can implement to support
71 // logging through the slog.Logger or slog.Handler APIs better. It then should
72 // also support special slog values like slog.Group. When used as a
73 // slog.Handler, the advantages are:
74 //
75 // - stack unwinding gets avoided in favor of logging the pre-recorded PC,
76 // as intended by slog
77 // - proper grouping of key/value pairs via WithGroup
78 // - verbosity levels > slog.LevelInfo can be recorded
79 // - less overhead
80 //
81 // Both APIs (Logger and slog.Logger/Handler) then are supported equally
82 // well. Developers can pick whatever API suits them better and/or mix
83 // packages which use either API in the same binary with a common logging
84 // implementation.
85 //
86 // This interface is necessary because the type implementing the LogSink
87 // interface cannot also implement the slog.Handler interface due to the
88 // different prototype of the common Enabled method.
89 //
90 // An implementation could support both interfaces in two different types, but then
91 // additional interfaces would be needed to convert between those types in FromSlogHandler
92 // and ToSlogHandler.
93 type SlogSink interface {
94 LogSink
95
96 Handle(ctx context.Context, record slog.Record) error
97 WithAttrs(attrs []slog.Attr) SlogSink
98 WithGroup(name string) SlogSink
99 }