swarmexec

swarmexec › Docs › Logs of services and stacks

Logs of services and stacks_

Every replica, several services or a whole stack, in one stream.

logs — stream logs

Stream a container's logs from anywhere in the swarm.

swarmexec logs [flags] <target>…
FlagDefaultDescription
-f, --followfalsekeep streaming new log lines
--tail0lines from the end to start with (0 = all)
-t, --timestampsfalseprefix each line with a timestamp
--since0s (no limit)only logs newer than this (e.g. 10m, 1h)
--log-formatfrom config, else classicparse lines as auto | classic | json | logfmt | gelf | raw — auto decides from the lines themselves (default from config, else classic)
--min-level—only show this level and above: trace | debug | info | warn | error | fatal. It filters the streamed output; swarmexec's own logging is the global --client-log-level
--grep—only show lines whose (parsed) message matches this Go regexp
--stack—follow every service of this stack (in addition to any targets)
--node—node hint/override for container-id targets
--connect-timeout10stimeout for connecting to the agent

Format-aware parsing & filtering. --log-format tells the client how to read each line so it can pull out a level and a message: classic extracts a level from a plain text line — a bracketed field ([error], [ info]), an ALL-CAPS word, or one closed by a colon (error:), which is what keeps a node called node-error-2 from turning every line that mentions it red; json parses logstash-style JSON (level/message fields), logfmt parses the key=value style used by many Go apps, the Docker daemon and HashiCorp tools (msg/message, level/lvl/severity, ts/time), gelf parses Graylog GELF JSON (numeric syslog level), and raw passes lines through unchanged. --min-level then drops anything below the chosen level, and --grep keeps only lines whose parsed message matches the regexp.

Lines with no detectable level always pass the --min-level filter, so multi-line stack traces aren't lost. The same defaults can be set once under the logs: config section (flags override).

Follow across container replacement. With -f on a service or service.slot target, logs keeps following when the container it is streaming is replaced by a rolling update, restart or reschedule: it re-resolves the service's current running container (the same slot, or the same node for a global service), reconnects automatically — like docker service logs -f — and prints a dim notice line (container replaced; reconnected to <id> on <node>). It waits up to ~30s for a replacement to schedule before giving up, and stops cleanly if the service is removed. A bare container-id target has no successor, so it simply stops as before.

Detection lives in the client: it learns topology from the swarm manager it is already connected to — the per-node agents are local to their node and cannot see swarm-level replacements.

A service with no running container. A service that crash-loops, fails its health check or exits on a bad config is the one whose logs you need, and it has nothing running. For a service or service.slot target with no running container, logs shows the newest exited container of each slot instead, says so on stderr (web has no running container; showing the last container (failed: task: non-zero exit (1))), and with -f picks up each new attempt as a replacement. If no task ever got a container (an image that cannot be pulled, no node that satisfies the constraints), there are no logs to read and the error names the newest task's state and reason instead. Several targets and --stack still skip a service with nothing running, with a note.

Several sources at once. Give several targets, a service with several replicas, or --stack <name>, and logs follows every source together: a service means all of its running replicas, as with docker service logs. Each line is prefixed with where it came from — web.2, or agent@node-3 for a global service — padded so the messages line up and coloured on a terminal (never with NO_COLOR set or when piped). Every source keeps its own reconnect across container replacements and its own --log-format auto detection, since services log differently; lines are merged whole, never torn between sources. A source that fails says so in its own lines and does not stop the others; the exit code tells you at the end. One target that is one container streams exactly as before, without a prefix.

Lines are merged in the order they arrive, not sorted by time across sources — add -t when the order between services matters. Each source is one stream against its node's agent, which counts toward the agent's -max-streams limit.
$ swarmexec logs -f --tail 100 web $ swarmexec logs --since 15m -t web $ swarmexec logs --log-format json --min-level warn web $ swarmexec logs --log-format auto --min-level warn web # let the lines decide $ swarmexec logs --grep 'timeout|refused' -f web $ swarmexec logs -f api worker db $ swarmexec logs -f --tail 20 --stack shop
Detecting the format. --log-format auto — and a in the TUI log view — scores the lines themselves against every built-in and takes the first one, most specific first, that explains at least two lines in three. GELF is checked before JSON because every GELF line is also valid JSON; a key=value line counts as logfmt only if it carries a msg or level key, so a URL query string and an access log do not. It samples the most recent lines, not the first ones, which are a framework's startup banner rather than the application. When the sample is too small or too mixed it says so and keeps the default instead of guessing: a wrong format is worse than none, because --min-level on a stream parsed wrongly filters nothing. On the pipe the first lines are held back until the verdict (at most 40, at most a second), so the output is never printed in two shapes. Still ahead: per-service detection from an online catalogue keyed by image — the same seam, a different source of the answer.