feat(localnet): add --json flag to logs command#39
Conversation
|
@teyrebaz33 please, sign commits so that we can merge :) |
There was a problem hiding this comment.
Pull request overview
Adds --json support to localnet logs to align diagnostic commands (localnet status, doctor) with machine-readable output expectations.
Changes:
- Added
--jsonflag plumbing forlocalnet logsfrom CLI parsing through to command dispatch. - Implemented structured JSON output for
localnet logs, includingtailandlines. - Added CLI tests covering help output and JSON behavior for missing/tailed logs.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/cli.rs |
Adds json: bool to LocalnetLogsArgs and forwards it into LocalnetAction::Logs. |
src/commands/localnet.rs |
Extends LocalnetAction::Logs with json and emits JSON output for logs. |
tests/cli.rs |
Adds tests validating --json appears in help and JSON output shape for missing log file and tailing. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if !log_path.exists() { | ||
| println!("log file does not exist yet: {}", log_path.display()); | ||
| return Ok(()); | ||
| return print_log_lines(tail, &[], log_path, json); |
There was a problem hiding this comment.
cmd_localnet_logs no longer distinguishes a missing log file from an empty one in text mode: when the file doesn’t exist it now flows through print_log_lines and prints “log file is empty”. This is misleading (and differs from read_log_tail, which reports missing files explicitly). Consider keeping the old “does not exist yet” message for non---json output, while still emitting an empty JSON payload for --json.
| return print_log_lines(tail, &[], log_path, json); | |
| if json { | |
| return print_log_lines(tail, &[], log_path, true); | |
| } | |
| println!("log file does not exist yet: {}", log_path.display()); | |
| return Ok(()); |
There was a problem hiding this comment.
Fixed in the latest commit: when the log file does not exist and --json is off, we now print the original 'log file does not exist yet' message. Only the JSON path goes through print_log_lines with an empty slice.
| let content = fs::read_to_string(log_path) | ||
| .with_context(|| format!("failed to read log file {}", log_path.display()))?; | ||
|
|
||
| if content.trim().is_empty() { | ||
| println!("log file is empty: {}", log_path.display()); | ||
| return Ok(()); | ||
| } | ||
|
|
||
| let lines: Vec<&str> = content.lines().collect(); | ||
| let start = lines.len().saturating_sub(tail); | ||
| for line in &lines[start..] { | ||
| println!("{line}"); | ||
| } | ||
|
|
||
| Ok(()) | ||
| print_log_lines(tail, &lines[start..], log_path, json) |
There was a problem hiding this comment.
The previous content.trim().is_empty() check was removed. As a result, files containing only whitespace/newlines will be treated as having log lines (possibly empty strings) and will be printed/emitted in JSON rather than reported as empty. If the intent is “no meaningful log output yet”, consider restoring a trim/blank check (and/or filtering empty lines) before building the lines array.
There was a problem hiding this comment.
Fixed: added a blank-line filter before slicing the tail, so files containing only whitespace/newlines are treated as empty and reported accordingly.
b4a96e3 to
69420ee
Compare
|
All commits have been re-signed with GPG — they now show as Verified on GitHub. |
|
Thanks @teyrebaz33. Please revise against the points below, then ping me for another pass. The repo's
|
|
First — apologies for the three-week silence on our side. That's on us, not you. Thanks for sticking with this and for already turning around two rounds of review feedback; the A few small things to clean up before we land it: Local checks before the next push: cargo fmt # currently fails: 3 spots in src/commands/localnet.rs and tests/cli.rs
cargo test --all-targets
git rebase origin/master # branch picked up a conflict during the waitThe tail-handling refactor went a touch further than it needed to. Issue #38 only asked for
Easiest fix is the smaller one: restore the original A few more tests would lock the edges down:
One note on goalposts that moved while you waited. Some of the contributor expectations (filling in Your Project or User Demand / User Story / Verification in the PR body, and updating
Totally optional, not blocking: the new JSON payload uses Thanks again for your patience — once the above is in we should be able to merge quickly this time. |
Closes #38
Summary
localnet logswas the only diagnostic command without--jsonsupport. This PR adds it for consistency withlocalnet status --jsonanddoctor --json.Changes
src/cli.rs: Addedjson: boolfield toLocalnetLogsArgssrc/commands/localnet.rs:cmd_localnet_logsnow acceptsjsonparam and emits structured outputtests/cli.rs: 3 new tests covering help text, empty log file, and tail outputOutput format
{ "tail": 50, "lines": [ "2026-04-06T10:00:00Z sequencer started", "2026-04-06T10:00:01Z listening on 127.0.0.1:3040" ] }