forge-it

logging-best-practices

0
0
# Install this skill:
npx skills add forge-it/agent-skills

Or install specific skill: npx add-skill https://github.com/forge-it/agent-skills/tree/main/software-engineering/general-logging

# Description

Logging best practices focused on wide events for powerful debugging and analytics

# SKILL.md


name: logging-best-practices
description: Logging best practices focused on wide events for powerful debugging and analytics
license: MIT
metadata:
author: [email protected]
version: "0.0.1"


Logging Best Practices Skill

Version: 0.0.1

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 updating logging code
- Adding logger.info, print, println!, info!, console.log, developer.log or similar depending on the programming language
- 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.

import time
from datetime import datetime, timezone

wide_event = {
    "method": "POST",
    "path": "/checkout",
    "request_id": request.state.request_id,
    "timestamp": datetime.now(timezone.utc).isoformat(),
}

try:
    user = get_user(request.state.user_id)
    wide_event["user"] = {"id": user.id, "subscription": user.subscription}

    cart = get_cart(user.id)
    wide_event["cart"] = {"total_cents": cart.total, "item_count": len(cart.items)}

    wide_event["status_code"] = 200
    wide_event["outcome"] = "success"
    return {"success": True}
except Exception as e:
    wide_event["status_code"] = 500
    wide_event["outcome"] = "error"
    wide_event["error"] = {"message": str(e), "type": type(e).__name__}
    raise
finally:
    wide_event["duration_ms"] = (time.time() - start_time) * 1000
    logger.info(wide_event)

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 logger.info 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: print('something wrong happened') instead of structured data
  6. Inconsistent schemas: Different field names across services

Guidelines

Wide Events

  • Emit one wide event per service hop
  • Include all relevant context
  • Connect events with request ID
  • Emit at request completion in finally block

Context

  • 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

  • Use a single logger throughout the codebase
  • Use middleware for consistent wide events
  • Use JSON format for formatting
  • Use key-value for rendering
  • Maintain consistent schema (use consistent field names across all services. If one service uses user_id and another uses userId, querying becomes painful.)
  • Limit yourself to four log levels: debug, info, warning, error
  • Never log unstructured strings (every log must be structured with queryable fields)

Common Pitfalls

  • Avoid multiple log lines per request. The only exception is temporary verbose logging during AI-assisted debugging sessions.
  • Design for unknown unknowns
  • Always propagate request IDs across services

# README.md

Agent Skills

A collection of curated skills for AI-assisted software development. Each skill provides focused guidelines that can be loaded into AI coding assistants to ensure consistent, high-quality output.

What are Skills?

Skills are structured markdown files containing best practices, guidelines, and conventions for specific development tasks. They help AI assistants understand project-specific rules and produce code that matches your team's standards.

Each skill includes:
- Purpose: What the skill helps with
- When to Apply: Conditions that trigger the skill
- Core Principles: Prioritized guidelines (CRITICAL, HIGH)
- Examples: Code samples demonstrating correct usage
- Anti-Patterns: Common mistakes to avoid

Available Skills

Software Engineering

Skill Description
general-logging Wide events logging pattern for powerful debugging and analytics
general-workflow Consultation-first approach for AI-assisted development
git-workflow Git branch naming, commits, and version control
python-code-style Python code style for clean, maintainable code
python-testing Python testing best practices using pytest
rust-code-style Rust code style with hexagonal architecture
rust-testing Rust testing best practices using cargo test
rust-cargo-make Cargo-make task runner for Rust build automation
rust-design-principles SOLID, KISS, and design patterns for Rust

Project Structure

agent-skills/
β”œβ”€β”€ README.md
└── software-engineering/
    β”œβ”€β”€ general-logging/
    β”‚   └── SKILL.md
    β”œβ”€β”€ general-workflow/
    β”‚   └── SKILL.md
    β”œβ”€β”€ git-workflow/
    β”‚   └── SKILL.md
    β”œβ”€β”€ python-code-style/
    β”‚   └── SKILL.md
    β”œβ”€β”€ python-testing/
    β”‚   └── SKILL.md
    β”œβ”€β”€ rust-cargo-make/
    β”‚   └── SKILL.md
    β”œβ”€β”€ rust-code-style/
    β”‚   └── SKILL.md
    β”œβ”€β”€ rust-design-principles/
    β”‚   └── SKILL.md
    └── rust-testing/
        └── SKILL.md

Usage

With Claude Code

Add skills to your project's CLAUDE.md file or reference them directly in conversations:

# CLAUDE.md

Please follow the guidelines in:
- software-engineering/general-logging/SKILL.md
- software-engineering/python-testing/SKILL.md

With Other AI Assistants

Copy the relevant SKILL.md content into your assistant's system prompt or project configuration.

Skill Format

Each skill follows a consistent structure:

---
name: skill-name
description: Brief description
license: MIT
metadata:
  author: [email protected]
  version: "0.0.1"
---

# Skill Name

Version: 0.0.1

## Purpose
What this skill helps with.

## When to Apply
Conditions that trigger this skill.

## Core Principles
### 1. Principle Name (CRITICAL|HIGH)
Explanation with code examples.

## Anti-Patterns to Avoid
Common mistakes.

## Guidelines
Quick reference rules.

Contributing

  1. Create a new directory under the appropriate category
  2. Add a SKILL.md following the format above
  3. Include practical examples and anti-patterns
  4. Keep guidelines focused and actionable

License

MIT

# Supported AI Coding Agents

This skill is compatible with the SKILL.md standard and works with all major AI coding agents:

Learn more about the SKILL.md standard and how to use these skills with your preferred AI coding agent.