Add logs command to show and follow bridge log files#209
Open
Add logs command to show and follow bridge log files#209
logs command to show and follow bridge log files#209Conversation
Wraps the bridge log file (~/.mcpc/logs/bridge-<session>.log) so users no longer need to know the raw path or hand-tail it. Defaults to last 100 lines; supports -n/--tail, --follow, --since (duration shorthand or ISO timestamp), and --json. Transparently spans rotated files (.log.1 … .log.5) when more lines are needed. With --json, lines are parsed into structured records; with --follow + --json, output is NDJSON. Adds _mcpc.logPath and _mcpc.logSize to `mcpc @<session> --json` output, and replaces "For details, check logs at <path>" hints with the more actionable "For details, run: mcpc @<session> logs".
CI fix:
unauthorized-persist.test.sh asserted the old "check logs at <path>"
hint, which the previous commit replaced with "run: mcpc <session>
logs". Updated the assertions to match the new hint.
New test coverage:
- test/e2e/suites/sessions/logs.test.sh: 14 end-to-end cases driving
the actual CLI (errors, --json shape, -n/--tail, --since with
duration + ISO, rotation spanning across .log.1/.log.2,
chronological ordering, --since across rotations, _mcpc.logPath /
_mcpc.logSize on session JSON, and the new "mcpc <session> logs"
error hint).
- test/unit/cli/logs.test.ts: 10 unit tests for the showLogs handler
covering @-target validation, missing-session error, invalid
--since, human stderr/stdout split, "no logs yet" header, JSON
structured/raw shape, --tail, --since, rotation file count, and
default tail of 100.
- test/unit/lib/log-reader.test.ts expanded from 14 → 28 cases:
parseLogLine edges (empty body, stack frames, every level),
parseDuration (long forms, case/whitespace, garbage), getBridge-
LogPath, listLogFiles edge cases (missing dir, non-numeric
suffixes, prefix-collision sessions), readRecentLogLines edges
(no trailing newline, blank lines, multi-rotation tail, tail = 0,
--since drops everything, --since across rotations, tail + since
combined), and 8 followLog cases (append detection, no backlog
replay, partial-line buffering, rotation re-open, startAtBeginning,
file-doesn't-exist-yet, stop() flushes partial trailing line,
idempotent stop()).
To make followLog deterministically testable I added a FollowOptions
parameter (pollIntervalMs, startAtBeginning) — the production caller
keeps the 1s default.
627 unit tests pass (up from 591).
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.
Summary
Fixes #205
Adds a new
mcpc @<session> logscommand that allows users to view and follow bridge log files for MCP sessions. The implementation includes a log reader library with support for log file rotation, timestamp filtering, and live tailing.Key Changes
New log reader library (
src/lib/log-reader.ts):parseLogLine(): Parses structured log records from raw lines with ISO timestamps, log levels, and optional context tagslistLogFiles(): Discovers and orders rotated log files (.log.5→.log.1→.log)readRecentLogLines(): Reads recent logs with support for--tail(limit lines) and--since(timestamp filtering)followLog(): Live-follows the current log file with rotation detection and belt-and-suspenders polling for filesystem reliabilityparseDuration()andresolveSince(): Parse duration shorthand (30s, 5m, 2h, 1d, 1w) and ISO timestamps for the--sinceoptionNew logs command (
src/cli/commands/logs.ts):mcpc @<session> logswith options:-n/--tail <n>: Show last N lines (default: 100)--follow: Stream new lines as they arrive (Ctrl+C to stop)--since <value>: Filter by duration or ISO timestamp--followIntegration:
Notable Implementation Details
--sincefollowLog()function uses bothfs.watch()and a 1-second polling interval for robustness across different filesystems (NFS, network mounts)https://claude.ai/code/session_01C8o7VrBnDQfQ3QywPe5tMm