CloudWatch Logs

When Something Breaks at 2 AM, Your Logs Should Tell You Why

Good logging turns a stressful 2 AM incident into a short investigation. Here's how to set it up.

Illustration of server logs flowing into a central log panel where a magnifying glass highlights the error line, with an alert bell and a night-time moon icon

Key takeaways

  • Logs are only useful during an incident if they are centralised, searchable and connected to metrics and traces.
  • Structured JSON logs with consistent fields such as level, service and request ID make filtering and analysis fast.
  • A request or correlation ID lets you follow one user's request across every service it touched.
  • Alerts should notify you about user impact, and their message should point to the dashboard or query that explains it.
  • Set retention periods and mask sensitive data. CloudWatch log groups keep data forever by default.

The 2 AM call

The following scenario is illustrative.

At 2:07 AM, the on-call engineer's phone goes off: checkout errors are rising. Half awake, they open the logs and find:

  • Logs scattered across a dozen servers and containers
  • Plain-text lines in different formats from different services
  • No way to tell which log lines belong to the failing requests

An hour later they find the cause: an expired API credential for the payment provider. The fix takes two minutes. Finding it took sixty.

Good logging doesn't prevent incidents. It makes them short.

Step 1: Centralise everything

You can't search what you can't reach. Send every log to one place: Amazon CloudWatch Logs.

  • AWS services such as Lambda, ECS, EKS, API Gateway and RDS can send logs to CloudWatch Logs with built-in integrations.
  • EC2 instances use the CloudWatch agent to ship application and system logs.
  • Log groups organise logs by application or service, for example /prod/checkout-api.

Once logs are centralised, servers and containers can come and go and their logs remain available, even after an Auto Scaling event has removed the instance that had the problem.

Step 2: Write structured logs

Compare these two log lines.

Unstructured:

Payment failed for order 8812 after 3 tries

Structured (JSON):

{"timestamp":"2026-10-21T02:05:41Z","level":"ERROR","service":"checkout-api","requestId":"a1b2c3","orderId":"8812","event":"payment_failed","dependency":"payment-gateway","status":401,"retries":3,"durationMs":2140}

The structured version can be filtered, counted and grouped by any field. Good practice is to:

  • Use the same field names across all services: timestamp, level, service, requestId and event.
  • Log events and outcomes, not just messages, such as the status codes and durations of dependency calls.
  • Use log levels consistently: DEBUG, INFO, WARN and ERROR.
  • Never log secrets or sensitive personal data, such as passwords, tokens or full card numbers.

Step 3: Connect requests with a correlation ID

A single user request may pass through a load balancer, an API, a database and an external service. Give each request a unique ID at the entry point, pass it to every downstream call, and include it in every log line.

During an incident, one search for that ID shows the request's full journey across services. Distributed tracing builds on the same idea. Instrument your application with OpenTelemetry and view traces in AWS X-Ray to see how long each step took, then jump from a slow trace to its logs using the shared ID.

Step 4: Query logs fast with CloudWatch Logs Insights

CloudWatch Logs Insights lets you search and analyse logs with a simple query language. With structured logs, an investigation that would take an hour can take minutes.

When did the errors start?

fields @timestamp, service, event | filter level = "ERROR" | stats count(*) by bin(5m)

Which dependency is failing?

filter event = "payment_failed" | stats count(*) by dependency, status

What happened to one specific request?

fields @timestamp, service, event, status | filter requestId = "a1b2c3" | sort @timestamp asc

Save your most useful queries so the on-call engineer doesn't have to write them from scratch at 2 AM. For live debugging, CloudWatch Logs Live Tail streams new log events as they arrive.

Step 5: Turn logs into metrics and alerts

Logs explain problems, but metrics and alarms tell you a problem exists.

  • Metric filters turn log patterns into CloudWatch metrics, for example a count of payment_failed events per minute.
  • CloudWatch alarms notify you when metrics such as error rate, latency or failed events cross thresholds you have chosen for your workload.
  • Dashboards put error rate, latency and dependency health side by side for a quick overview.

Make alerts actionable:

  • Alert on user impact, such as checkout errors or slow responses, not on every warning line.
  • Include a next step in the alert description, such as a link to the dashboard, a saved query or the runbook.
  • Keep alerts few and meaningful. Too many alerts teach people to ignore them.

Step 6: Control cost and protect sensitive data

Logging everything forever is expensive and risky. Three settings help:

  • Retention: CloudWatch log groups keep logs indefinitely by default. Set a retention period for each log group that matches your operational and compliance needs.
  • Log classes: the Infrequent Access log class can lower the cost of logs you rarely query, though it has fewer features. Check that it fits before you switch.
  • Data protection: CloudWatch Logs data protection policies can detect and mask sensitive data such as email addresses or credentials. Use them as a safety net alongside good logging practice.

Also limit who can read production logs with IAM. Logs often reveal more about your systems than you expect.

A 2 AM investigation, done right

With good logging in place, the same incident looks very different:

  1. 2:05 An alarm fires: the checkout error rate is above its threshold. The alert links to the checkout dashboard.
  2. 2:08 The dashboard shows errors starting at 2:01, while latency and server metrics look normal.
  3. 2:10 A saved Logs Insights query groups errors by dependency, and every error comes from payment-gateway with status 401.
  4. 2:12 A trace for one failed request confirms the payment call is being rejected.
  5. 2:15 The team rotates the expired credential, and the error rate returns to normal.

The fix is the same as before. The difference is ten minutes instead of sixty.

Quick checklist

  • All application and service logs are centralised in CloudWatch Logs
  • Logs are structured as JSON with consistent field names
  • A request ID is created at the entry point and included in every log line
  • Tracing is enabled for critical paths, using OpenTelemetry and X-Ray
  • Logs Insights queries are saved for common investigations
  • Metric filters and alarms track user-impacting errors
  • Alert descriptions link to dashboards, queries or runbooks
  • Retention periods are set on every log group
  • Sensitive data is masked and access to logs is restricted

Conclusion

Incidents will happen. What you control is how quickly you understand them. Centralised, structured logs, connected by request IDs and supported by metrics, alerts and tracing, turn a 2 AM mystery into a short, focused investigation.

Want faster answers when production breaks? The Pentafox Cloud team helps businesses build logging, monitoring and incident response practices on AWS. Get in touch for an observability review.

FAQ

Common Questions

  • Writing log entries in a consistent, machine-readable format, usually JSON, with named fields such as level, service and request ID. This makes logs easy to filter, count and analyse.

  • A CloudWatch feature that lets you search and analyse log data with a query language. You can filter events, count errors over time and group results by any field.

  • A unique ID assigned to a request when it enters your system and included in every related log line, so you can follow that request across all the services it touches.

  • By default, log groups keep data indefinitely. You can set a retention period on each log group to control cost and meet compliance needs.

  • No. Log meaningful events, errors and dependency calls with useful context. Avoid logging secrets or sensitive personal data, and set retention periods so logs don't grow forever.

Keep reading

View All Posts

Get started

Ready to Build on the Cloud?

Let's design a cloud environment on AWS, Azure or GCP that is secure, scalable, reliable and cost-effective.