logx

package
v0.7.17-dev.2 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package logx is the mechanism behind the log contract (docs/logging.md): the request id carried in a context, the handler that stamps it on every line written under that context, and the sanitiser that keeps tokens and secrets out of a line and keeps a line below the size at which Docker splits it.

The vocabulary — the `event` names a line may carry — lives in internal/logevent; this package knows nothing about what is logged, only how.

Index

Constants

View Source
const MaxStackLen = 8192

MaxStackLen is the bound for a `stack` attribute: a panic's stack is the one value worth most of the line, and 8 KB beside a 4 KB panic value and the line's other fields still stays under Docker's split.

View Source
const MaxStringLen = 4096

MaxStringLen bounds any string attribute. Docker splits a log line above 16 KB into 16,384-byte pieces that are no longer JSON and that nothing downstream can reassemble (measured); a line whose largest value is 4 KB stays well under that with room for the rest of its fields.

View Source
const Redacted = "[redacted]"

Redacted replaces a value under a secret-named key.

View Source
const RequestIDKey = "request_id"

RequestIDKey is the attribute every line written under a request carries.

Variables

This section is empty.

Functions

func MaskEmail

func MaskEmail(addr string) string

MaskEmail keeps the first character of the local part and the domain: enough to tell which mailbox a line is about, not enough to be an address of a person (docs/logging.md: none in a line the contract adds).

func MaskTokens

func MaskTokens(s string) string

MaskTokens replaces every token-shaped substring with its display prefix and a marker. The server uses it to refuse an inbound value that a log line would carry masked; tests use it for things that must never log one.

func New

func New(o Options) *slog.Logger

New builds the contract's logger: JSON, one line per record, every attribute sanitised, the request id from the context on every line, the service identity on every line.

func ParseLevel

func ParseLevel(level string) slog.Level

ParseLevel maps the config's level names onto slog's; anything unknown is info, which is what the config's default means.

func RequestIDFrom

func RequestIDFrom(ctx context.Context) string

RequestIDFrom returns the request id carried by ctx, or "".

func Sanitize

func Sanitize(_ []string, a slog.Attr) slog.Attr

Sanitize is the ReplaceAttr of the contract's handler: a value under a secret-named key is replaced, every string is masked of token-shaped substrings and cut at MaxStringLen, and both rules recurse into groups, maps, slices and errors. Masked, never dropped: a line about an incident is worth more than the value it would have leaked.

func WithRequestID

func WithRequestID(ctx context.Context, id string) context.Context

WithRequestID returns ctx carrying the request id.

Types

type Handler

type Handler struct{ slog.Handler }

Handler stamps the context's request id on every record it handles. It wraps any slog.Handler; the wrapped handler's ReplaceAttr still sees the stamped attribute, so a request id is sanitised like everything else.

func (Handler) Handle

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

Handle implements slog.Handler.

func (Handler) WithAttrs

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

WithAttrs implements slog.Handler.

func (Handler) WithGroup

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

WithGroup implements slog.Handler.

type Options

type Options struct {
	// Level is the minimum level: debug, info (the default), warn, error.
	Level string
	// Service, Role and Version are stamped on every line:
	// the binary's name, the role it runs, the build.
	Service, Role, Version string
	// W is where lines go; nil means os.Stderr.
	W io.Writer
}

Options configure New.

Jump to

Keyboard shortcuts

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