Skip to content

feat: add --log-file flag to write JSON request logs to a file #3

Description

@jishanahmed-shaikh

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

  • --log-file proxy.log creates the file and writes JSON log lines
  • Log file is appended to (not overwritten) on restart
  • Stderr output is unchanged when --log-file is used
  • File writes are thread-safe

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions