Structured JSON Logging Best Practices
Comprehensive framework for implementing structured JSON logging across applications, covering schema design, framework setup, contextual logging, security, performance optimization, and enterprise integration patterns. Enables machine-readable logs for effective monitoring, debugging, and observability in distributed systems.
When to Apply
Reference these guidelines when:
- Implementing logging in new applications or services
- Migrating from unstructured text logging to structured formats
- Setting up centralized logging and monitoring systems
- Designing log schemas for microservices architectures
- Implementing observability and tracing in distributed systems
- Establishing logging standards across development teams
- Optimizing log performance and storage efficiency
- Ensuring log security and compliance requirements
- Integrating logs with monitoring and alerting systems
Rule Categories by Priority
| Priority | Category | Impact | Files | Rules |
|---|---|---|---|---|
| 1 | Schema Design | CRITICAL | 3 | 9 |
| 2 | Framework Setup | CRITICAL | 3 | 9 |
| 3 | Contextual Logging | HIGH | 3 | 8 |
| 4 | Security & Compliance | HIGH | 2 | 7 |
| 5 | Performance Optimization | MEDIUM-HIGH | 2 | 5 |
| 6 | Integration & Monitoring | MEDIUM-HIGH | 2 | 4 |
| 7 | Migration & Adoption | MEDIUM | 1 | 3 |
| Total | 7 Categories | 12 Files | 45 Rules |
Quick Reference
1. Schema Design (CRITICAL)
schema-base-fields: Standard fields for all log entriesschema-naming-conventions: Consistent field naming patternsschema-data-types: Appropriate data types for different valuesschema-contextual-fields: Request and trace correlationschema-custom-fields: Application-specific field definitionsschema-validation: Schema validation and enforcement
2. Framework Setup (CRITICAL)
python-structlog: Python structured logging with structlogpython-json-logger: Python JSON logging with python-json-loggerjavascript-winston: Node.js structured logging with Winstongo-logrus: Go structured logging with logrusjava-logback: Java structured logging with Logbackdotnet-serilog: .NET structured logging with Serilogfastapi-logging: FastAPI structured logging integrationdjango-logging: Django structured logging integrationexpress-logging: Express.js structured logging middleware
3. Contextual Logging (HIGH)
correlation-ids: Request and trace ID propagationuser-context: User and session context loggingbusiness-context: Business logic context enrichmenterror-context: Error and exception context captureperformance-context: Performance and timing context
4. Security & Compliance (HIGH)
sensitive-data-protection: Preventing sensitive data in logspii-masking: Personal identifiable information maskingaudit-logging: Security event and audit trail loggingcompliance-fields: Compliance-required log fieldsencryption-security: Log encryption and secure transportaccess-control: Log access control and permissions
5. Performance Optimization (MEDIUM-HIGH)
log-buffering: Efficient log buffering and batchingasync-logging: Non-blocking asynchronous logginglog-compression: Log compression for storage efficiencysampling-strategies: Log sampling for high-volume scenariosresource-limits: Memory and CPU limits for logging
6. Integration & Monitoring (MEDIUM-HIGH)
elk-integration: ELK stack structured logging integrationprometheus-metrics: Log-based metrics and alertingopentelemetry-tracing: Distributed tracing integrationlog-aggregation: Centralized log aggregation patterns
7. Migration & Adoption (MEDIUM)
migration-strategy: Gradual migration from unstructured logginglegacy-integration: Integrating with existing logging systemsteam-adoption: Team training and adoption strategies
How to Use
Read individual rule files for detailed explanations and code examples:
rules/schema-base-fields.md # Standard log schema
rules/python-structlog.md # Python implementation
rules/correlation-ids.md # Request correlation
rules/sensitive-data-protection.md # Security best practices
rules/elk-integration.md # ELK stack integration
Each rule file contains:
- Impact level and category
- Problem description with consequences
- Solution with code examples
- Common mistakes to avoid
- When to apply the rule
Reference Documentation
Detailed guides for complex topics:
references/log-schema-design.md # Complete log schema design
references/framework-comparison.md # Logging framework comparison
references/enterprise-patterns.md # Enterprise logging patterns
Full Compiled Document
For the complete structured JSON logging guide with all rules expanded: AGENTS.md
Structured JSON Logging Version: Compatible with modern logging frameworks and ELK stack Focus: Production-ready, enterprise-grade logging for observability and monitoring skills/structured-json-logging-skill/SKILL.md