Application Logging (Structured)
Emit application logs as structured, machine-parseable records with consistent fields for efficient searching and analysis.
Description
Structured logging is the practice of emitting log entries as machine-parseable records (typically JSON) rather than free-form text strings. Each log entry contains consistent fields such as timestamp, log level, message, request ID, user ID, service name, and any contextual data relevant to the event. This structure enables powerful querying, filtering, alerting, and visualization in log management platforms.
A well-structured logging setup uses log levels consistently: ERROR for unrecoverable failures requiring attention, WARN for recoverable anomalies, INFO for significant business events and request summaries, and DEBUG for detailed diagnostic information (disabled in production by default). Every log entry should include a correlation/request ID that traces a request across all services and middleware, enabling end-to-end request tracing. Sensitive data (passwords, tokens, PII) must be redacted before logging.
In a 12-Factor App architecture, logs are written to stdout/stderr as an unbuffered stream and captured by the execution environment (Docker logging driver, systemd journal, Kubernetes pod logs). The application should never concern itself with log routing, storage, or rotation -- those are infrastructure concerns. Libraries like pino (Node.js), winston (Node.js), or structlog (Python) provide structured logging with configurable transports, child loggers for adding contextual fields, and serializers for safe object stringification.
Prompt Snippet
Implement structured JSON logging using pino with a base context including service name, environment, and git SHA. Configure log level from an environment variable (LOG_LEVEL, defaulting to 'info'). Create an Express/Fastify request logging middleware that logs each request with method, path, status code, response time in ms, content-length, and a unique X-Request-ID (generated via crypto.randomUUID and propagated in response headers). Use pino.child() to create request-scoped loggers that automatically include the request ID in every downstream log entry. Redact sensitive fields (authorization, cookie, x-api-key) using pino's redact option. Output to stdout in JSON format with no formatting in production, and use pino-pretty in development.
Tags
Related Terms
Log Aggregation
Collect, centralize, and index logs from all application instances and services into a searchable platform.
Error Tracking (Sentry)
Capture, aggregate, and triage application errors in real-time with full stack traces and contextual data.
Application Monitoring (APM)
Monitor application performance, trace requests across services, and identify bottlenecks using APM instrumentation.
12-Factor App Methodology
A set of twelve principles for building modern, scalable, maintainable software-as-a-service applications.