package slogx

import "git.sr.ht/~pingoo/stdx/log/slogx"

package slogx provides various helpers to wrok with the stdlib's "log/slog" package see https://pkg.go.dev/log/slog https://gist.github.com/wijayaerick/de3de10c47a79d5310968ba5ff101a19 https://github.com/golang/go/issues/56345 https://go.googlesource.com/proposal/+/master/design/56345-structured-logging.md https://github.com/golang/go/wiki/Resources-for-slog

Index

Functions

func Err

func Err(err error) slog.Attr

func Fatal

func Fatal(logger *slog.Logger, message string, args ...any)

TODO: use actual fatal level https://opentelemetry.io/docs/reference/specification/logs/data-model/#example-mappings

func FromCtx

func FromCtx(ctx context.Context) *slog.Logger

FromCtx returns the Logger associated with the ctx. If no logger is associated, a New() logger is returned with a addedfield "slogx.FromCtx": "error".

For example, to add a field to an existing logger in the context, use this notation:

ctx := r.Context()
logger := slogx.FromCtx(ctx)
logger = logger.With(...)

func Time

func Time(key string, t timex.Time) slog.Attr

func ToCtx

func ToCtx(ctx context.Context, logger *slog.Logger) context.Context

ToCtx returns a copy of ctx with logger associated.

Types

type DiscardHandler

type DiscardHandler struct {
}

func NewDiscardHandler

func NewDiscardHandler() *DiscardHandler

NewDiscardHandler returns a new DiscardHandler that do nothing and discards logs

func (*DiscardHandler) Enabled

func (handler *DiscardHandler) Enabled(_ context.Context, _ slog.Level) bool

func (*DiscardHandler) Handle

func (handler *DiscardHandler) Handle(_ context.Context, _ slog.Record) error

func (*DiscardHandler) WithAttrs

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

func (*DiscardHandler) WithGroup

func (handler *DiscardHandler) WithGroup(name string) slog.Handler

type MultiHandler

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

func NewMultiHandler

func NewMultiHandler(handlers ...slog.Handler) *MultiHandler

func (*MultiHandler) Enabled

func (mh *MultiHandler) Enabled(ctx context.Context, l slog.Level) bool

Implements slog.Handler

func (*MultiHandler) Handle

func (mh *MultiHandler) Handle(ctx context.Context, r slog.Record) error

Implements slog.Handler

func (*MultiHandler) WithAttrs

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

Implements slog.Handler

func (*MultiHandler) WithGroup

func (mh *MultiHandler) WithGroup(name string) slog.Handler

Implements slog.Handler

Source Files

attr.go ctx.go discard_handler.go doc.go fatal.go multi_handler.go

Version
v0.0.0-20240218134121-094174641f6e (latest)
Published
Feb 18, 2024
Platform
linux/amd64
Imports
4 packages
Last checked
4 months ago

Tools for package owners.