Skip to content

Logging

Introduction

Have the program write down "what happened" in a consistent, searchable format, so you can look back when something goes wrong.

Why use it

  • Investigate issues: when a user says “my order failed”, you can find the record by time and user.
  • Spot early warning signs: warning-level logs can signal trouble before a major outage.
  • Postmortems: know when the failure started and who was affected.

How to do it

  1. Use structured logs: do not write only a sentence; write a set of “fields” (usually JSON), such as user_id, order_id, and duration. Structured logs can be searched and aggregated by field; plain text can only be scanned by eye.
  2. Use clear levels:
    • Debug: development detail; off by default in production
    • Information: important normal events (order created)
    • Warning: abnormal but recoverable (retried once)
    • Error: the operation failed and needs attention
    • Critical/Fatal: the service is about to become unavailable
  3. Carry a correlation id: give every request a unique id and attach it to every log that request produces. Then one id can stitch together records across modules and services.
  4. Write to standard output and let the platform (Docker, K8s) collect it; do not manage log-file rotation yourself.
  5. Centralize storage: aggregate logs from many machines into one place to query (such as Loki or ELK).

Another common pitfall: logging on every loop iteration and blowing up disk and cost; or marking every exception as Error, so real problems are drowned out.

Component comparison

Serilog + Seq

Self-hosted open sourceFree tier + paidOpsMedium

Serilog, the popular structured logging library for C#, writes the logs, and Seq is a dedicated web tool for receiving and searching them; once installed you can query by field in the browser.

Best forSmall and mid-sized C# projects that want searchable, filterable logs without building an ELK stack.

Avoid whenThe project is in Python, or you need to aggregate logs from many languages and many machines.

Pros

Logs carry fields (user, order ID, etc.), making search far easier than digging through text files

Serilog works naturally with .NET's built-in ILogger, and setup takes a few lines of configuration

Seq runs with a single Docker command

Cons

Seq is a separate service that you must deploy, back up, and clean up old logs for

Seq's free license has usage limits (personal / small scale), so larger teams need to check the licensing terms

Only suited to C# projects; Python services need another approach

Official docs ↗

Write to files + automatic rotation (logging / structlog)

Built into the frameworkOpsLow

Python writes logs to local files using the standard library logging or structlog, automatically splitting files when they fill up and deleting old ones.

Best forA single server, personal or small-team projects where simply getting logs recorded matters most.

Avoid whenThere are multiple servers, or non-developers also need to read the logs.

Pros

No extra services to install and zero extra memory usage

The standard library has built-in rotation, so configuration is simple

When something breaks, just log in to the server and grep

Cons

With multiple servers you must log in to each one to read logs

No web interface; querying and statistics rely on the command line, which is unfriendly to people who do not know it

If the server disk fails the logs are lost with it, so a separate backup is needed

ELK / Elastic stack

Self-hosted open sourceOpen sourceOpsHigh

Ships logs centrally into Elasticsearch for indexing, with Kibana for web search and charts; the most full-featured veteran logging platform.

Best forTeams with large log volumes, multiple servers, and dedicated operations staff.

Avoid whenYou have only one or two small servers, or nobody can maintain it long term.

Pros

Very strong full-text search, fast even with large log volumes

Kibana provides charts and dashboards with rich features

Mature ecosystem with ready-made integrations for every language and many kinds of software

Cons

Very memory-hungry; a small 2-core 2 GB server basically cannot run it

You must maintain indexes, disk capacity, and version upgrades, which takes someone dedicated

Some advanced features in the free edition (such as certain alerting and security features) require a paid subscription

Official docs ↗

Grafana Loki

Self-hosted open sourceOpen sourceOpsMedium

A lighter logging system than ELK: it only labels logs without full-text indexing, and is viewed through Grafana's web interface.

Best forTeams already using Grafana that want centralized log querying while saving resources.

Avoid whenYou need powerful full-text search and complex analysis, or you have only one machine and file logs are enough.

Pros

Uses fewer resources and less storage than ELK

Used alongside Prometheus and Grafana, so metrics and logs can be switched between in the same interface

Filtering logs by label is easier to pick up than Elasticsearch

Cons

Searching by content (full-text search) is slower than Elasticsearch

You must deploy an extra collection agent (such as Promtail or Grafana Alloy)

Poor label design slows queries, which takes some experience to avoid

Official docs ↗

Cloud provider log service (e.g. Alibaba Cloud SLS)

Cloud-managedPay-as-you-goOpsLow

Send logs to a cloud provider, which handles storage, search, and alerting while you just view them.

Best forProjects whose servers are already on this cloud and that do not want to operate a logging system themselves.

Avoid whenThe budget is extremely tight, log volume is very unpredictable, or data must not leave your own data center.

Pros

No need to build or maintain anything yourself; works out of the box

Capacity scales with log volume, so there is no risk of filling the disk

Usually comes with search, charts, and alerting

Cons

Billed by storage and query volume, so the bill rises when log volume spikes

Tied to one cloud, so switching clouds later means changing the collection configuration

Data is stored with a third party, so check first if you have compliance requirements

pino

Open-source libraryOpen sourceOpsLow

A high-performance structured (JSON) logging library for Node.js, used by default in Fastify.

Best forApplication logging for any Node.js backend.

Avoid whenThe framework already mandates a different logging system.

Pros

Fast, with structured JSON output

Supports field redaction (redact)

Mature ecosystem that connects to all kinds of logging platforms

Cons

Default output is hard for humans to read, so development needs pino-pretty

Async transports may lose the last few entries when the process crashes

You must define how the request ID is propagated

Official docs ↗

tracing + tracing-subscriber

Open-source libraryOpen sourceOpsLow

The usual structured logging and span facility for Rust, with JSON output.

Best forApplication logging for any Rust backend.

Avoid whenThe team has already standardized on another logging facade and will not migrate soon.

Pros

Spans can carry the request ID through the call chain

JSON output feeds logging platforms

Mature integration with Tokio and Axum

Cons

Subscriber and filter setup needs a team convention

Over-fine log levels slow the process

Older log-crate code needs a bridge

Official docs ↗

log/slog (standard library)

Open-source libraryOpen sourceOpsLow

Structured logging built into Go's standard library, supporting JSON output and log levels.

Best forApplication logging for any Go backend.

Avoid whenYou need advanced features and the team already has a unified third-party logging library.

Pros

Standard library, zero dependencies

Structured key-value pairs, switchable between JSON and text output

Works with context to carry the request ID

Cons

Leaner than third-party libraries (sampling and redaction, for example, need your own wrapper)

Migrating legacy code requires a unified entry point

Field naming conventions must be agreed on

Official docs ↗

Checklist

  • Use structured logs (JSON), not arbitrarily concatenated sentences
  • Every request carries a correlation id through all of its logs
  • Production defaults to Information and can be adjusted without restart
  • Logs contain no passwords, tokens, ID numbers, or similar sensitive data
  • Logs go to standard output and are collected centrally
  • Logs can be correlated with error tracking via the same request id