Skip to content

stack: add 'docker stack logs' command - #7282

Open
dlarochette wants to merge 1 commit into
docker:masterfrom
dlarochette:feat/stack-logs
Open

stack: add 'docker stack logs' command#7282
dlarochette wants to merge 1 commit into
docker:masterfrom
dlarochette:feat/stack-logs

Conversation

@dlarochette

Copy link
Copy Markdown

What I did

Added a docker stack logs subcommand that fetches the logs of all services in a stack and aggregates them into a single stream, with each line prefixed by the (colored) name of the service it belongs to — the swarm equivalent of docker compose logs.

How I did it

  • ServiceList filtered on the com.docker.stack.namespace label (same filter as the other stack subcommands), erroring with the usual nothing found in stack: message when empty.
  • One goroutine per service calling ServiceLogs, demultiplexing the stream with stdcopy.StdCopy into a small line-buffered prefix writer. A shared mutex keeps concurrent lines intact; partial writes are buffered until a newline so lines are never split.
  • Rotating ANSI color palette per service (same spirit as compose), --no-color to opt out. Prefixes are padded to the longest service name so output aligns.
  • Flags supported: -f/--follow, --since, -n/--tail (per service), -t/--timestamps, --no-color — all mapping directly to the service logs API.

How to verify it

Unit tests cover argument validation, API error propagation, option passthrough, output prefixing, and the line-buffering writer:

go test ./cli/command/stack/ -run 'TestStackLogs|TestPrefixWriter|TestLogPrefix' -v

Verified manually on a 2-node swarm (services on both manager and worker):

$ docker stack logs --tail 2 --no-color d2
d2_local  | from-manager
d2_local  | from-manager
d2_remote | from-worker
d2_remote | from-worker

--follow streams continuously; --timestamps and --since behave as for docker service logs.

Description for the changelog

Add `docker stack logs` command to fetch aggregated, per-service-prefixed logs of all services in a swarm stack.

Add a 'docker stack logs' subcommand that fetches the logs of all
services in a stack and aggregates them into a single stream. Each
line is prefixed with the name of the service it belongs to, colored
per service (with --no-color to opt out), similar to what
'docker compose logs' does for compose projects.

Supports the usual log options: --follow, --since, --tail, and
--timestamps, which map directly to the service logs API.

Signed-off-by: David LAROCHETTE <david.larochette@movesol.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant