Zerolog observability

From makifbaysal/tasktrooper · View interactive version

Zerolog observability is an AI agent skill from the GitHub repository makifbaysal/tasktrooper. Use when adding logging to Go code - the repository's logger with structured fields, request-scoped correlation, correct levels, no secrets or double-logging

How to use this skill

Skill file

Structured Logging (Go)

Overview

Logs are for machines first: structured fields you can filter and correlate, not prose. The recurring defects are fmt.Println debugging left in, data interpolated into the message instead of fields, the same error logged at every layer, and secrets leaking into logs.

Core principle: log the failure at its site, with fields, once — and use the repository's own logger.

Which logger

Follow the repository's existing logger: github.com/rs/zerolog, log/slog (stdlib), or go.uber.org/zap all give structured fields and are fine to continue. Use zerolog only when the repo has no logger yet. Never introduce a second logging library alongside one the repo already uses (rule repo-conventions-win). Never fmt.Println / log.Printf in production code regardless of which structured logger the repo picked.

Rules

```go

// middleware, once per request

l := log.With().Str("request_id", reqID).Logger()

c.SetUserContext(l.WithContext(c.UserContext()))

// anywhere downstream

zerolog.Ctx(ctx).Error().Err(err).Str("task_id", id.String()).Msg("dispatch failed")

```

(slog: slog.Default().With("request_id", reqID) carried the same way via context, or a custom Handler.)

Levels

| Level | Use for |

|-------|---------|

| Debug | development detail, verbose tracing |

| Info | state changes worth auditing (task moved, migration applied) |

| Warn | handled anomaly (retry, fallback taken) |

| Error | a failure needing attention |

Worked Example

// ❌ prose message, interpolated id, will be logged again by the caller too
log.Error().Msg(fmt.Sprintf("dispatch failed for task %s: %v", id, err))

// ✅ structured fields, error attached, logged once at the handling site, request-scoped logger
zerolog.Ctx(ctx).Error().
    Err(err).
    Str("task_id", id.String()).
    Str("op", "dispatch").
    Msg("dispatch failed")
// upstream callers propagate with fmt.Errorf("dispatch: %w", err) — they do NOT re-log

Now a query like op="dispatch" level="error" finds every dispatch failure with its task id and request id, and each failure appears exactly once.

Java

Follow the repo's existing setup: SLF4J (with JBoss Logging or Logback underneath) using MDC for request_id and other correlation fields; Spring Boot's built-in structured logging (logging.structured.format.console=ecs, 3.4+); or Quarkus's quarkus-logging-json extension. The same rules apply — fields not string concatenation, handle once, never log secrets.

Common Mistakes

Red Flags

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.