Skip to content

skillCode quality

Logging Best Practices

Adds structured log lines with levels, correlation IDs and no personal data where they help. Use it when a service is hard to debug from its logs.

npx workspacetemplates add logging-best-practices --tool claude-code

Writes 1 file to .claude/skills/logging-best-practices/

.claude/skills/logging-best-practices/SKILL.md25 lines · 1.5 KBFile
---
name: logging-best-practices
description: Adds structured log lines with levels, correlation IDs and no personal data where they help. Use it when a service is hard to debug from its logs.
---

# Logging best practices

Log decisions and failures, not every step. Every line must help someone answer "what happened to this request?"

## Steps

1. Find the logger already in use (`grep -rn "logger\|logging\|pino\|winston\|slog" src`). Reuse it. Do not add a second library.
2. Locate the points that matter: request entry and exit, calls to other services and databases, retries, fallbacks, state changes, and every catch block.
3. Make sure a correlation ID exists. Read it from an incoming header such as `x-request-id` or create one at the edge, then attach it to the logger context so every line carries it.
4. Write each entry as structured key-value data with a short event name (`payment.declined`), not an interpolated sentence. Put variable data in fields.
5. Pick levels deliberately: ERROR only when someone should act, WARN for handled surprises, INFO for business events, DEBUG for detail that is off in production.
6. Log an error once, where it is handled, with the exception and stack. Do not log and rethrow at every layer.

## Never log

Passwords, tokens, API keys, session cookies, full card or national ID numbers, email addresses, or whole request bodies. Log an internal user ID or a masked value instead.

## Report

List the files changed, the events added with their levels, and any place where you found personal data already being logged.

What it does

A log line is only useful if someone can find it, link it to one request and read it at three in the morning. This skill makes the agent place a small number of structured entries at the points that decide an outcome, and leaves the rest of the code quiet.

Every entry carries a level, a stable event name and a correlation ID, and never a password, token, email or full request body. The agent matches the logger your project already uses instead of adding a new one.

Example

you › Add logging to the order checkout flow so we can trace failed payments.

agent › Added 6 structured entries in src/checkout/service.ts: checkout.started, payment.authorized, payment.declined (WARN, with decline code), inventory.reserve_failed (ERROR), checkout.completed and a single catch-block entry. All carry requestId from the existing middleware. Found and removed an email address in the old "order created" line.

Questions

Will it install a logging library?
No. It looks for the logger your project already uses and follows its conventions. A new dependency is only suggested in the report, never added.
How does it decide what counts as personal data?
It treats credentials, contact details, payment data and free-text user input as off limits, and prefers opaque IDs. You can add project-specific field names to the request.
  • API Deprecation Handling

    SkillCode quality

    Free
  • Code Duplication Removal

    SkillCode quality

    Free
  • Code Smell Detection

    SkillCode quality

    Free
  • Concurrency Bug Analysis

    SkillCode quality

    Free