zapslog

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2024 License: MIT Imports: 6 Imported by: 71

Documentation

Overview

Package zapslog provides an implementation of slog.Handler which writes to the supplied zapcore.Core.

Use of this package requires at least Go 1.21.

Example (Slog)
//go:build go1.21

package main

import (
	"context"
	"log/slog"
	"net"
	"time"

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

type Password string

func (p Password) LogValue() slog.Value {
	return slog.StringValue("REDACTED")
}

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

	sl := slog.New(zapslog.NewHandler(logger.Core()))
	ctx := context.Background()

	sl.Info("user", "name", "Al", "secret", Password("secret"))
	sl.Error("oops", "err", net.ErrClosed, "status", 500)
	sl.LogAttrs(
		ctx,
		slog.LevelError,
		"oops",
		slog.Any("err", net.ErrClosed),
		slog.Int("status", 500),
	)
	sl.Info("message",
		slog.Group("group",
			slog.Float64("pi", 3.14),
			slog.Duration("1min", time.Minute),
		),
	)
	sl.WithGroup("s").LogAttrs(
		ctx,
		slog.LevelWarn,
		"warn msg", // message
		slog.Uint64("u", 1),
		slog.Any("m", map[string]any{
			"foo": "bar",
		}))
	sl.LogAttrs(ctx, slog.LevelDebug, "not show up")

}
Output:

{"level":"info","msg":"user","name":"Al","secret":"REDACTED"}
{"level":"error","msg":"oops","err":"use of closed network connection","status":500}
{"level":"error","msg":"oops","err":"use of closed network connection","status":500}
{"level":"info","msg":"message","group":{"pi":3.14,"1min":"1m0s"}}
{"level":"warn","msg":"warn msg","s":{"u":1,"m":{"foo":"bar"}}}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler implements the slog.Handler by writing to a zap Core.

func NewHandler

func NewHandler(core zapcore.Core, opts ...HandlerOption) *Handler

NewHandler builds a Handler that writes to the supplied zapcore.Core with options.

func (*Handler) Enabled

func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool

Enabled reports whether the handler handles records at the given level.

func (*Handler) Handle

func (h *Handler) Handle(ctx context.Context, record slog.Record) error

Handle handles the Record.

func (*Handler) WithAttrs

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a new Handler whose attributes consist of both the receiver's attributes and the arguments.

func (*Handler) WithGroup

func (h *Handler) WithGroup(group string) slog.Handler

WithGroup returns a new Handler with the given group appended to the receiver's existing groups.

type HandlerOption

type HandlerOption interface {
	// contains filtered or unexported methods
}

A HandlerOption configures a slog Handler.

func AddStacktraceAt

func AddStacktraceAt(lvl slog.Level) HandlerOption

AddStacktraceAt configures the Logger to record a stack trace for all messages at or above a given level.

func WithCaller

func WithCaller(enabled bool) HandlerOption

WithCaller configures the Logger to include the filename and line number of the caller in log messages--if available.

func WithCallerSkip

func WithCallerSkip(skip int) HandlerOption

WithCallerSkip increases the number of callers skipped by caller annotation (as enabled by the WithCaller option).

When building wrappers around the Logger, supplying this Option prevents Zap from always reporting the wrapper code as the caller.

func WithName

func WithName(name string) HandlerOption

WithName configures the Logger to annotate each message with the logger name.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL