Skill 4.2.1

Implement effective logging strategies to record application behavior and state (for example, by using structured logging for application events and user actions).

Key Concepts

Use log levels (DEBUG, INFO, WARN, ERROR) and set the level per environment, keeping production at INFO or WARN; Lambda's advanced logging controls let you set the application and system log levels and JSON format without code changes. Log key business events and every error with context such as request ID, user or tenant ID and relevant parameters.

Set retention periods on log groups to control cost, since the default is never to expire. Never log secrets or personal data, and use CloudWatch Logs data protection policies as a safety net. Centralise logs from multiple accounts with cross-account log sharing or subscriptions.

A structured entry includes consistent fields such as timestamp, level, message, service name, request ID and a correlation ID that follows a request across services, plus event-specific fields like order_id or action. CloudWatch Logs Insights discovers JSON fields automatically, so you can filter and aggregate on them without parsing.

Lambda can emit logs in JSON format natively, and the Powertools for AWS Lambda Logger adds structured output, Lambda context, cold-start flags and correlation IDs from the incoming event. Audit-style logs of user actions should record who did what and when, without sensitive data.

Logging records discrete events, such as a request received or an error thrown, with details and context. Monitoring watches known metrics against thresholds and alerts when something you anticipated goes wrong, such as error rate above 1%.

Observability is the ability to understand a system's internal state from its outputs, including problems you did not anticipate. It combines logs, metrics and traces, correlated by IDs, so you can ask new questions of the data. On AWS, CloudWatch provides logs, metrics, alarms and Application Signals, X-Ray provides traces, and AWS Distro for OpenTelemetry provides vendor-neutral instrumentation.