Course contentsShow
← Software Engineering
Lesson 1130 of 1,93334. Logging and ObservabilityFree lesson

Key-Value Pairs and Log Fields

Understand how to add context through structured fields like user_id, request_id, and error_code alongside log messages.

Key-Value Pairs and Log Fields

What you'll learn: How to enrich log messages with structured context fields to make logs searchable, filterable, and truly useful for debugging and analysis.

The Core Idea

While structured logging gives you machine-readable format (like JSON), key-value pairs are what you actually put inside that structure. Instead of cramming everything into a single string message, you attach specific fields like user_id, request_id, error_code, or duration_ms alongside your log message.

Think of it like metadata on a photo: the image itself is your log message, but the EXIF data (location, timestamp, camera settings) provides the searchable context that makes that photo findable later.

Why Key-Value Fields Matter

When you log "User login failed", you're stuck. Which user? What error? Now compare this with fields:

{
  "message": "User login failed",
  "user_id": "12345",
  "error_code": "INVALID_PASSWORD",
  "request_id": "req-abc-789",
  "ip_address": "192.168.1.100"
}

Now you can instantly filter all failures for that user, trace the entire request path using request_id, or analyze patterns by error_code. You're not parsing strings—you're querying data.

Common Context Fields

  • Identifiers: user_id, session_id, request_id, transaction_id
  • Errors: error_code, error_type, stack_trace
  • Performance: duration_ms, response_size, db_query_time
  • Environment: service_name, hostname, version
  • Business: order_id, product_id, payment_method

The key is consistency: use the same field names across your entire system so logs from different services can correlate seamlessly.

Key Takeaway: Key-value fields transform logs from unstructured text into queryable data, enabling you to filter, correlate, and analyze events across distributed systems efficiently.