Logging

Purpose

Logging is the practice of recording events, state changes and errors during software execution to support operations, debugging and analysis.

Well-structured logs are the primary tool for understanding production behaviour, diagnosing incidents and monitoring system health.

This guide defines the principles and standards for logging across all services and applications.

Background

Logs serve three primary audiences:

AudienceNeed
OperatorsReal-time visibility into system health and incidents.
EngineersHistorical record for debugging and post-mortem analysis.
SystemsMachine consumption for monitoring, alerting and analytics.

Each audience benefits from logs that are structured, consistent and context-rich.

Principles

Log in Structured Format

Always use a structured, machine-parseable format (JSON, key-value).

Structured logs are searchable, filterable and consumable by log aggregation systems.

Good (JSON):

{"timestamp": "2026-07-27T10:00:00Z", "level": "ERROR",
 "service": "order-service", "request_id": "req-abc-123",
 "message": "Payment processing failed",
 "error": "payment_declined", "duration_ms": 452}

Avoid (free-text):

[2026-07-27 10:00:00] ERROR: Payment failed after 452ms

Log at the Appropriate Level

LevelUse When
ERRORA failure that requires human investigation. The system
cannot complete the requested operation.
WARNAn unexpected condition that the system handled
gracefully. Does not require immediate action but may
indicate a future problem.
INFOSignificant lifecycle events: service startup, shutdown,
configuration load, successful deployment, state
transitions.
DEBUGDetailed information for diagnosing issues during
development or incident investigation. Should be disabled
in production under normal operation.
TRACEVery detailed diagnostic information. Typically used only
for tracing specific requests through the system.

Include Context with Every Log Event***

Every log event should include enough context to understand what happened without requiring additional investigation.

Essential context fields:

Additional context for relevant events:

Never Log Sensitive Information***

Sensitive data must never appear in logs.

This includes:

Logs are a common target for attackers. Treat log content as a security concern.

If sensitive data must be logged for debugging, ensure it is:

Standards

Log Event Structure

Every log event should include these standard fields:

{
  "timestamp":   "2026-07-27T10:00:00.123Z",
  "level":       "INFO",
  "service":     "order-service",
  "environment": "production",
  "request_id":  "req-abc-123",
  "trace_id":    "trace-xyz-789",
  "message":     "Order created successfully"
}

Request Correlation

Every request should carry a unique request_id that is propagated across service boundaries.

The request_id enables tracing a request through multiple services and log sources.

Error Logging

When logging errors:

Anti-patterns

Logging in Loops

Logging inside a loop produces excessive volume and obscures meaningful events.

Log after the loop completes, or log periodically with a summary.

Logging Control Flow

Logging the entry and exit of every function produces noise.

Log at meaningful boundaries: service entry points, external calls, state transitions and errors.

Logging Without Context

A log message like User not found without identifying which user or which operation is useless.

Always include relevant identifiers.

Mixing Log Levels

Using INFO for errors or ERROR for warnings undermines the meaning of log levels and makes filtering unreliable.

AI Integration

AI assistants can help with logging by:

When asking AI for logging advice, provide:

Related Documents