Video in progress

How do you set up collecting logs into a central place in a multi-service system: ELK or Loki?

The application writes logs to stdout or a file, and an agent (Fluent Bit, Promtail, Filebeat) reads them and sends them to central storage. ELK indexes every field and gives powerful search, but it is expensive; Loki indexes only labels: cheap, but searching inside the text is slower.

📖 Answer

SSHing into 15 servers and running grep works up to two or three services. After that you need a central log system.

The usual setup:

application (JSON -> stdout) -> agent (Fluent Bit / Promtail) -> storage -> UI
                                                                  Elasticsearch -> Kibana
                                                                  Loki          -> Grafana

Elasticsearch (ELK/EFK):

  • every field is indexed, so orderId:881 AND level:ERROR is found instantly;
  • complex aggregations, full-text search;
  • but it uses a lot of disk and RAM, and running the cluster is a job of its own.

Loki:

  • only labels are indexed (app, namespace, level), and the text itself is compressed into object storage;
  • much cheaper, and in Grafana it sits in one place next to metrics and traces;
  • label choice matters: if you put userId in a label, you get the same cardinality problem as in Prometheus;
  • text search over a wide time range is slower.
{app="payment-service", level="ERROR"} | json | orderId="881"

Which one? If you need analytics and complex search over logs, Elasticsearch or OpenSearch. If the main need is "jump from a trace to its logs and see a particular service's errors" and the budget is limited, Loki.

Whichever you choose: logs should be JSON, every service should have a traceId field, and the retention period should be set explicitly.