# Golang Samber Slog > Extensiones de logging estructurado para Go con paquetes samber/slog-****: pipelines multi-handler, sampling, formateo de atributos, middleware HTTP y enrutamiento a backends como Datadog, Sentry o Loki. Fuente: https://skillsagentes.com/skills/samber/cc-skills-golang/golang-samber-slog Markdown: https://skillsagentes.com/skills/samber/cc-skills-golang/golang-samber-slog.md Repositorio: https://github.com/samber/cc-skills-golang Autor: samber Licencia: MIT Actualizado: el mes pasado Coste de contexto: 115 tok instalada, 3.1k tok al activarse, 14k tok con todos los archivos del bundle Bundle: 6 archivos, 55 KB Permisos que pide: read edit write glob grep bash(go:*) bash(golangci-lint:*) bash(git:*) agent webfetch mcp__context7__resolve-library-id mcp__context7__query-docs askuserquestion bash(godig:*) bash(gopls:*) lsp mcp__gopls__* ## Instalación Un skill son archivos markdown: los mismos archivos valen para cualquier agente y lo único que cambia es el directorio de destino, es decir la bandera `--agent`. Añade `-g` para instalarlo en todos los proyectos de la máquina. ```bash # Claude Code npx -y skills add samber/cc-skills-golang --skill golang-samber-slog --agent claude-code # Cursor npx -y skills add samber/cc-skills-golang --skill golang-samber-slog --agent cursor # Codex npx -y skills add samber/cc-skills-golang --skill golang-samber-slog --agent codex # Gemini CLI npx -y skills add samber/cc-skills-golang --skill golang-samber-slog --agent gemini # Windsurf npx -y skills add samber/cc-skills-golang --skill golang-samber-slog --agent windsurf # Cline npx -y skills add samber/cc-skills-golang --skill golang-samber-slog --agent cline ``` ## Qué hace - Diseña pipelines de logging con slog.Handler compuestos: sampling, formateo de atributos y enrutamiento a sinks - Aplica el orden canónico sample → format → route para evitar desperdiciar CPU - Configura middlewares HTTP (slog-fiber, slog-gin, slog-chi, slog-echo) para atributos de request - Enruta logs a backends como Datadog, Sentry, Loki, Kafka, Slack con manejo de shutdown para handlers en batch - Detecta errores comunes de pipeline como Router sin catch-all o Fanout bloqueante ## Cuándo usarla - Al usar o adoptar slog en un proyecto Go - Cuando el código ya importa algún paquete github.com/samber/slog-* ## Qué la activa - "Ayúdame a montar un pipeline de logging con slog-multi y slog-sampling" - "Quiero enrutar los errores a Sentry y el resto a stdout con slog" - "Configura slog-gin para loguear las peticiones HTTP" - "Necesito que los logs se envíen a Datadog con flush al cerrar la app" ## Antes de instalar - Requiere Go 1.21+ y el binario go instalado; asume proyectos que usan o adoptarán los paquetes samber/slog-*. ## Archivos - SKILL.md — 12 KB - evals/evals.json — 16 KB - references/backend-handlers.md — 7 KB - references/http-middlewares.md — 5 KB - references/pipeline-patterns.md — 8 KB - references/sampling-strategies.md — 6 KB ## SKILL.md Reproducido tal cual desde samber/cc-skills-golang bajo MIT. Esta sección es el documento original y está en inglés. **Persona:** You are a Go logging architect. You design log pipelines where every record flows through the right handlers — sampling drops noise early, formatters strip PII before records leave the process, and routers send errors to Sentry while info goes to Loki. # samber/slog-\*\*\*\* — Structured Logging Pipeline for Go 20+ composable `slog.Handler` packages for Go 1.21+. Three core pipeline libraries plus HTTP middlewares and backend sinks that all implement the standard `slog.Handler` interface. **Official resources:** - [github.com/samber/slog-multi](https://github.com/samber/slog-multi) — handler composition - [github.com/samber/slog-sampling](https://github.com/samber/slog-sampling) — throughput control - [github.com/samber/slog-formatter](https://github.com/samber/slog-formatter) — attribute transformation This skill is not exhaustive. Please refer to library documentation and code examples for more information. For Go package docs, symbols, versions, importers, and known vulnerabilities, → See `samber/cc-skills-golang@golang-pkg-go-dev` skill (`godig`) — prefer it over Context7 for Go package facts. To navigate this library's usage in your own code (definitions, call sites, diagnostics), → See `samber/cc-skills-golang@golang-gopls` skill (`gopls`). Context7 remains a fallback for docs not indexed on pkg.go.dev. ## The Pipeline Model Every samber/slog pipeline follows a canonical ordering. Records flow left to right — place sampling first to drop early and avoid wasting CPU on records that never reach a sink. ``` record → [Sampling] → [Pipe: trace/PII] → [Router] → [Sinks] ``` Order matters: sampling before formatting saves CPU. Formatting before routing ensures all sinks receive clean attributes. Reversing this wastes work on records that get dropped. ## Core Libraries | Library | Purpose | Key constructors | | --- | --- | --- | | `slog-multi` | Handler composition | `Fanout`, `Router`, `FirstMatch`, `Failover`, `Pool`, `Pipe` | | `slog-sampling` | Throughput control | `UniformSamplingOption`, `ThresholdSamplingOption`, `AbsoluteSamplingOption`, `CustomSamplingOption` | | `slog-formatter` | Attribute transforms | `PIIFormatter`, `ErrorFormatter`, `FormatByType[T]`, `FormatByKey`, `FlattenFormatterMiddleware` | ## slog-multi — Handler Composition Six composition patterns, each for a different routing need: | Pattern | Behavior | Latency impact | | --- | --- | --- | | `Fanout(handlers...)` | Broadcast to all handlers sequentially | Sum of all handler latencies | | `Router().Add(h, predicate).Handler()` | Route to ALL matching handlers | Sum of matching handlers | | `Router().Add(...).FirstMatch().Handler()` | Route to FIRST match only | Single handler latency | | `Failover()(handlers...)` | Try sequentially until one succeeds | Primary handler latency (happy path) | | `Pool()(handlers...)` | Load-balance: sends each record to ONE handler | Single handler latency | | `Pipe(middlewares...).Handler(sink)` | Middleware chain before sink | Middleware overhead + sink | ```go // Route errors to Sentry, all logs to stdout logger := slog.New( slogmulti.Router(). Add(sentryHandler, slogmulti.LevelIs(slog.LevelError)). Add(slog.NewJSONHandler(os.Stdout, nil)). Handler(), ) ``` Built-in predicates: `LevelIs`, `LevelIsNot`, `MessageIs`, `MessageIsNot`, `MessageContains`, `MessageNotContains`, `AttrValueIs`, `AttrKindIs`. For full code examples of every pattern, see [Pipeline Patterns](references/pipeline-patterns.md). ## slog-sampling — Throughput Control | Strategy | Behavior | Best for | | --- | --- | --- | | Uniform | Drop fixed % of all records | Dev/staging noise reduction | | Threshold | Log first N per interval, then sample at rate R | Production — preserves initial visibility | | Absolute | Cap at N records per interval globally | Hard cost control | | Custom | User function returns sample rate per record | Level-aware or time-aware rules | Sampling MUST be the outermost handler in the pipeline — placing it after formatting wastes CPU on records that get dropped. ```go // Threshold: log first 10 per 5s, then 10% — errors always pass through via Router logger := slog.New( slogmulti. Pipe(slogsampling.ThresholdSamplingOption{ Tick: 5 * time.Second, Threshold: 10, Rate: 0.1, }.NewMiddleware()). Handler(innerHandler), ) ``` Matchers group similar records for deduplication: `MatchByLevel()`, `MatchByMessage()`, `MatchByLevelAndMessage()` (default), `MatchBySource()`, `MatchByAttribute(groups, key)`. For strategy comparison and configuration details, see [Sampling Strategies](references/sampling-strategies.md). ## slog-formatter — Attribute Transformation Apply as a `Pipe` middleware so all downstream handlers receive clean attributes. ```go logger := slog.New( slogmulti.Pipe(slogformatter.NewFormatterMiddleware( slogformatter.PIIFormatter("user"), // mask PII fields slogformatter.ErrorFormatter("error"), // structured error info slogformatter.IPAddressFormatter("client"), // mask IP addresses )).Handler(slog.NewJSONHandler(os.Stdout, nil)), ) ``` Key formatters: `PIIFormatter`, `ErrorFormatter`, `TimeFormatter`, `UnixTimestampFormatter`, `IPAddressFormatter`, `HTTPRequestFormatter`, `HTTPResponseFormatter`. Generic formatters: `FormatByType[T]`, `FormatByKey`, `FormatByKind`, `FormatByGroup`, `FormatByGroupKey`. Flatten nested attributes with `FlattenFormatterMiddleware`. ## HTTP Middlewares Consistent pattern across frameworks: `router.Use(slogXXX.New(logger))`. Available: `slog-gin`, `slog-echo`, `slog-fiber`, `slog-chi`, `slog-http` (net/http). All share a `Config` struct with: `DefaultLevel`, `ClientErrorLevel`, `ServerErrorLevel`, `WithRequestBody`, `WithResponseBody`, `WithUserAgent`, `WithRequestID`, `WithTraceID`, `WithSpanID`, `Filters`. ```go // Gin with filters — skip health checks router.Use(sloggin.NewWithConfig(logger, sloggin.Config{ DefaultLevel: slog.LevelInfo, ClientErrorLevel: slog.LevelWarn, ServerErrorLevel: slog.LevelError, WithRequestBody: true, Filters: []sloggin.Filter{ sloggin.IgnorePath("/health", "/metrics"), }, })) ``` For framework-specific setup, see [HTTP Middlewares](references/http-middlewares.md). ## Backend Sinks All follow the `Option{}.NewXxxHandler()` constructor pattern. | Category | Packages | | ------------ | ---------------------------------------------------------- | | Cloud | `slog-datadog`, `slog-sentry`, `slog-loki`, `slog-graylog` | | Messaging | `slog-kafka`, `slog-fluentd`, `slog-logstash`, `slog-nats` | | Notification | `slog-slack`, `slog-telegram`, `slog-webhook` | | Storage | `slog-parquet` | | Bridges | `slog-zap`, `slog-zerolog`, `slog-logrus` | **Batch handlers require graceful shutdown** — `slog-datadog`, `slog-loki`, `slog-kafka`, and `slog-parquet` buffer records internally. Flush on shutdown (e.g., `handler.Stop(ctx)` for Datadog, `lokiClient.Stop()` for Loki, `writer.Close()` for Kafka) or buffered logs are lost. For configuration examples and shutdown patterns, see [Backend Handlers](references/backend-handlers.md). ## Common Mistakes | Mistake | Why it fails | Fix | | --- | --- | --- | | Sampling after formatting | Wastes CPU formatting records that get dropped | Place sampling as outermost handler | | Fanout to many synchronous handlers | Blocks caller — latency is sum of all handlers | Use `Pool()` for concurrent dispatch | | Missing shutdown flush on batch handlers | Buffered logs lost on shutdown | `defer handler.Stop(ctx)` (Datadog), `defer lokiClient.Stop()` (Loki), `defer writer.Close()` (Kafka) | | Router without default/catch-all handler | Unmatched records silently dropped | Add a handler with no predicate as catch-all | | `AttrFromContext` without HTTP middleware | Context has no request attributes to extract | Install `slog-gin`/`echo`/`fiber`/`chi` middleware first | | Using `Pipe` with no middleware | No-op wrapper adding per-record overhead | Remove `Pipe()` if no middleware needed | ## Performance Warnings - **Fanout latency** = sum of all handler latencies (sequential). With 5 handlers at 10ms each, every log call costs 50ms. Use `Pool()` to reduce to max(latencies) - **Pipe middleware** adds per-record function call overhead — keep chains short (2-4 middlewares) - **slog-formatter** processes attributes sequentially — many formatters compound. For hot-path attribute formatting, prefer implementing `slog.LogValuer` on your types instead - **Benchmark** your pipeline with `go test -bench` before production deployment **Diagnose:** measure per-record allocation and latency of your pipeline and identify which handler in the chain allocates most. ## Best Practices 1. **Sample first, format second, route last** — this canonical ordering minimizes wasted work and ensures all sinks see clean data 2. **Use Pipe for cross-cutting concerns** — trace ID injection and PII scrubbing belong in middleware, not per-handler logic 3. **Test pipelines with `slogmulti.NewHandleInlineHandler`** — assert on records reaching each stage without real sinks 4. **Use `AttrFromContext`** to propagate request-scoped attributes from HTTP middleware to all handlers 5. **Prefer Router over Fanout** when handlers need different record subsets — Router evaluates predicates and skips non-matching handlers ## Cross-References - → See `samber/cc-skills-golang@golang-observability` skill for slog fundamentals (levels, context, handler setup, migration) - → See `samber/cc-skills-golang@golang-error-handling` skill for the log-or-return rule - → See `samber/cc-skills-golang@golang-security` skill for PII handling in logs - → See `samber/cc-skills-golang@golang-samber-oops` skill for structured error context with `samber/oops` If you encounter a bug or unexpected behavior in any samber/slog-\* package, open an issue at the relevant repository (e.g., [slog-multi/issues](https://github.com/samber/slog-multi/issues), [slog-sampling/issues](https://github.com/samber/slog-sampling/issues)). ## Dónde encaja - Categoría: [Herramientas para desarrolladores](https://skillsagentes.com/categorias/herramientas-desarrollo.md) — Skills que cambian cómo tu agente escribe, revisa y despliega código. - Creador: [samber](https://skillsagentes.com/creators/samber.md) — 0 skills en el directorio - [Todas las skills](https://skillsagentes.com/skills.md) - [Ranking de instalaciones](https://skillsagentes.com/ranking.md) ## Otras skills del mismo repositorio - [Golang Lint](https://skillsagentes.com/skills/samber/cc-skills-golang/golang-lint.md): Buenas prácticas de linting y configuración de golangci-lint para proyectos Golang: ejecutar linters, configurar .golangci.yml, suprimir avisos con nolint, interpretar salidas y elegir linters. - [Golang How To](https://skillsagentes.com/skills/samber/cc-skills-golang/golang-how-to.md): Orquestador de skills de Golang, siempre activo en cualquier tarea de código, revisión, debug o setup: carga las skills más relevantes de samber/cc-skills-golang, a menudo varias a la vez. - [Golang Benchmark](https://skillsagentes.com/skills/samber/cc-skills-golang/golang-benchmark.md): Benchmarking, profiling y medición de rendimiento en Golang: escribir y comparar benchmarks, perfilar con pprof, analizar con benchstat y detectar regresiones en CI. - [Golang Testing](https://skillsagentes.com/skills/samber/cc-skills-golang/golang-testing.md): Tests de Golang listos para producción: table-driven, suites y mocks con testify, tests paralelos, fuzzing, fixtures, detección de fugas de goroutines con goleak, snapshot testing, cobertura, tests de integración. - [Golang Performance](https://skillsagentes.com/skills/samber/cc-skills-golang/golang-performance.md): Patrones y metodología de optimización de rendimiento en Golang: si hay cuello de botella X, aplica el patrón Y, una vez que profiling o benchmarks ya lo identificaron. ## Skills relacionadas - [Golang Samber Hot](https://skillsagentes.com/skills/samber/cc-skills-golang/golang-samber-hot.md): Caché en memoria en Golang con samber/hot: algoritmos de expulsión (LRU, LFU, TinyLFU, S3FIFO, ARC, TwoQueue, SIEVE, FIFO), TTL, loaders, sharding y métricas Prometheus. - [Golang Samber Lo](https://skillsagentes.com/skills/samber/cc-skills-golang/golang-samber-lo.md): Helpers funcionales para Go con samber/lo: 500+ funciones genéricas type-safe para slices, maps, canales, strings, math, tuplas y concurrencia. - [Golang Samber Mo](https://skillsagentes.com/skills/samber/cc-skills-golang/golang-samber-mo.md): Tipos monádicos para Golang con samber/mo — Option, Result, Either, Future, IO, Task y State para valores nulos seguros, manejo de errores y composición funcional. - [Golang Spf13 Viper](https://skillsagentes.com/skills/samber/cc-skills-golang/golang-spf13-viper.md): Librería de configuración en Go con spf13/viper: precedencia en capas (flag > env > archivo > KV > default), BindPFlag, AutomaticEnv, Unmarshal con mapstructure, Sub, WatchConfig y aislamiento en tests. - [Golang Structs Interfaces](https://skillsagentes.com/skills/samber/cc-skills-golang/golang-structs-interfaces.md): Patrones de diseño de structs e interfaces en Golang: composición, embedding, aserciones de tipo, interfaces pequeñas, DI vía interfaces, tags de campo y receptores puntero vs valor. --- Skills Agentes · [Índice de páginas en markdown](https://skillsagentes.com/sitemap.md) · [Inicio](https://skillsagentes.com/index.md)