Skip to content

Logging

Implement structured logging, log levels, and aggregation for effective observability.

What you'll learn

  • Why structured logging beats plain text
  • Log levels and when to use each
  • Log aggregation with the ELK stack
  • Best practices for searchable logs

Concept

Structured Logging

Traditional logging writes plain text messages: "User 42 logged in at 10:30". This is easy for humans to read but hard for machines to search and analyze. Structured logging writes logs as JSON: {"level":"info","user":42,"event":"login","time":"2024-01-01T10:30:00Z"}. Each field is searchable, filterable, and aggregatable. In production, you almost always want structured logs because log aggregation tools can index and query them efficiently.

Log Levels

Log levels categorize messages by severity. DEBUG is for detailed diagnostic information, off in production. INFO records normal operations — server started, request served. WARN indicates something unexpected but not fatal — a deprecated API was used, a retry succeeded. ERROR indicates a failure that needs attention — a request failed, a connection was lost. FATAL is for unrecoverable errors that crash the process. Set the minimum level per environment: DEBUG in development, INFO or WARN in production.

Log Aggregation

On a single server, you can read logs with tail and grep. But with dozens of containers and services, you need log aggregation — a central system that collects, indexes, and searches logs from all sources. The ELK stack (Elasticsearch, Logstash, Kibana) is a popular open-source choice. Loki is a lighter alternative. Datadog and Splunk are commercial options. These tools let you search across all services, filter by time and field, and create visualizations.

Best Practices

Log meaningful events, not every line of code. Include a request ID or trace ID in every log so you can follow a single request across services. Never log secrets — passwords, tokens, credit card numbers. Use consistent field names across services. Include enough context to diagnose problems: the user ID, the input that caused the error, the relevant configuration. Rotate and retain logs according to your compliance requirements.

Correlation IDs

A correlation ID (or trace ID) is a unique identifier attached to each incoming request and passed to all downstream services and log entries. When something goes wrong, you search for the correlation ID and see the complete journey of that request across every service it touched. This is essential for debugging distributed systems where a single user request may span five or more microservices. Generate the ID at the entry point (API gateway or middleware) and propagate it through HTTP headers, message queues, and database queries.

Example

              # Structured logging in Node.js (JSON format)
# {"level":"info","time":"2024-01-01T10:30:00Z","msg":"Request served",
#  "method":"GET","path":"/api/users","status":200,"durationMs":42}

# View and filter logs on a server
tail -f /var/log/myapp.log              # follow logs live
grep '"level":"error"' /var/log/myapp.log   # filter errors
jq 'select(.status >= 500)' < logs.json     # find server errors

# Docker container logs
docker logs -f --since 10m myapp       # last 10 minutes
docker logs myapp 2>&1 | grep ERROR    # filter errors

# Log rotation with logrotate
# /etc/logrotate.d/myapp:
#   /var/log/myapp.log { daily rotate 30 compress copytruncate }
            

Structured logs are JSON with searchable fields. tail and grep view logs live, jq filters JSON logs, and logrotate manages log file rotation to prevent disk fill.

Try it

Common mistakes

The mistake

Logging sensitive data like passwords or tokens

The fix

Never log secrets. Use a logging filter or sanitizer that redacts known sensitive fields. Review log output regularly to ensure no secrets leak into logs.

The mistake

Using console.log without structure

The fix

Use a structured logger like pino (Node.js) or structlog (Python) that outputs JSON. Plain text logs are hard to search and analyze at scale.

Practice Logging

2 exercises

Practice Logging

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors