home / skills / dasien / claudemultiagenttemplate / logging-strategies

This skill helps you implement structured logging with correlation, levels, and context to improve debugging and monitoring across services.

npx playbooks add skill dasien/claudemultiagenttemplate --skill logging-strategies

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
2.7 KB
---
name: "Logging Strategies"
description: "Implement structured logging with appropriate levels, context, and correlation for effective debugging and monitoring"
category: "observability"
required_tools: ["Read", "Write", "Edit"]
---

## Purpose
Create comprehensive logging that enables debugging, monitoring, and analysis of application behavior in production.

## When to Use
- Implementing new features
- Debugging production issues
- Performance monitoring
- Security auditing

## Key Capabilities
1. **Structured Logging** - JSON logs with context
2. **Log Levels** - Appropriate severity classification
3. **Correlation** - Trace requests across services

## Example
```python
import logging
import json
from datetime import datetime
import uuid

class StructuredLogger:
    def __init__(self, service_name):
        self.service = service_name
        self.logger = logging.getLogger(service_name)
    
    def log(self, level, message, **context):
        log_entry = {
            'timestamp': datetime.utcnow().isoformat(),
            'service': self.service,
            'level': level,
            'message': message,
            **context
        }
        
        log_line = json.dumps(log_entry)
        
        if level == 'ERROR':
            self.logger.error(log_line)
        elif level == 'WARNING':
            self.logger.warning(log_line)
        elif level == 'INFO':
            self.logger.info(log_line)
        else:
            self.logger.debug(log_line)
    
    def with_context(self, **context):
        class ContextLogger:
            def __init__(self, parent, ctx):
                self.parent = parent
                self.context = ctx
            
            def info(self, message, **extra):
                self.parent.log('INFO', message, **{**self.context, **extra})
            
            def error(self, message, **extra):
                self.parent.log('ERROR', message, **{**self.context, **extra})
        
        return ContextLogger(self, context)

# Usage
logger = StructuredLogger('api-service')

def handle_request(request_id, user_id):
    request_logger = logger.with_context(
        request_id=request_id,
        user_id=user_id
    )
    
    request_logger.info("Processing request")
    
    try:
        # Business logic
        result = process_order()
        request_logger.info("Order processed", order_id=result.id)
    except Exception as e:
        request_logger.error("Order failed", error=str(e), exc_info=True)
```

## Best Practices
- ✅ Use structured logging (JSON)
- ✅ Include correlation IDs
- ✅ Log at appropriate levels
- ✅ Include context (user_id, request_id)
- ✅ Log errors with stack traces
- ❌ Avoid: Logging sensitive data (passwords, tokens)
- ❌ Avoid: Excessive debug logging in production

Overview

This skill implements structured logging for multi-agent Python systems to improve debugging, monitoring, and incident response. It standardizes JSON log entries, enforces severity levels, and attaches contextual and correlation identifiers for tracing requests across services. The result is consistent, searchable logs suitable for observability pipelines.

How this skill works

The skill wraps Python's logging with a small StructuredLogger that emits JSON-formatted log lines containing timestamp, service name, level, message, and arbitrary context fields. It supports scoped context via a with_context helper so per-request or per-agent metadata (request_id, user_id) is applied automatically. Errors include stack traces and logs use severity routing (DEBUG, INFO, WARNING, ERROR) so downstream collectors and alerting systems can filter reliably.

When to use it

  • During implementation of new features to capture actionable runtime data
  • When debugging production incidents to trace behavior across services
  • For performance and health monitoring with structured metrics extracted from logs
  • During security audits to ensure sensitive events are recorded and traceable
  • When building multi-agent workflows that require correlation across asynchronous tasks

Best practices

  • Emit logs as JSON for consistent parsing by log aggregators
  • Always include correlation IDs (request_id, trace_id) to trace distributed flows
  • Choose log levels conservatively; use ERROR for failures, WARNING for anomalies, INFO for normal operations
  • Attach contextual fields (service, user_id, endpoint) instead of embedding them in messages
  • Avoid logging sensitive data (passwords, tokens, PII) and redact where necessary
  • Limit verbose debug logging in production; use sampling or dynamic log level controls

Example use cases

  • API service attaches request_id and user_id to every log for end-to-end tracing
  • Worker agents include job_id and agent_id to link task processing steps
  • Error handlers log exceptions with exc_info to capture stack traces for postmortems
  • Monitoring pipeline parses JSON logs to generate metrics and trigger alerts on ERROR spikes
  • Security audits search structured logs for suspicious user_id or IP patterns

FAQ

How do I add a correlation ID to existing code paths?

Generate a request_id at the entry point (HTTP handler or task dispatcher) and propagate it through function calls or attach via with_context so downstream logs include the same ID.

What should I avoid logging?

Never log secrets such as passwords, API keys, or full tokens. Mask or omit PII and consider using a redaction layer before logs are emitted.