Skip to content
← All guides

How to search logs across multiple servers at once

An incident rarely stays on one machine. A request fails on the load balancer, times out in one of four API nodes and leaves a stack trace on a worker you didn't think of. To find it you need to search the same pattern in the same log path on many servers, and do it fast.

This guide covers three approaches, from zero setup to a dedicated tool. All of them leave your log files where they are.

1. A shell loop over SSH

If you have SSH keys set up, a loop is the quickest start:

for host in api-01 api-02 api-03 worker-01; do
  ssh "$host" 'grep -n "ERROR" /var/log/app/app.log' | sed "s/^/$host: /"
done

The sed prefix tells you which server each line came from. To search rotated, compressed files as well, use zgrep, which reads both plain and .gz files:

ssh "$host" 'zgrep -n "OrderService" /var/log/app/app.log*'

Good for: a one-off check on a handful of hosts. Falls short when: hosts are searched one after another, so ten slow servers mean ten waits. Output from many hosts is hard to read, and every teammate needs SSH access and the right hostnames.

2. Parallel SSH

Run the loop bodies in parallel with & and wait, or use a tool built for it such as parallel-ssh (pssh):

for host in api-01 api-02 api-03 worker-01; do
  ssh "$host" 'grep -n "ERROR" /var/log/app/app.log' | sed "s/^/$host: /" &
done
wait

Results now arrive as each host finishes. You still get raw text: lines from different servers are not sorted by time, and narrowing down (only ERROR, only thread exec-302) means another round of grep.

3. A self-hosted search UI over SSH

The next step keeps the same model, search on the host over SSH, and adds what the shell lacks: saved server lists, results merged into one table, sorting by timestamp and filters on parsed fields.

Logswarm works this way. It runs as one Docker container, connects to your servers over SSH, runs ripgrep, zgrep or grep on each host, and streams only matching lines back to the browser. In practice:

  1. Add each server once (host, user, key) and test the connection.
  2. Group log paths into directories and presets, for example "all API nodes, /var/log/app/".
  3. Search the preset. Matches stream in from every host as they are found, with the server name and file line next to each result.
  4. A regex pattern splits each line into columns (timestamp, level, thread, message), so you can filter to level = ERROR without searching again.

Nothing is shipped to a central store, so there is no ingest pipeline or index to keep running. Read the Docker setup to try it.

Which approach to pick

SituationBest fit
2-3 servers, occasional checkShell loop
Many servers, you are comfortable reading raw outputParallel SSH
A team searching the same servers during incidentsA shared search UI over SSH
Long retention, dashboards and alerting on log dataA central log platform (see ELK, Graylog or Loki?)

Related