Structured Logging: Logs as Data, Not Strings
Treat logs as structured data (like JSON), not just plain text. This makes them machine-readable and queryable, letting you filter, search, and create dashboards on specific fields (e.g., `user_id`, `trace_id`).
WHY IT EXISTS: Traditional logging produces flat text files. As systems grow, searching terabytes of free-form text with tools like grep becomes slow, inefficient, and unreliable. You can't easily answer questions like "What's the average request latency for paying customers?" without complex, brittle parsing.
THE MENTAL MODEL: Think of your application's logs not as a diary, but as a database table. Each log entry is a row, and fields like timestamp, user_id, event_type, and duration_ms are columns. This lets you query your operational data like you would a database, unlocking powerful analysis.
HOW IT WORKS: Instead of concatenating strings to form a message like "Request failed for user 123 with status 500", you use a logging library to create a structured object. For example, in JSON: {"level": "error", "event": "request_failed", "user_id": 123, "status_code": 500}. This data is then sent to a logging backend (like Google Cloud Logging, Datadog, or an ELK stack) that can index these key-value pairs for fast searching and aggregation.
WHEN TO USE IT: Always, in modern application development. It is the foundation of observability in microservices, distributed systems, and any application where you need to debug complex issues, monitor performance, or create alerts based on specific conditions (e.g., "alert if the p99 latency for endpoint /api/v3/checkout exceeds 500ms").
WHEN NOT TO USE IT: The practice is almost universally beneficial. For a tiny, single-instance script, it might feel like overkill, but establishing the habit is valuable. The main trade-off is a slightly higher initial setup cost compared to simple print statements, but this pays for itself instantly during the first outage.
ONE CANONICAL EXAMPLE: A web server receives a request. A traditional log might say: [2023-10-27 10:00:00] INFO: GET /users/42 completed in 55ms. A structured log for the same event would be a JSON object: {"timestamp": "2023-10-27T10:00:00Z", "level": "info", "http_method": "GET", "http_path": "/users/42", "duration_ms": 55}. Now you can easily query for all requests to /users/* that took longer than 100ms across thousands of servers.
Read the original → docs.cloud.google.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.