You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This enables the use of the logr.Logger API on top of a slog.Handler. The other
direction is also possible, but some information is lost. Instead, logr
implementations should better get extended for usage as slog.Handler.
Going back and forth between the two APIs is now part of logr.
Copy file name to clipboardExpand all lines: README.md
+82-1Lines changed: 82 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -74,6 +74,28 @@ received:
74
74
If the Go standard library had defined an interface for logging, this project
75
75
probably would not be needed. Alas, here we are.
76
76
77
+
When the Go developers started developing such an interface with
78
+
[slog](https://github.com/golang/go/issues/56345), they adopted some of the
79
+
logr design but also left out some parts and changed others:
80
+
81
+
| Feature | logr | slog |
82
+
|---------|------|------|
83
+
| High-level API |`Logger` (passed by value) |`Logger` (passed by [pointer](https://github.com/golang/go/issues/59126)) |
84
+
| Low-level API |`LogSink`|`Handler`|
85
+
| Stack unwinding | done by `LogSink`| done by `Logger`|
86
+
| Skipping helper functions |`WithCallDepth`, `WithCallStackHelper`|[not supported by Logger](https://github.com/golang/go/issues/59145)|
87
+
| Generating a value for logging on demand |`Marshaler`|`LogValuer`|
88
+
| Log levels | >= 0, higher meaning "less important" | positive and negative, with 0 for "info" and higher meaning "more important" |
89
+
| Error log entries | always logged, don't have a verbosity level | normal log entries with level >= `LevelError`|
90
+
| Passing logger via context |`NewContext`, `FromContext`| no API |
91
+
| Adding a name to a logger |`WithName`| no API |
92
+
| Grouping of key/value pairs | not supported |`WithGroup`, `GroupValue`|
93
+
94
+
The high-level slog API is explicitly meant to be one of many different APIs
95
+
that can be layered on top of a shared `slog.Handler`. logr is one such
96
+
alternative API, with interoperability as [described
97
+
below](#slog-interoperability).
98
+
77
99
### Inspiration
78
100
79
101
Before you consider this package, please read [this blog post by the
@@ -119,6 +141,63 @@ There are implementations for the following logging libraries:
119
141
-**github.com/go-kit/log**: [gokitlogr](https://github.com/tonglil/gokitlogr) (also compatible with github.com/go-kit/kit/log since v0.12.0)
120
142
-**bytes.Buffer** (writing to a buffer): [bufrlogr](https://github.com/tonglil/buflogr) (useful for ensuring values were logged, like during testing)
121
143
144
+
## slog interoperability
145
+
146
+
Interoperability goes both ways, using the `logr.Logger` API with a `slog.Handler`
147
+
and using the `slog.Logger` API with a `logr.LogSink`. logr provides `ToSlog` and
148
+
`FromSlog` API calls to convert between a `logr.Logger` and a `slog.Logger`. Because
149
+
the `slog.Logger` API is optional, there are also variants of these calls which
150
+
work directly with a `slog.Handler`.
151
+
152
+
Ideally, the backend should support both logr and slog. In that case, log calls
153
+
can go from the high-level API to the backend with no intermediate glue
154
+
code. Because of a conflict in the parameters of the common Enabled method, it
155
+
is [not possible to implement both interfaces in the same
156
+
type](https://github.com/golang/go/issues/59110). A second type and methods for
157
+
converting from one type to the other are needed. Here is an example:
158
+
159
+
```
160
+
// logSink implements logr.LogSink and logr.SlogImplementor.
@@ -242,7 +321,9 @@ Otherwise, you can start out with `0` as "you always want to see this",
242
321
243
322
Then gradually choose levels in between as you need them, working your way
244
323
down from 10 (for debug and trace style logs) and up from 1 (for chattier
245
-
info-type logs.)
324
+
info-type logs). For reference, slog pre-defines -4 for debug logs
325
+
(corresponds to 4 in logr), which matches what is
326
+
[recommended for Kubernetes](https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md#what-method-to-use).
0 commit comments