Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zapslog: Support for a custom mapping of slog.Level to zap.Level #1413

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions exp/zapslog/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

"go.uber.org/zap"
"go.uber.org/zap/exp/zapslog"
"go.uber.org/zap/zapcore"
)

type Password string
Expand Down Expand Up @@ -69,6 +70,7 @@ func Example_slog() {
"foo": "bar",
}))
sl.LogAttrs(ctx, slog.LevelDebug, "not show up")
sl.LogAttrs(ctx, slog.LevelDebug, "not show up")

// Output:
// {"level":"info","msg":"user","name":"Al","secret":"REDACTED"}
Expand All @@ -77,3 +79,52 @@ func Example_slog() {
// {"level":"info","msg":"message","group":{"pi":3.14,"1min":"1m0s"}}
// {"level":"warn","msg":"warn msg","s":{"u":1,"m":{"foo":"bar"}}}
}

type exampleDpanicLeveler struct{}

func (c *exampleDpanicLeveler) ConvertLevel(l slog.Level) zapcore.Level {
switch {
case l >= slog.LevelError:
return zapcore.ErrorLevel
case l >= slog.LevelWarn:
return zapcore.WarnLevel
case l >= slog.LevelInfo:
return zapcore.InfoLevel
case l == -3:
return zapcore.DPanicLevel
case l == -5:
return zapcore.FatalLevel
default:
return zapcore.DebugLevel
}
}

func ExampleConvertLeveler() {
logger := zap.NewExample(zap.IncreaseLevel(zap.InfoLevel))
defer logger.Sync()

sl := slog.New(zapslog.NewHandler(logger.Core(), zapslog.WithConvertLeveler(&exampleDpanicLeveler{})))
ctx := context.Background()

sl.Info("user", "name", "Al", "secret", Password("secret"))
sl.Error("oops", "err", net.ErrClosed, "status", 500)
sl.LogAttrs(
ctx,
-3,
"oops",
slog.Any("err", net.ErrClosed),
slog.Int("status", 500),
)
sl.LogAttrs(
ctx,
-5,
"oops",
slog.Any("err", net.ErrClosed),
slog.Int("status", 500),
)
// Output:
// {"level":"info","msg":"user","name":"Al","secret":"REDACTED"}
// {"level":"error","msg":"oops","err":"use of closed network connection","status":500}
// {"level":"dpanic","msg":"oops","err":"use of closed network connection","status":500}
// {"level":"fatal","msg":"oops","err":"use of closed network connection","status":500}
}
22 changes: 4 additions & 18 deletions exp/zapslog/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Handler struct {
addCaller bool
addStackAt slog.Level
callerSkip int
leveler ConvertLeveler

// List of unapplied groups.
//
Expand All @@ -54,6 +55,7 @@ func NewHandler(core zapcore.Core, opts ...HandlerOption) *Handler {
h := &Handler{
core: core,
addStackAt: slog.LevelError,
leveler: &DefaultConvertLeveler{},
}
for _, v := range opts {
v.apply(h)
Expand Down Expand Up @@ -113,31 +115,15 @@ func convertAttrToField(attr slog.Attr) zapcore.Field {
}
}

// convertSlogLevel maps slog Levels to zap Levels.
// Note that there is some room between slog levels while zap levels are continuous, so we can't 1:1 map them.
// See also https://go.googlesource.com/proposal/+/master/design/56345-structured-logging.md?pli=1#levels
func convertSlogLevel(l slog.Level) zapcore.Level {
switch {
case l >= slog.LevelError:
return zapcore.ErrorLevel
case l >= slog.LevelWarn:
return zapcore.WarnLevel
case l >= slog.LevelInfo:
return zapcore.InfoLevel
default:
return zapcore.DebugLevel
}
}

// Enabled reports whether the handler handles records at the given level.
func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool {
return h.core.Enabled(convertSlogLevel(level))
return h.core.Enabled(h.leveler.ConvertLevel(level))
}

// Handle handles the Record.
func (h *Handler) Handle(ctx context.Context, record slog.Record) error {
ent := zapcore.Entry{
Level: convertSlogLevel(record.Level),
Level: h.leveler.ConvertLevel(record.Level),
Time: record.Time,
Message: record.Message,
LoggerName: h.name,
Expand Down
65 changes: 65 additions & 0 deletions exp/zapslog/leveler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) 2023 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

//go:build go1.21

package zapslog

import (
"log/slog"

"go.uber.org/zap/zapcore"
)

// ConvertLeveler maps from [log/slog.Level] to [go.uber.org/zap/zapcore.Level].
// Note that there is some room between slog levels while zap levels are continuous, so we can't 1:1 map them.
// See also [structured logging proposal]
//
// [structured logging proposal]: https://go.googlesource.com/proposal/+/master/design/56345-structured-logging.md?pli=1#levels
type ConvertLeveler interface {
ConvertLevel(l slog.Level) zapcore.Level
}

// DefaultConvertLeveler static maps from [log/slog.Level] to [go.uber.org/zap/zapcore.Level].
// implements: [go.uber.org/zap/exp/zapslog.ConvertLeveler]
type DefaultConvertLeveler struct{}

// ConvertLevel static maps from [log/slog.Level] to [go.uber.org/zap/zapcore.Level].
// - [log/slog.LevelError] to [go.uber.org/zap/zapcore.ErrorLevel]
// - [log/slog.LevelWarn] to [go.uber.org/zap/zapcore.WarnLevel]
// - [log/slog.LevelInfo] to [go.uber.org/zap/zapcore.InfoLevel]
// - [log/slog.LevelDebug] or default to [go.uber.org/zap/zapcore.DebugLevel]
//
// Note that there is some room between slog levels while zap levels are continuous, so we can't 1:1 map them.
// See also [structured logging proposal]
//
// [structured logging proposal]: https://go.googlesource.com/proposal/+/master/design/56345-structured-logging.md?pli=1#levels
func (c *DefaultConvertLeveler) ConvertLevel(l slog.Level) zapcore.Level {
switch {
case l >= slog.LevelError:
return zapcore.ErrorLevel
case l >= slog.LevelWarn:
return zapcore.WarnLevel
case l >= slog.LevelInfo:
return zapcore.InfoLevel
default:
return zapcore.DebugLevel
}
}
8 changes: 8 additions & 0 deletions exp/zapslog/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,11 @@ func AddStacktraceAt(lvl slog.Level) HandlerOption {
log.addStackAt = lvl
})
}

// WithConvertLeveler configures the ConvertLeveler to convert log levels
// from [log/slog.Level] to [go.uber.org/zap/zapcore.Level].
func WithConvertLeveler(leveler ConvertLeveler) HandlerOption {
return handlerOptionFunc(func(handler *Handler) {
handler.leveler = leveler
})
}