Add --format flag to bin/cluster (console|line|json)#24
Merged
Conversation
The cluster watcher previously hard-wired the log formatter based on whether --file was used: ConsoleFormatter for STDOUT, LineFormatter for files. Both are text formats, so applications shipping logs to a structured-log backend (DigitalOcean App Platform, Datadog, ELK, etc.) had no way to make context fields queryable without forking the binary or replacing it with a custom watcher entry point. This change adds --format <console|line|json> with a backwards-compatible default: when --file is set the default stays line, otherwise console. The new json branch wires Monolog\Formatter\JsonFormatter, which emits one JSON object per line including channel, level_name, datetime, context, and extra as first-class fields. The format and transport are now independent: --format json --file and --format console (no --file) all work as expected. Invalid values throw with a clear "Valid: console, line, json" message. Tested with examples/cluster/hello-world.php for all three formats and an invalid value.
Member
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The cluster watcher in
bin/clusterhard-wires the log formatter based on whether--fileis used:ConsoleFormatterfor STDOUT,LineFormatterfor files. Both are text formats.Applications shipping logs to a structured-log backend (DigitalOcean App Platform, Datadog, ELK, GCP Cloud Logging, …) need JSON output so context fields (channel, level_name, datetime, custom context like
requestId) become first-class queryable fields rather than being buried as a stringified suffix in a text line.Today the only ways to get JSON are: vendor the binary, replace it with a custom watcher entry point, or post-process logs downstream. Each is a maintenance burden for a 10-line conceptual change.
Proposed change
Add a new
--format <console|line|json>flag tobin/cluster. Default behaviour is preserved:--format, no--file→console(current default).--format,--fileset →line(current default).--format json→Monolog\Formatter\JsonFormatter(one JSON object per line, all fields structured).The format and transport are now independent:
--format json --file pathworks,--format consoleto stdout works, etc.Invalid values throw a clear
Invalid --format value 'X'. Valid: console, line, jsonmessage.Backwards compatibility
bin/cluster.Test plan
Tested locally with
examples/cluster/hello-world.phpfor all three formats and an invalid value:I can add unit tests if there's a precedent for testing
bin/clusteritself — I didn't find one intest/, so left it as a manual exercise consistent with the existing CLI surface.Why we are filing this
We run a multi-worker ReactPHP cluster and ship logs to DigitalOcean's log explorer, which treats text-suffix JSON as opaque text. Bumping
bin/clusterto support JSON output is the smallest correct change and keeps the binary as a single source of truth instead of every consumer maintaining a custom launcher.