Add --format json to miren logs (MIR-802)#675
Conversation
All four log subcommands (app, sandbox, build, system) now accept --format json, producing newline-delimited JSON (JSONL) that agents and scripts can parse. Each line is a JSON object with timestamp, stream, source, message, and attributes fields.
📝 WalkthroughWalkthroughThis pull request introduces JSON output formatting to the logs command. A new 📝 Coding Plan
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
cli/commands/logs_test.go (1)
171-177: Prefer structured key checks over raw substring checks.The current assertions can false-positive if
"source"or"attributes"appears inside message values. Unmarshal intomap[string]anyand assert key absence directly.♻️ Suggested test hardening
raw := strings.TrimSpace(buf.String()) - if strings.Contains(raw, "source") { - t.Errorf("expected source to be omitted, got: %s", raw) - } - if strings.Contains(raw, "attributes") { - t.Errorf("expected attributes to be omitted, got: %s", raw) - } + var obj map[string]any + if err := json.Unmarshal([]byte(raw), &obj); err != nil { + t.Fatalf("invalid JSON output: %v\nraw: %s", err, raw) + } + if _, ok := obj["source"]; ok { + t.Errorf("expected source to be omitted, got: %s", raw) + } + if _, ok := obj["attributes"]; ok { + t.Errorf("expected attributes to be omitted, got: %s", raw) + }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@cli/commands/logs_test.go` around lines 171 - 177, The test currently checks for "source" and "attributes" using substring searches on raw (buf.String()), which can false-positive; instead unmarshal the trimmed JSON string into a map[string]any (e.g., var m map[string]any) and assert that the keys "source" and "attributes" are not present (using _, ok := m["source"] / m["attributes"] and fail the test if ok is true). Replace the strings.Contains(raw, ...) checks with these structured key-absence assertions so the test verifies keys rather than arbitrary substrings.cli/commands/logs.go (1)
282-285: Avoid silent drop on JSON marshal failure.Returning without any signal makes entry loss invisible during troubleshooting. Emit an explicit stderr diagnostic at minimum.
🛠️ Suggested reliability tweak
data, err := json.Marshal(entry) if err != nil { + fmt.Fprintf(ctx.Stderr, "failed to encode log entry as JSON: %v\n", err) return }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@cli/commands/logs.go` around lines 282 - 285, The code currently swallows json.Marshal errors by simply returning after "data, err := json.Marshal(entry)" which silently drops log entries; update the error path in the function containing that line to emit an explicit diagnostic (e.g., write a descriptive message including err to stderr or use the existing logger) instead of a bare return, and consider returning the error to the caller if the function's signature allows; locate the "json.Marshal(entry)" call and replace the silent "return" with an explicit os.Stderr or logger output that includes err and context about the failed entry.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@cli/commands/logs_test.go`:
- Around line 171-177: The test currently checks for "source" and "attributes"
using substring searches on raw (buf.String()), which can false-positive;
instead unmarshal the trimmed JSON string into a map[string]any (e.g., var m
map[string]any) and assert that the keys "source" and "attributes" are not
present (using _, ok := m["source"] / m["attributes"] and fail the test if ok is
true). Replace the strings.Contains(raw, ...) checks with these structured
key-absence assertions so the test verifies keys rather than arbitrary
substrings.
In `@cli/commands/logs.go`:
- Around line 282-285: The code currently swallows json.Marshal errors by simply
returning after "data, err := json.Marshal(entry)" which silently drops log
entries; update the error path in the function containing that line to emit an
explicit diagnostic (e.g., write a descriptive message including err to stderr
or use the existing logger) instead of a bare return, and consider returning the
error to the caller if the function's signature allows; locate the
"json.Marshal(entry)" call and replace the silent "return" with an explicit
os.Stderr or logger output that includes err and context about the failed entry.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: edabd1bb-05bc-4b25-8480-205e51830217
📒 Files selected for processing (2)
cli/commands/logs.gocli/commands/logs_test.go
Summary
--format jsonflag to all four log subcommands (app,sandbox,build,system)timestamp,stream,source(optional),message, andattributes(optional)FormatOptions/--format jsonpattern used by other commandsTest plan
hack/it cli/commands— 94 tests)miren logs --format json | jq .produces valid JSON per linemiren logs --format json -fstreams correctly