slogx

package
v0.0.0-...-0941746 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2024 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

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

Constants

This section is empty.

Variables

This section is empty.

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

Jump to

Keyboard shortcuts

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