Skip to content

Commit 9ac9983

Browse files
committed
document slog support in logr package
The old API isn't going away, so the matching functions cross-reference each other.
1 parent eea3488 commit 9ac9983

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ logr design but also left out some parts and changed others:
9595
The high-level slog API is explicitly meant to be one of many different APIs
9696
that can be layered on top of a shared `slog.Handler`. logr is one such
9797
alternative API, with [interoperability](#slog-interoperability) provided by the [`slogr`](slogr)
98+
package and (since 1.4.0) by corresponding functionality in the main `logr`
9899
package.
99100

100101
### Inspiration
@@ -145,11 +146,12 @@ There are implementations for the following logging libraries:
145146
## slog interoperability
146147

147148
Interoperability goes both ways, using the `logr.Logger` API with a `slog.Handler`
148-
and using the `slog.Logger` API with a `logr.LogSink`. [slogr](./slogr) provides `NewLogr` and
149-
`NewSlogHandler` API calls to convert between a `logr.Logger` and a `slog.Handler`.
149+
and using the `slog.Logger` API with a `logr.LogSink`. [slogr](./slogr) provides `NewLogr` (= `logr.NewFromSlogHandler`) and
150+
`NewSlogHandler` (= `logr.NewSlogHandler`) API calls to convert between a `logr.Logger` and a `slog.Handler`.
150151
As usual, `slog.New` can be used to wrap such a `slog.Handler` in the high-level
151152
slog API. `slogr` itself leaves that to the caller.
152153

154+
153155
## Using a `logr.Sink` as backend for slog
154156

155157
Ideally, a logr sink implementation should support both logr and slog by

slogr.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import (
2828
//
2929
// The logr verbosity level is mapped to slog levels such that V(0) becomes
3030
// slog.LevelInfo and V(4) becomes slog.LevelDebug.
31+
//
32+
// NewFromSlogHandler is identical to [slogr.NewLogr]. Either of these can be used.
3133
func NewFromSlogHandler(handler slog.Handler) Logger {
3234
if handler, ok := handler.(*slogHandler); ok {
3335
if handler.sink == nil {
@@ -56,6 +58,8 @@ func NewFromSlogHandler(handler slog.Handler) Logger {
5658
// slog.New(NewSlogHandler(logger)).Warning(...) -> logger.GetSink().Info(level=0, ...)
5759
// slog.New(NewSlogHandler(logger)).Info(...) -> logger.GetSink().Info(level=0, ...)
5860
// slog.New(NewSlogHandler(logger.V(4))).Info(...) -> logger.GetSink().Info(level=4, ...)
61+
//
62+
// NewSlogHandler is identical to [slogr.NewSlogHandler]. Either of these can be used.
5963
func NewSlogHandler(logger Logger) slog.Handler {
6064
if sink, ok := logger.GetSink().(*slogSink); ok && logger.GetV() == 0 {
6165
return sink.handler
@@ -91,6 +95,8 @@ func NewSlogHandler(logger Logger) slog.Handler {
9195
// An implementation could support both interfaces in two different types, but then
9296
// additional interfaces would be needed to convert between those types in NewLogr
9397
// and NewSlogHandler.
98+
//
99+
// SlogSink is identical to [slogr.SlogSink]. Either of these can be used.
94100
type SlogSink interface {
95101
LogSink
96102

slogr/slogr.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ limitations under the License.
2222
// APIs.
2323
//
2424
// See the README in the top-level [./logr] package for a discussion of
25-
// interoperability.
25+
// interoperability. The same functionality is also available through the main
26+
// logr package.
2627
package slogr
2728

2829
import (
@@ -35,6 +36,9 @@ import (
3536
//
3637
// The logr verbosity level is mapped to slog levels such that V(0) becomes
3738
// slog.LevelInfo and V(4) becomes slog.LevelDebug.
39+
//
40+
// NewLogr is identical to [logr.NewFromSlogHandler]. Either of these can
41+
// be used.
3842
func NewLogr(handler slog.Handler) logr.Logger {
3943
return logr.NewFromSlogHandler(handler)
4044
}
@@ -57,6 +61,9 @@ func NewLogr(handler slog.Handler) logr.Logger {
5761
// slog.New(NewSlogHandler(logger)).Warning(...) -> logger.GetSink().Info(level=0, ...)
5862
// slog.New(NewSlogHandler(logger)).Info(...) -> logger.GetSink().Info(level=0, ...)
5963
// slog.New(NewSlogHandler(logger.V(4))).Info(...) -> logger.GetSink().Info(level=4, ...)
64+
//
65+
// NewSlogHandler is identical to [logr.NewSlogHandler]. Either of these can
66+
// be used.
6067
func NewSlogHandler(logger logr.Logger) slog.Handler {
6168
return logr.NewSlogHandler(logger)
6269
}
@@ -84,4 +91,6 @@ func NewSlogHandler(logger logr.Logger) slog.Handler {
8491
// An implementation could support both interfaces in two different types, but then
8592
// additional interfaces would be needed to convert between those types in NewLogr
8693
// and NewSlogHandler.
94+
//
95+
// SlogSink is identical to [logr.SlogSink]. Either of these can be used.
8796
type SlogSink = logr.SlogSink

0 commit comments

Comments
 (0)