4.2.2 Effective Logging Strategy

Implement an Effective Logging Strategy

Instrumentation

Instrumentation refers to the measure of a product’s performance to:

  • Write trace information
  • Diagnose errors
  • Record application behavior and state
  • Feed data into operational dashboards
  • Add real-time metrics for visibility

Centralized & Structured Logging

Centralized logging standardizes application logging cho:

  • Operational information về workload state
  • User behavior tracking
  • System state monitoring
  • Anomalous events detection

Structured logging libraries ensure:

  • ✅ Configurable logging levels
  • ✅ API consistency
  • ✅ Common output formats (JSON)
  • ✅ Easy parsing and analysis

Lambda Powertools Logging

from aws_lambda_powertools import Logger

logger = Logger(service="order-service")

@logger.inject_lambda_context
def handler(event, context):
    logger.info("Processing order", extra={"order_id": event['orderId']})
    # Automatically includes: request_id, function_name, cold_start

Lợi ích:

  • Structured JSON logs
  • Automatic context injection
  • Correlation IDs
  • Sampling support

Logging Levels

LevelUse Case
DEBUGDetailed diagnostic (dev only)
INFONormal operations, business events
WARNUnexpected but handled situations
ERRORFailures needing attention
CRITICALSystem-level failures

What to Log

✅ Log❌ Don’t Log
Request IDsPasswords, tokens
Error messages + stack tracesCredit card numbers
Business events (order created)PII (full email, SSN)
Performance metrics (duration)Raw request bodies with PII
State transitionsAPI keys, secrets
User behaviorSensitive data
System stateCompliance-restricted info
Anomalous events

CloudWatch Logs Insights for Lambda Performance

-- Find Lambda performance data from default log events
fields @timestamp, @duration, @billedDuration, @maxMemoryUsed
| filter @type = "REPORT"
| stats avg(@duration) as avg_duration,
        max(@duration) as max_duration,
        pct(@duration, 99) as p99_duration
  by bin(5m)

Lambda Logging Best Practice

import logging
import json

logger = logging.getLogger()
logger.setLevel(logging.INFO)

def handler(event, context):
    logger.info(json.dumps({
        "action": "process_order",
        "order_id": event.get("orderId"),
        "remaining_ms": context.get_remaining_time_in_millis()
    }))

Log Retention Strategy

EnvironmentRetention
Dev1-7 days
Staging14-30 days
Production30-90 days
Compliance1-10 years (export to S3)

Multi-layered Architectures & Distributed Systems

Understanding multi-layered architectures và distributed systems requires more than traditional logs and infrastructure metrics. Cần kết hợp:

  • Centralized logging
  • Structured logging
  • Distributed tracing
  • Custom metrics

Exam Tip:

  • Set log retention — default never expires (costs!)
  • Log levels: ERROR for production, DEBUG for dev
  • Never log secrets or PII
  • Use structured logging (JSON) cho easy parsing
  • Lambda Powertools simplifies structured logging
  • CloudWatch Logs Insights có thể extract Lambda performance data