Skip to content

Commit

Permalink
slogr: add glue code for logging to slog.Handler and with slog.Logger
Browse files Browse the repository at this point in the history
Interoperability with log/slog from Go 1.21 includes the ability to use a
slog.Handler as backend with logr.Logger as frontend and vice versa.

This is only the initial step. In particular writing with slog.Logger to
a logr.LogSink is not working well (time stamp and call site from
record get ignored). Further work is needed to improve this.
  • Loading branch information
pohly committed Aug 7, 2023
1 parent 76962d7 commit 3c7cd51
Show file tree
Hide file tree
Showing 6 changed files with 518 additions and 1 deletion.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,29 @@ received:
If the Go standard library had defined an interface for logging, this project
probably would not be needed. Alas, here we are.

When the Go developers started developing such an interface with
[slog](https://github.com/golang/go/issues/56345), they adopted some of the
logr design but also left out some parts and changed others:

| Feature | logr | slog |
|---------|------|------|
| High-level API | `Logger` (passed by value) | `Logger` (passed by [pointer](https://github.com/golang/go/issues/59126)) |
| Low-level API | `LogSink` | `Handler` |
| Stack unwinding | done by `LogSink` | done by `Logger` |
| Skipping helper functions | `WithCallDepth`, `WithCallStackHelper` | [not supported by Logger](https://github.com/golang/go/issues/59145) |
| Generating a value for logging on demand | `Marshaler` | `LogValuer` |
| Log levels | >= 0, higher meaning "less important" | positive and negative, with 0 for "info" and higher meaning "more important" |
| Error log entries | always logged, don't have a verbosity level | normal log entries with level >= `LevelError` |
| Passing logger via context | `NewContext`, `FromContext` | no API |
| Adding a name to a logger | `WithName` | no API |
| Modify verbosity of log entries in a call chain | `V` | no API |
| Grouping of key/value pairs | not supported | `WithGroup`, `GroupValue` |

The high-level slog API is explicitly meant to be one of many different APIs
that can be layered on top of a shared `slog.Handler`. logr is one such
alternative API, with interoperability provided by the [`slogr`](slogr)
package.

### Inspiration

Before you consider this package, please read [this blog post by the
Expand Down Expand Up @@ -242,7 +265,9 @@ Otherwise, you can start out with `0` as "you always want to see this",

Then gradually choose levels in between as you need them, working your way
down from 10 (for debug and trace style logs) and up from 1 (for chattier
info-type logs.)
info-type logs). For reference, slog pre-defines -4 for debug logs
(corresponds to 4 in logr), which matches what is
[recommended for Kubernetes](https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md#what-method-to-use).

#### How do I choose my keys?

Expand Down
45 changes: 45 additions & 0 deletions slogr/discardhandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//go:build go1.21
// +build go1.21

/*
Copyright 2023 The logr Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package slogr

import (
"context"
"log/slog"
)

type discardHandler struct{}

var _ slog.Handler = &discardHandler{}

func (d *discardHandler) Enabled(ctx context.Context, level slog.Level) bool {
return false
}

func (d *discardHandler) Handle(ctx context.Context, record slog.Record) error {
return nil
}

func (d *discardHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return d
}

func (d *discardHandler) WithGroup(name string) slog.Handler {
return d
}
104 changes: 104 additions & 0 deletions slogr/sloghandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//go:build go1.21
// +build go1.21

/*
Copyright 2023 The logr Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package slogr

import (
"context"
"log/slog"

"github.com/go-logr/logr"
)

type slogHandler struct {
sink logr.LogSink
groupPrefix string
level slog.Level
}

var _ slog.Handler = &slogHandler{}

// groupSeparator is used to concatenate WithGroup names and attribute keys.
const groupSeparator = "."

func (l *slogHandler) Enabled(ctx context.Context, level slog.Level) bool {
return l.sink != nil && (level >= slog.LevelError || l.sink.Enabled(l.levelFromSlog(level)))
}

func (l *slogHandler) Handle(ctx context.Context, record slog.Record) error {
// No need to check for nil sink here because Handle will only be called
// when Enabled returned true.

kvList := make([]any, 0, 2*record.NumAttrs())
record.Attrs(func(attr slog.Attr) bool {
if attr.Key != "" {
kvList = append(kvList, l.addGroupPrefix(attr.Key), attr.Value.Resolve().Any())
}
return true
})
if record.Level >= slog.LevelError {
l.sink.Error(nil, record.Message, kvList...)
} else {
level := l.levelFromSlog(record.Level)
l.sink.Info(level, record.Message, kvList...)
}
return nil
}

func (l *slogHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
if l.sink == nil || len(attrs) == 0 {
return l
}
kvList := make([]any, 0, 2*len(attrs))
for _, attr := range attrs {
if attr.Key != "" {
kvList = append(kvList, l.addGroupPrefix(attr.Key), attr.Value.Resolve().Any())
}
}
copy := *l
copy.sink = l.sink.WithValues(kvList...)
return &copy
}

func (l *slogHandler) WithGroup(name string) slog.Handler {
if l.sink == nil {
return l
}
copy := *l
copy.groupPrefix = copy.addGroupPrefix(name)
return &copy
}

func (l *slogHandler) addGroupPrefix(name string) string {
if l.groupPrefix == "" {
return name
}
return l.groupPrefix + groupSeparator + name
}

func (l *slogHandler) levelFromSlog(level slog.Level) int {
// Adjust by the per-logger verbosity level.
level -= l.level
if level >= 0 {
// logr has no level lower than 0, so we have to truncate.
// LogSink implementations might not expect a negative value.
return 0
}
return int(-level)
}
46 changes: 46 additions & 0 deletions slogr/slogr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//go:build go1.21
// +build go1.21

/*
Copyright 2023 The logr Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package slogr

import (
"log/slog"

"github.com/go-logr/logr"
)

// NewLogr returns a logr.Logger which writes to the same handler as the slog.Logger.
//
// In the output the logr verbosity level gets negated, so V(4) becomes
// slog.LevelDebug.
func NewLogr(logger *slog.Logger) logr.Logger {
return logr.New(&slogSink{handler: logger.Handler()})
}

// NewSlog returns a slog.Handler which writes to the same sink as the logr.Logger.
//
// The returned logger writes all records with level >= slog.LevelError as
// error log entries with LogSink.Error, regardless of the verbosity of the
// logr.Logger. The level of all other records gets reduced by the verbosity
// level of the logr.Logger, so a slog.Logger.Info call gets written with
// slog.LevelDebug when using a logr.Logger where verbosity was modified with
// V(4).
func NewSlog(logger logr.Logger) slog.Handler {
return &slogHandler{sink: logger.GetSink(), level: slog.Level(logger.GetV())}
}

0 comments on commit 3c7cd51

Please sign in to comment.