logx

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package logx is the application logger: log/slog, with the request-scoped identity from ctxkit attached automatically.

There is one rule worth stating. Log the CAUSE, never the response. An *errs.Error carries a caller-safe Message and an internal Cause; the client gets the first, this package gets both. Logging only what you returned is how a production incident becomes a trace id with no explanation behind it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func C

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

C returns the context logger with the request-scoped identity attached: trace id, request id, account and user when present.

Use it at the point of logging — `logx.C(ctx).Warn(...)` — rather than building a decorated logger and passing it around.

func Err

func Err(ctx context.Context, msg string, err error, args ...any)

Err logs an error with everything the client did not get: the kind, the code, the internal cause and the structured meta.

It is the counterpart of the web layer's sanitised response — between the two, every failure is both safe on the wire and fully explained in the log.

func From

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

From returns the logger on ctx, falling back to slog.Default. It never returns nil.

func Into

func Into(ctx context.Context, l *slog.Logger) context.Context

Into returns a context carrying l, so a deep call can log with the request's own fields without threading a logger through every signature.

func New

func New(opts Options) *slog.Logger

New builds a *slog.Logger from opts.

func ParseLevel

func ParseLevel(s string) slog.Level

ParseLevel maps a config string ("debug", "info", "warn", "error") to a slog.Level, defaulting to Info for anything unrecognised.

Types

type Format

type Format string

Format selects the encoder.

const (
	// FormatJSON is the production encoder: one JSON object per line.
	FormatJSON Format = "json"
	// FormatText is the development encoder: human-readable key=value.
	FormatText Format = "text"
)

type Options

type Options struct {
	Level  slog.Level
	Format Format
	// Writer defaults to os.Stdout. Logs go to stdout, not stderr: stderr is
	// for the process dying, and mixing the two makes a crash hard to find.
	Writer io.Writer
	// Service is attached to every record as "service".
	Service string
	// AddSource includes file:line. Costs a little; worth it outside hot loops.
	AddSource bool
}

Options configures a logger.

Jump to

Keyboard shortcuts

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