Categorygithub.com/monzo/slog
modulepackage
0.0.0-20250128160826-b4d59675d8b8
Repository: https://github.com/monzo/slog.git
Documentation: pkg.go.dev

# README

slog

Structured logging.

slog is a library for capturing structured log information. In contrast to "traditional" logging libraries, slog:

  • captures a Context for each event
  • captures arbitrary key-value metadata on each log event

slog forwards messages to log by default. But you probably want to write a custom output to make use of the context and metadata. At Monzo, slog captures events both on a per-service and a per-request basis (using the context information) and sends them to a centralised logging system. This lets us view all the logs for a given request across all the micro-services it touches.

Usage

Internally at Monzo, we recommend that users always prefer structured logging where possible. An example of using slog for this would be:

slog.Info(ctx, "Loading widget", map[string]interface{}{
    "stage": "reticulating splines",
})

Errors

slog also provides a shorthand for capturing errors in the form of:

slog.Error(ctx, "Failed to load widget", err)

You may also add metadata to errors captured this way:

slog.Error(ctx, "Failed to load widget", err, map[string]interface{}{
    "user_id": 42,
})

Slog will pick up the first error it finds in the metadata and make it available in event.Error.

Other uses

For backwards-compatibility, slog accepts metadata in the form of map[string]string.

It also accepts format parameters in the style of Printf:

stage := "reticulating splines"
slog.Info(ctx, "Loading widget at stage: %s", stage)

# Functions

Critical constructs a logging event with critical severity.
Debug constructs a logging event with debug severity.
No description provided by the author
Error constructs a logging event with error severity.
Eventf constructs an event from the given message string and formatting operands.
FromError constructs a logging event with error severity by default.
Info constructs a logging event with info severity.
Log sends the given Events via the default Logger.
NewInMemoryLogger creates a logger that will keep all log events in memory Call InMemoryLogger.Events to access all logged events.
NewSeverityLogger creates a SeverityLogger which wraps the default logger.
Params returns all parameters stored in the given context using WithParams.
No description provided by the author
Trace constructs a logging event with trace severity.
Warn constructs a logging event with warn severity.
WithParam is shorthand for calling WithParams with a single key-value pair.
WithParams returns a copy of the parent context containing the given log parameters.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

An Event is a discrete logging event.
No description provided by the author
No description provided by the author
SeverityLogger is a logger which can log at different severity levels.
StdlibLogger is a very simple logger which forwards events to Go's standard library logger.

# Interfaces

FromErrorLogger is a logger which logs errors.
LeveledLogger is a logger which logs at different levels.
A Logger is a way of outputting events.

# Type aliases

EventSet is a time-sortable collection of logging events.
A MultiLogger sends invocations to multiple Loggers.
No description provided by the author