zapslog

package module
v0.0.0-...-1e9ce05 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2023 License: MIT Imports: 8 Imported by: 0

README

zapslog

ZapSlog provides a slog.Handler implementation backed by Uber's zap.Logger.

Notice: slog is still experimental and not officially part of the standard library. There may be changes to slog before it becomes finalized and part of the standard library. You may want to keep that in mind before using slog or this library.

Why slog?

slog provides structured logging functionality from the Go team. Although experimental at this time, ideally it will become the defacto logging interface used by frameworks and libraries. Presently the logging ecosystem in Go is fragmented although there are a few excellent third party libraries such as Zap and Zerolog. Third party loggers can be used as Handlers for slog making it very versatile for library developers.

Why zapslog?

zapslog uses zap.Logger as the handler for slog to get the performance Zap is known for while using the slog interfaces.

Example

package main

import (
	"context"
	"os"

	"go.uber.org/zap"
	"golang.org/x/exp/slog"

	"github.com/jkratz55/zapslog"
)

func main() {

	zLogger, _ := zap.NewProduction()
	zSlogger := slog.New(zapslog.NewHandler(zLogger))
	zSlogger = zSlogger.With(slog.String("appId", "FG2212"))
	zSlogger.Info("Something something dark side")
	zSlogger.Log(context.Background(),
		zapslog.LevelError,
		"Testing some stuffs",
		slog.Int("count", 11),
		slog.String("service", "test"),
		slog.String("err", "eeeeeee"),
		slog.Any("data", map[string]any{
			"units": "cm",
		}),
		slog.Group("http",
			slog.Int("code", 123),
			slog.String("name", "cow")))
}

Documentation

Index

Constants

View Source
const (
	LevelDebug  = slog.LevelDebug
	LevelInfo   = slog.LevelInfo
	LevelWarn   = slog.LevelWarn
	LevelError  = slog.LevelError
	LevelDPanic = slog.Level(9)
	LevelPanic  = slog.Level(10)
	LevelFatal  = slog.Level(11)
)

Variables

This section is empty.

Functions

func LevelName

func LevelName(leveler slog.Leveler) string

Types

type Handler

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

Handler is an implementation of slog.Handler interface backed by Zap's Logger.

The zero-value of Handler is not usable. Handler should be created/initialized using the NewHandler function.

func NewHandler

func NewHandler(logger *zap.Logger) *Handler

NewHandler creates and initializes a new Handler.

This function will panic if passed a nil zap.Logger.

func (*Handler) Enabled

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

func (*Handler) Handle

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

func (*Handler) WithAttrs

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

func (*Handler) WithGroup

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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