golang-samber-slog

Compose structured logging pipelines in Go using samber slog handler libraries.

Updated Jun 15, 2026
One-click install
npx skills add https://github.com/2877389577/novels_ai_gen --skill golang-samber-slog-2877389577
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: golang-samber-slog
Source: https://github.com/2877389577/novels_ai_gen/tree/main/.agents/skills/golang-samber-slog
Command: npx skills add https://github.com/2877389577/novels_ai_gen --skill golang-samber-slog-2877389577

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Building production logging in Go with slog requires wiring together sampling, PII scrubbing, routing, HTTP middleware, and backend sinks — and mistakes like sampling after formatting, missing shutdown flushes, or silently dropped records are common. This Skill provides the canonical pipeline patterns and correct constructor usage for the 20+ samber/slog-* handler packages. ## Core Features & Use Cases - Pipeline Composition: Use slog-multi patterns (Fanout, Router, FirstMatch, Failover, Pool, Pipe) to route records to the right handlers with correct latency and error semantics. - Sampling & Formatting: Apply slog-sampling strategies (Uniform, Threshold, Absolute, Custom) as the outermost stage and slog-formatter middleware for PII masking and attribute transformation. - HTTP Middleware & Backend Sinks: Configure slog-gin/echo/fiber/chi middleware with per-status log levels, and connect sinks like Sentry, Loki, Datadog, Kafka, and Slack with proper graceful-shutdown flushing. - Use Case: Route ERROR logs to Sentry without sampling while sending sampled INFO logs to Loki, with PII scrubbed before any record leaves the process. ## Quick Start Set up a slog pipeline in Go using samber/slog libraries that samples INFO logs at 10%, scrubs PII, and routes errors to Sentry and everything else to Loki.

Frequently Asked Questions about golang-samber-slog

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I route Go slog logs to multiple handlers by level?▼

Use slogmulti.Router() with predicates like slogmulti.LevelIs(slog.LevelError) to send errors to Sentry while other levels go to Loki or stdout. Add a catch-all handler with no predicate, since records matching no predicate are silently dropped.

How do I add log sampling to a Go slog pipeline?▼

Use slogsampling options such as ThresholdSamplingOption or UniformSamplingOption as a Pipe middleware, placed as the outermost stage before formatting and routing. Matchers like MatchByLevelAndMessage() group records so each unique message gets its own counter.

What is the difference between slog-multi Fanout, Router, and Pool?▼

Fanout broadcasts every record to all handlers sequentially with latency equal to their sum. Router sends records to handlers whose predicates match, and Pool load-balances each record to one handler, giving latency of a single handler.

Does slog-gin support different log levels for 4xx and 5xx responses?▼

Yes, sloggin.NewWithConfig accepts a Config struct with DefaultLevel, ClientErrorLevel, and ServerErrorLevel fields. Set ClientErrorLevel to Warn and ServerErrorLevel to Error so 4xx and 5xx responses log at distinct levels without custom middleware.

Why are my buffered logs lost when my Go service shuts down?▼

Batch handlers like slog-datadog, slog-loki, slog-kafka, and slog-parquet buffer records internally and must be flushed on shutdown. Call handler.Stop(ctx) for Datadog, lokiClient.Stop() for Loki, writer.Close() for Kafka, or buffer.Flush(true) for Parquet.

Why is AttrFromContext returning empty attributes in my slog handlers?▼

AttrFromContext only works when an HTTP middleware such as slog-gin, slog-echo, slog-fiber, or slog-chi has populated the context with request attributes first. Without that middleware, the extraction functions silently return nil.