Overview
Establishes centralized, structured logging for production systems. Covers JSON log format, log level guidelines, shipping options (Fluentd, Promtail, Vector, Filebeat), storage backends (ELK, Loki + Grafana, CloudWatch, Datadog), retention policies, and basic alerting on error rate or anomaly patterns. Includes code examples for Node.js, Python, and Go.
When to Use This Skill
- Moving from local file logs or
console.log to a proper observability stack.
- Debugging production issues that require historical logs.
- Meeting compliance or audit requirements.
Prerequisites
- Applications that can emit structured logs.
- A logging backend (self-hosted or SaaS).
- Network access from apps to the log shipper/collector.
Steps
Adopt structured logging:
- Use a library that outputs JSON (pino for Node, structlog or logging with json formatter for Python, zap for Go).
- Include standard fields:
timestamp, level, message, service, trace_id, user_id (when available), error.
Log level policy:
- ERROR: Something broke, needs attention.
- WARN: Degraded but recovered.
- INFO: Normal business events.
- DEBUG: Detailed for local/dev only (never in prod by default).
Choose shipping architecture:
- Sidecar / DaemonSet (Promtail for Loki, Fluent Bit).
- Direct from app (for cloud providers).
- Agent on host.
Storage & query:
- Loki + Grafana (lightweight, labels + LogQL).
- ELK (full text search, Kibana).
- Cloud native (CloudWatch Logs, GCP Logging).
Retention & cost:
- Hot/warm/cold tiers.
- 30-90 days hot, longer in cold storage or S3.
Alerting:
- High error rate (>1% or threshold).
- Sudden spike in logs (possible loop).
- Specific error strings (e.g., "OutOfMemory").
Output:
- Structured logger config for 2-3 languages.
- Docker / Kubernetes log shipping config (Promtail example).
- Basic Grafana dashboard for logs + error rate.
- Retention and alert rule examples.
Examples
Pino logger setup for Node, structlog for Python, Promtail config for Kubernetes, and a simple Loki alert rule are included.
Edge Cases & Error Handling
- High cardinality labels: Avoid putting user IDs or request IDs in labels in Loki (use in message or indexed fields carefully).
- Sensitive data: Never log passwords, tokens, PII. Use log redaction middleware.
- Log volume explosion: Sample debug logs in prod or use dynamic levels.
Verification
- Deploy the logger + shipper.
- Generate traffic and errors.
- Query logs in the backend by service, level, trace_id.
- Confirm an alert fires on injected errors.
- Success: All services emit consistent JSON logs, they are queryable centrally, and critical errors are alerted.
References
1---2name: log-aggregator-setup3description: Sets up centralized log aggregation with structured logging, log levels, and alerting. Use when implementing observability in production systems.4license: Apache-2.05---67## Overview89Establishes centralized, structured logging for production systems. Covers JSON log format, log level guidelines, shipping options (Fluentd, Promtail, Vector, Filebeat), storage backends (ELK, Loki + Grafana, CloudWatch, Datadog), retention policies, and basic alerting on error rate or anomaly patterns. Includes code examples for Node.js, Python, and Go.1011## When to Use This Skill1213- Moving from local file logs or `console.log` to a proper observability stack.14- Debugging production issues that require historical logs.15- Meeting compliance or audit requirements.1617## Prerequisites1819- Applications that can emit structured logs.20- A logging backend (self-hosted or SaaS).21- Network access from apps to the log shipper/collector.2223## Steps24251. **Adopt structured logging**:26 - Use a library that outputs JSON (pino for Node, structlog or logging with json formatter for Python, zap for Go).27 - Include standard fields: `timestamp`, `level`, `message`, `service`, `trace_id`, `user_id` (when available), `error`.28292. **Log level policy**:30 - ERROR: Something broke, needs attention.31 - WARN: Degraded but recovered.32 - INFO: Normal business events.33 - DEBUG: Detailed for local/dev only (never in prod by default).34353. **Choose shipping architecture**:36 - Sidecar / DaemonSet (Promtail for Loki, Fluent Bit).37 - Direct from app (for cloud providers).38 - Agent on host.39404. **Storage & query**:41 - Loki + Grafana (lightweight, labels + LogQL).42 - ELK (full text search, Kibana).43 - Cloud native (CloudWatch Logs, GCP Logging).44455. **Retention & cost**:46 - Hot/warm/cold tiers.47 - 30-90 days hot, longer in cold storage or S3.48496. **Alerting**:50 - High error rate (>1% or threshold).51 - Sudden spike in logs (possible loop).52 - Specific error strings (e.g., "OutOfMemory").53547. **Output**:55 - Structured logger config for 2-3 languages.56 - Docker / Kubernetes log shipping config (Promtail example).57 - Basic Grafana dashboard for logs + error rate.58 - Retention and alert rule examples.5960## Examples6162Pino logger setup for Node, structlog for Python, Promtail config for Kubernetes, and a simple Loki alert rule are included.6364## Edge Cases & Error Handling6566- **High cardinality labels**: Avoid putting user IDs or request IDs in labels in Loki (use in message or indexed fields carefully).67- **Sensitive data**: Never log passwords, tokens, PII. Use log redaction middleware.68- **Log volume explosion**: Sample debug logs in prod or use dynamic levels.6970## Verification71721. Deploy the logger + shipper.732. Generate traffic and errors.743. Query logs in the backend by service, level, trace_id.754. Confirm an alert fires on injected errors.765. Success: All services emit consistent JSON logs, they are queryable centrally, and critical errors are alerted.7778## References7980- [Pino](https://github.com/pinojs/pino)81- [Loki + Promtail](https://grafana.com/docs/loki/latest/)82- [Fluent Bit](https://fluentbit.io/)83- [Structured Logging](https://www.structlog.org/en/stable/)