logging-best-practices · boristane — Claude Code skill: how to install and use
This skill guides your agent to implement wide events (canonical log lines) instead of scattered logs. It consolidates request metrics, business context, and errors into one JSON event.
npx skills add boristane/agent-skills --skill logging-best-practices -a claude-codeWhat logging-best-practices does
This skill instructs the agent to implement wide events, also known as canonical log lines. Instead of scattering console.log calls across handlers, your service builds a single structured JSON event. It emits that event inside a finally block when the request finishes. The event captures high-cardinality IDs such as userId and requestId, deployment details like commit hash or region, and business context — for instance, cart totals or user subscription tiers.
The skill contains four rule documents: rules/wide-events.md, rules/context.md, rules/structure.md, and rules/pitfalls.md. They enforce a single logger instance across the codebase, mandate middleware for timing and status capture, and restrict log levels to just info and error. Unstructured text logs are forbidden. Cutting log levels down to two means rethinking traditional debugging habits, but it stops messy log pollution early.
What it helps with
- replace scattered
console.logcalls with a single JSON event per request - attach business context and deployment metadata to every log record
- implement middleware to capture request duration and outcomes automatically
- standardize the logging schema and restrict levels to
infoanderror - eliminate unstructured text strings from application logs
Example prompts for the agent
- Refactor the checkout route logging using the wide events pattern from logging-best-practices
- Review this middleware to emit a canonical log line with requestId, userId, and execution time
- Set up a single application logger and restrict log levels to info and error
How to install logging-best-practices
1Skills CLI (Vercel)
npx skills add boristane/agent-skills --skill logging-best-practices -a claude-code2Manually: put the files in place
~/.claude/skills/logging-best-practices/SKILL.mdCopy the whole skill folder; for one project use .claude/skills/logging-best-practices/
1Skills CLI (Vercel)
npx skills add boristane/agent-skills --skill logging-best-practices -a codex2Manually: put the files in place
.agents/skills/logging-best-practices/SKILL.mdFor all projects use ~/.agents/skills/
1Skills CLI (Vercel)
npx skills add boristane/agent-skills --skill logging-best-practices -a cursor2Manually: put the files in place
.cursor/skills/logging-best-practices/SKILL.mdCursor also reads .agents/skills/ and .claude/skills/
1GitHub CLI
gh skill install boristane/agent-skills logging-best-practices2Manually: put the files in place
.github/skills/logging-best-practices/SKILL.md1Skills CLI (Vercel)
npx skills add boristane/agent-skills --skill logging-best-practices -a gemini-cli2Platform CLI
gemini skills install https://github.com/boristane/agent-skills --path skills/logging-best-practicesManually: put the files in place
.devin/skills/logging-best-practices/SKILL.mdThe legacy .windsurf/skills/ path still works
Skills CLI (Vercel)
npx skills add boristane/agent-skills --skill logging-best-practices -a opencodeClaude.ai
Zip the folder and upload it in Settings → Capabilities → Skills
Step by step, with commands for each platform: How to install skills in Claude Code: five ways
Questions about logging-best-practices
How to install logging-best-practices in Claude Code?
Run the command npx skills add boristane/agent-skills --skill logging-best-practices -a claude-code.
What are wide events in logging?
Wide events, or canonical log lines, consolidate all data for a single request into one context-rich JSON entry emitted upon completion. Instead of writing separate log lines, the service combines technical metrics, user IDs, and business context into a single record.
Which log levels does this skill recommend?
The skill restricts logging to just two levels: info for standard completions and error for failures. Intermediate levels like debug or warn are omitted, and unstructured string messages are strictly forbidden.
Source text
SKILL.md · MIT
---
name: logging-best-practices
description: Logging best practices focused on wide events (canonical log lines) for powerful debugging and analytics
license: MIT
metadata:
author: boristane
version: "1.0.0"
---
# Logging Best Practices Skill
Version: 1.0.0
## Purpose
This skill provides guidelines for implementing effective logging in applications. It focuses on **wide events** (also called canonical log lines) - a pattern where you emit a single, context-rich event per request per service, enabling powerful debugging and analytics.
## When to Apply
Apply these guidelines when:
- Writing or reviewing logging code
- Adding console.log, logger.info, or similar
- Designing logging strategy for new services
- Setting up logging infrastructure
## Core Principles
### 1. Wide Events (CRITICAL)
Emit **one context-rich event per request per service**. Instead of scattering log lines throughout your handler, consolidate everything into a single structured event emitted at request completion.
```typescript
const wideEvent: Record<string, unknown> = {
method: 'POST',
path: '/checkout',
requestId: c.get('requestId'),
timestamp: new Date().toISOString(),
};
try {
const user = await getUser(c.get('userId'));
wideEvent.user = { id: user.id, subscription: user.subscription };
const cart = await getCart(user.id);
wideEvent.cart = { total_cents: cart.total, item_count: cart.items.length };
wideEvent.status_code = 200;
wideEvent.outcome = 'success';
return c.json({ success: true });
} catch (error) {
wideEvent.status_code = 500;
wideEvent.outcome = 'error';
wideEvent.error = { message: error.message, type: error.name };
throw error;
} finally {
wideEvent.duration_ms = Date.now() - startTime;
logger.info(wideEvent);
}
```
### 2. High Cardinality & Dimensionality (CRITICAL)
Include fields with high cardinality (user IDs, request IDs - millions of unique values) and high dimensionality (many fields per event). This enables querying by specific users and answering questions you haven't anticipated yet.
### 3. Business Context (CRITICAL)
Always include business context: user subscription tier, cart value, feature flags, account age. The goal is to know "a premium customer couldn't complete a $2,499 purchase" not just "checkout failed."
### 4. Environment Characteristics (CRITICAL)
Include environment and deployment info in every event: commit hash, service version, region, instance ID. This enables correlating issues with deployments and identifying region-specific problems.
### 5. Single Logger (HIGH)
Use one logger instance configured at startup and import it everywhere. This ensures consistent formatting and automatic environment context.
### 6. Middleware Pattern (HIGH)
Use middleware to handle wide event infrastructure (timing, status, environment, emission). Handlers should only add business context.
### 7. Structure & Consistency (HIGH)
- Use JSON format consistently
- Maintain consistent field names across services
- Simplify to two log levels: `info` and `error`
- Never log unstructured strings
## Anti-Patterns to Avoid
1. **Scattered logs**: Multiple console.log() calls per request
2. **Multiple loggers**: Different logger instances in different files
3. **Missing environment context**: No commit hash or deployment info
4. **Missing business context**: Logging technical details without user/business data
5. **Unstructured strings**: `console.log('something happened')` instead of structured data
6. **Inconsistent schemas**: Different field names across services
## Guidelines
### Wide Events (`rules/wide-events.md`)
- Emit one wide event per service hop
- Include all relevant context
- Connect events with request ID
- Emit at request completion in finally block
### Context (`rules/context.md`)
- Support high cardinality fields (user_id, request_id)
- Include high dimensionality (many fields)
- Always include business context
- Always include environment characteristics (commit_hash, version, region)
### Structure (`rules/structure.md`)
- Use a single logger throughout the codebase
- Use middleware for consistent wide events
- Use JSON format
- Maintain consistent schema
- Simplify to info and error levels
- Never log unstructured strings
### Common Pitfalls (`rules/pitfalls.md`)
- Avoid multiple log lines per request
- Design for unknown unknowns
- Always propagate request IDs across services
References:
- [Logging Sucks](https://loggingsucks.com)
- [Observability Wide Events 101](https://boristane.com/blog/observability-wide-events-101/)
- [Stripe - Canonical Log Lines](https://stripe.com/blog/canonical-log-lines)
© boristane · MIT · boristane/agent-skills
Similar skills
All skills for Backend and APIs →logging-best-practices by other authors comes first.