Format a CSV Log File

Structured logging transforms application logs from human-readable text strings into machine-parseable records that can be filtered, aggregated, and analyzed programmatically. When exported as CSV from logging platforms like Datadog, CloudWatch Logs, or custom log aggregators, structured logs become a dataset you can sort, filter, and cross-reference with other operational data. Understanding the structure and patterns in log CSVs is an essential debugging skill. This example shows six log entries from a distributed system handling two requests. Request req_001 flows through the api-gateway, user-service, and back to api-gateway, completing successfully in 923ms. Request req_002 shows a failure pattern: the payment-service reports a connection timeout to stripe.com, and the api-gateway sends a 503 response. Reading these six rows tells the complete story of a payment processing failure. The request_id column is the key to distributed tracing. By filtering to all rows where request_id = req_002, you see the complete execution chain for that request across all services, in the exact sequence it happened (using the timestamp for ordering). This request correlation pattern — sometimes called a trace ID or correlation ID — is what separates debuggable microservice systems from ones where failures are impossible to diagnose. Log levels and their operational meaning: INFO logs document normal operations and are typically kept for 30-90 days for audit purposes. WARN logs indicate unusual conditions that aren't immediately harmful but deserve attention — the 850ms database query in this example is a performance warning that could lead to timeouts under load. ERROR logs indicate failures that affected functionality and should trigger monitoring alerts. The level column enables filtering to just errors or just warnings when investigating incidents. Message analysis for patterns: scanning the message column for recurring patterns reveals systemic issues. If "Connection timeout: stripe.com" appears in ERROR logs repeatedly over time, it indicates a networking or Stripe service reliability problem rather than a one-off event. The timestamp and count of ERROR entries from a specific service per minute is a key metric for incident detection. Real-world analysis workflows: export error logs from the past 24 hours as CSV, group by service name, count errors per service to identify which service is generating the most failures; correlate WARNING-level slow queries with subsequent ERROR-level timeouts to confirm the slow queries are a leading indicator of the timeouts. Tips: always include a request_id or trace_id in every log entry and propagate it through all service calls for the same request. Without this, debugging cross-service failures requires manually correlating timestamps across service logs, which is error-prone and slow.

Example
timestamp,level,service,request_id,message
2024-01-15T10:00:01Z,INFO,api-gateway,req_001,Request received: GET /api/users
2024-01-15T10:00:01Z,INFO,user-service,req_001,Fetching user list from database
2024-01-15T10:00:02Z,WARN,user-service,req_001,Slow query: 850ms for SELECT users
2024-01-15T10:00:02Z,INFO,api-gateway,req_001,Response sent: 200 OK in 923ms
2024-01-15T10:00:05Z,ERROR,payment-service,req_002,Connection timeout: stripe.com
2024-01-15T10:00:05Z,ERROR,api-gateway,req_002,Response sent: 503 Service Unavailable
[ open in CSV File Viewer → ]

FAQ

What is structured logging?
Structured logging records log entries as machine-readable key-value pairs (JSON or CSV) rather than plain text strings. This makes logs searchable, filterable, and easy to ingest into monitoring platforms.
What are the standard log levels?
Common levels in order of severity are TRACE, DEBUG, INFO, WARN, ERROR, and FATAL. Set production log level to INFO to capture normal operations and errors without the noise of debug messages.
How do I correlate logs across services?
Include a request_id or trace_id field in every log entry and propagate it across service calls. This lets you filter all log entries for a single end-to-end request.

Related Examples