logging

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package logging builds the process logger and carries per-unit-of-work attributes through a context.Context.

Setup installs a JSON logger on stdout as the slog default, which also routes the standard library log package through it. With attaches attributes to a context; Handler appends them to every record logged through a *Context method with that context, so a request ID or loan ID set once at the entry point reaches every line below it. When the context carries an OpenTelemetry span, the record also gets its trace_id and span_id.

Index

Constants

View Source
const LevelEnv = "LOG_LEVEL"

LevelEnv names the environment variable Setup reads the log level from.

Variables

This section is empty.

Functions

func Attrs

func Attrs(ctx context.Context) []slog.Attr

Attrs returns the attributes attached to ctx by With.

func New

func New(w io.Writer, level slog.Leveler) *slog.Logger

New returns a JSON logger writing to w at level, wrapped in Handler.

func ParseLevel

func ParseLevel(s string) (slog.Level, error)

ParseLevel parses debug, info, warn or error, case-insensitively. An empty string is info.

func Setup

func Setup() *slog.Logger

Setup builds a stdout logger at the level named by LOG_LEVEL, installs it as the slog default and returns it. An unparseable level falls back to info and is reported through the new logger.

func With

func With(ctx context.Context, attrs ...slog.Attr) context.Context

With returns a child context carrying attrs in addition to any already attached to ctx.

func WithLoan

func WithLoan(ctx context.Context, loanID, reference string) context.Context

WithLoan attaches a loan's identity. Empty values are skipped, so callers that only know the id need not special-case the reference.

Types

type Handler

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

Handler wraps a slog.Handler and appends the attributes attached to the record's context with With, plus trace_id and span_id when the context carries a valid span.

func NewHandler

func NewHandler(inner slog.Handler) *Handler

NewHandler wraps inner.

func (*Handler) Enabled

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

Enabled reports whether inner handles records at level.

func (*Handler) Handle

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

Handle appends the context's attributes to r and passes it to inner.

func (*Handler) WithAttrs

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

WithAttrs returns a Handler whose inner handler has attrs.

func (*Handler) WithGroup

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

WithGroup returns a Handler whose inner handler has the group name.

Jump to

Keyboard shortcuts

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