feat: add Zstandard (.json.zst) compression for saved logs and full completion text persistence

This commit is contained in:
tyuXX 2026-11-05 20:34:47 +00:00
parent 9d44c92b01
commit 792f8a1160
10 changed files with 886 additions and 43 deletions

View file

@ -242,15 +242,39 @@ List available models
#### `GET /health`
Health check endpoint with key rotation statistics
## Logging
## Logging & Automatic Zstandard Compression
All requests and responses are logged to JSON files in the configured `LOG_DIR`. Log files are named by date: `api-logs-YYYY-MM-DD.json`
All requests and responses (including full prompts and assistant completion text) are logged to newline-delimited JSON files in the configured `LOG_DIR`.
- **Active Today's Log:** Kept uncompressed at `api-logs-YYYY-MM-DD.json` for fast real-time appends.
- **Historical Logs (`.json.zst`):** On server startup and upon daily UTC date rollover, any previous days' `api-logs-YYYY-MM-DD.json` files are automatically compressed using **Zstandard** (default level `19`) into `api-logs-YYYY-MM-DD.json.zst` and the uncompressed `.json` file is removed. If a `.json.zst` archive already exists for that date, new entries are losslessly merged and deduplicated before re-compressing.
- **Stats Rebuild Compatibility:** `StatsTracker` transparently reads both `.json` and `.json.zst` files when reconstructing historical statistics.
Each log entry contains:
- timestamp
- request ID
- type (request/response/error)
- full request/response data
- `timestamp`
- `requestId`
- `type` (`request` / `response` / `error`)
- `data` (full request payload, assistant completion text, finish reason, and token usage)
### Inspecting & Decompressing Saved Logs
Use the built-in CLI inspector (`npm run logs`) to view or extract compressed logs:
```bash
# List all saved log files (.json and .json.zst) with sizes, compression ratios, and entry counts
npm run logs
# Print all entries for a specific date or filename (automatically decompresses .zst in memory)
npm run logs -- 2026-11-04
npm run logs -- 2026-11-04 --pretty
npm run logs -- 2026-11-04 --search "model-name"
# Extract a .json.zst archive back to a .json file on disk
npm run logs -- --decompress 2026-11-04
# Manually trigger compression of previous days' logs
npm run logs -- --compress
```
## Key Rotation Strategy
@ -269,6 +293,8 @@ The system uses a round-robin rotation strategy with rate limit awareness:
| `GEMINI_API_KEYS` | Comma-separated list of Gemini API keys | (required) |
| `PORT` | Server port | 3000 |
| `LOG_DIR` | Directory for log files | ./logs |
| `ENABLE_LOG_COMPRESSION` | Automatically compress past days' logs to `.json.zst` | `true` |
| `LOG_COMPRESSION_LEVEL` | Zstandard compression level (`1`–`22`) | `19` |
| `CLOUDFLARE_TUNNEL_TOKEN` | Cloudflare tunnel token (Docker only) | (required for Docker) |
| `CLOUDFLARE_DOMAIN` | Domain for Cloudflare tunnel (Docker only) | (required for Docker) |