Summary
Add a --log-file flag that writes all request logs to a file in addition to (or instead of) stderr. This is useful for auditing token usage over time or debugging issues after the fact.
Proposed behaviour
tokenproxy start --max-tokens-per-request 4096 --log-file proxy.log
Each log line should be a JSON object for easy parsing:
{"ts": "2026-04-28T14:32:01", "level": "INFO", "path": "/v1/chat/completions", "prompt_tokens": 312, "max_output_tokens": 512, "blocked": false}
{"ts": "2026-04-28T14:32:05", "level": "BLOCK", "path": "/v1/chat/completions", "prompt_tokens": 4200, "max_output_tokens": 1024, "blocked": true, "reason": "exceeds per-request limit"}
Implementation hints
- Add
--log-file argument to the start subparser in cli.py
- Pass the file path to
ProxyServer and open it in append mode
- In
_log(), write to both stderr and the log file when a path is configured
- Use
threading.Lock to make file writes thread-safe
Acceptance criteria
Summary
Add a
--log-fileflag that writes all request logs to a file in addition to (or instead of) stderr. This is useful for auditing token usage over time or debugging issues after the fact.Proposed behaviour
Each log line should be a JSON object for easy parsing:
{"ts": "2026-04-28T14:32:01", "level": "INFO", "path": "/v1/chat/completions", "prompt_tokens": 312, "max_output_tokens": 512, "blocked": false} {"ts": "2026-04-28T14:32:05", "level": "BLOCK", "path": "/v1/chat/completions", "prompt_tokens": 4200, "max_output_tokens": 1024, "blocked": true, "reason": "exceeds per-request limit"}Implementation hints
--log-fileargument to thestartsubparser incli.pyProxyServerand open it in append mode_log(), write to both stderr and the log file when a path is configuredthreading.Lockto make file writes thread-safeAcceptance criteria
--log-file proxy.logcreates the file and writes JSON log lines--log-fileis used