logkit

package
v0.19.0 Latest Latest
Warning

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

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

Documentation

Overview

Package logkit is what a log record names: its event, defined once, and the trace it was written in.

A record's msg is a sentence for people; its event is the name programs match on (alert rules, dashboards, searches), a soft contract kept compatible as best anfra can (docs/designs/logs.md). An event is defined at package level with DefineEvent, never invented at a call site, so Events lists every event a binary can write:

var eventFailed = logkit.DefineEvent("routine.failed", "A routine's run returned an error.",
	"routine", "outcome", "error")

logger.ErrorContext(ctx, "Routine failed", logkit.EventKey, eventFailed, "routine", name, ...)

TraceHandler adds the trace and span a record was written in, so one request's records join across services.

Index

Constants

View Source
const EventKey = "event"

EventKey is the attribute that carries a record's event.

Variables

This section is empty.

Functions

func TraceHandler

func TraceHandler(h slog.Handler) slog.Handler

TraceHandler wraps h to add the trace and span a record was written in, from the context it was logged with, as trace_id and span_id: OpenTelemetry's names for trace context in JSON logs. They join one request's records across services, and link each to its span. A record logged without a context, or outside any span, has neither.

They go where the record's own attributes go: under a logger's group, if it has one, so a logger that writes records with an event has none.

Types

type Event

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

Event is something that happened, as programs reading the log know it. As an attribute's value, it is its name.

func DefineEvent

func DefineEvent(name, doc string, attrs ...string) Event

DefineEvent defines an event: its name, what it means, and the attributes its records may carry. Call it from a package-level var, once per name; a name already defined or not <area>.<what happened> in snake_case, no doc, or an attribute not snake_case or listed twice panics at startup.

func Events

func Events() []Event

Events is every event defined in this binary, in definition order.

func (Event) Attrs

func (e Event) Attrs() []string

Attrs are the attributes its records may carry, besides those every record has.

func (Event) Doc

func (e Event) Doc() string

Doc says what the event means.

func (Event) LogValue

func (e Event) LogValue() slog.Value

LogValue makes the event's name its value in a record.

func (Event) Name

func (e Event) Name() string

Name is the event's name, as records carry it.

func (Event) String

func (e Event) String() string

String is the event's name.

Jump to

Keyboard shortcuts

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