Awesome Go

slog-configurator

CategoryLogging
SubcategoryLogging
Stars0

Configures the standard library log/slog logger from environment variables: level, format, source location, and stdout/stderr split

About slog-configurator

slogging

Go Reference CI coverage version license imported by

Everything for Go's stdlib log/slog in one place: configure it from the environment, and stack handlers onto it that ship logs to Loki or keep them searchable in memory.

Spiritual successor to logrus-configurator, and the continuation of slog-configurator. Coming from there? Read Migrating from slog-configurator.

Contents

Documentation

The full documentation lives at psyb0t.github.io/slogging.

PageCovers
Configuring slogLOG_LEVEL, LOG_FORMAT, LOG_ADD_SOURCE, renaming them, and AddSink vs SetOutput vs SetHandlers
Output and fan-out handlersThe stdout and stderr split, several writers per stream, a level you can change at runtime, teeing to many handlers
Searchable ringThe in-memory ring: limits, attribute search, paging, stats
Loki handlerSettings, labels, line format, and what happens when Loki is down
TroubleshootingThe startup panic, doubled lines, sinks that see nothing, missing Loki records
Migrating from slog-configuratorOld import paths and the pre-1.7 handler API
DevelopmentMake targets, tests, CI and releases

Quick start

go get github.com/psyb0t/slogging
package main

import (
	"log/slog"

	_ "github.com/psyb0t/slogging/slogconf" // configures slog at init time
)

func main() {
	slog.Info("this is an info message", "user", "psyb0t", "action", "testing")
	slog.Error("this is an error message", "error_code", "E001")
}
export LOG_LEVEL="debug"      # debug / info / warn / error
export LOG_FORMAT="json"      # json / text
export LOG_ADD_SOURCE="true"  # include source file/line/function
go run main.go

stdout/stderr are split automatically. Info and debug go to stdout, warnings and errors to stderr. Container log collectors capture both and tag them, so error noise stays separate from happy-path noise.

Need more than the blank import? Rename the variables with slogconf.Init, add a destination with slogconf.AddSink, or swap where you print with slogconf.SetOutput. Those are different calls on purpose, and Configuring slog explains why.

Layout

slogconf/              configure slog from the environment
handlers/              Handler (the process's output) + FanOutHandler (tees to many)
handlers/logring/      bounded in-memory ring you can search
handlers/loki/         push records to Loki's HTTP API

Two kinds of thing live here, and the split is the whole design. handlers holds the structural pieces: the one that writes your output, and the one that tees a record to many. Its subpackages are destinations: a searchable ring, a Loki server. You configure one output and add as many destinations as you like.

Runtime dependencies are ctxerrors and nothing else. No config loader, no HTTP framework. Every handler talks to log/slog and the standard library.

Development

make test           # all tests with -race
make test-coverage  # coverage gate (fails under 90%)
make lint           # go fix + golangci-lint
make lint-fix       # same, with --fix
make docs           # build the docs site locally with Docker

See make help for the full list and Development for CI and releases.

Sponsor

License

MIT. See LICENSE.

See CHANGELOG.md for release notes.

Frequently Asked Questions

What is slog-configurator?

slog-configurator is a Logging library for the Go programming language. Configures the standard library log/slog logger from environment variables: level, format, source location, and stdout/stderr split

How do I install slog-configurator?

Install slog-configurator with the Go module system using `go get psyb0t/slog-configurator`. Check the repository for the current installation instructions.

What category does slog-configurator belong to?

slog-configurator is listed under Logging, specifically Logging.

← Back to Logging