logging

package
v0.62.2 Latest Latest
Warning

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

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

Documentation

Overview

Package logging builds the structured logger every command writes with: log/slog lines in JSON, personal data masked, the request identifier of the context added (ADR 0002).

A value with both a Redacted and a String method, such as domain.Email, domain.Phone or domain.IBAN, is never written as such: the logger writes an object holding its Redacted form and a keyed pseudonym of its String form, {"redacted":"***@exemple.fr","hmac":"…"}. The pseudonym is the first 16 bytes of HMAC-SHA256 under a secret key, in hex: the same value gets the same pseudonym, so lines about it can be grouped, but it cannot be recovered nor guessed by hashing candidates without the key.

Masking relies on the value reaching the logger whole: logging email.String() writes the address. Log the value object itself.

Index

Constants

View Source
const MinKeyBytes = 32

MinKeyBytes is the shortest HMAC key New accepts: 32 bytes, the output size of SHA-256, which RFC 2104 (section 3) gives as the minimum recommended key length.

View Source
const RequestIDKey = "request_id"

RequestIDKey is the key of the request identifier in a log line.

Variables

View Source
var ErrInvalidConfig = errors.New("invalid logging config")

ErrInvalidConfig means New was given a Config it cannot use.

Functions

func New

func New(w io.Writer, cfg Config) (*slog.Logger, error)

New returns a logger writing one JSON object per line to w, with personal data masked and the request identifier of the context added.

func RequestID

func RequestID(ctx context.Context) (string, bool)

RequestID returns the request identifier ctx carries. It reports false when there is none or it is empty.

func WithRequestID

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

WithRequestID returns a copy of ctx carrying the request identifier id, which every line logged with that context then holds.

Types

type Config

type Config struct {
	// Level is the minimum level written; nil means slog.LevelInfo.
	Level slog.Leveler

	// Key is the secret HMAC key that pseudonymises personal data, at
	// least MinKeyBytes long. Required. New copies it.
	Key []byte
}

Config configures a logger.

Directories

Path Synopsis
Package loggingtest builds redacting loggers for tests, writing to a buffer the test reads back.
Package loggingtest builds redacting loggers for tests, writing to a buffer the test reads back.

Jump to

Keyboard shortcuts

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