logger

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package logger writes a log line and fails the span the work runs in when that line reports an error.

It is for the steps a host runs on behalf of a script and does not want in the script's output. Migrations are the case it was written for: a migration set runs at startup, where the script's output is an HTTP body or a rendered page, so a per-file status line written there lands in what the script is producing.

log := logger.New(ctx, "migrate")
for _, item := range applied {
	log.Info("migration", "file", item.Filename, "status", item.Status)
}

The method set is Info and Error, spelled and behaving the way slog spells them, which is also the shape a library asking to be handed a logger asks for. Output goes to slog.Default, read per call so a process that configures logging after a logger was built still gets it, or to the logger given to WithLogger.

The trace is not a second log. Nothing is recorded on it per message, and no spans are opened for log output; Error alone reaches it, as the error of the span in the logger's context, which fails that span and the trace with it. A migration that failed is then red on the front end rather than a line someone has to read. The span is not ended, because the caller that opened it is the one that ends it.

Recording is nil safe the way the rest of the instrumentation is: without a trace in the context the span is nil and its methods do nothing, so a CLI run pays nothing beyond the log line it wanted anyway.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

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

Logger is a slog logger that fails the span it runs in when it is told about an error. Log output is the log's business and stays there; what reaches the trace is the one thing a trace has a place for, which is that the work went wrong.

The context it holds is the one the work runs under: a logger is handed to a library for the length of one call, so it lives as long as the context it was built with.

func New

func New(ctx context.Context, name string) *Logger

New returns a logger writing to the default slog logger, failing the span in ctx on Error.

The name prefixes the message, which is what tells two libraries logging the same word apart. An empty name logs the message alone.

func (*Logger) Error

func (l *Logger) Error(msg string, args ...any)

Error logs a message at error level and records it on the span the logger was built in, which fails that span and the trace with it. The error is the first one among the values when the caller passed one, so the span keeps what went wrong and not only what it was called. The span is not ended: it belongs to whoever opened it, and the work it measures is still theirs to finish.

func (*Logger) Info

func (l *Logger) Info(msg string, args ...any)

Info logs a message at info level.

func (*Logger) WithLogger

func (l *Logger) WithLogger(log *slog.Logger) *Logger

WithLogger returns a copy of the logger writing to log rather than to the default slog logger. A nil log restores the default.

Jump to

Keyboard shortcuts

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