You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add per-serverID log files for easier backend troubleshooting (#724)
## Per-ServerID Logging Implementation ✅
This PR implements per-serverID log files to make it easier to
troubleshoot individual MCP servers.
### Completed Tasks
- [x] Explore the repository and understand current logging
infrastructure
- [x] Design per-serverID logging architecture
- [x] Add new `ServerFileLogger` that manages per-serverID log files
- [x] Create log files named `{serverID}.log` in the log directory
- [x] Add helper functions to log with serverID context
- [x] Implement per-serverID file logger
- [x] Create `ServerFileLogger` struct with map of serverID -> file
logger
- [x] Add `LogWithServer` functions that route to per-serverID files
- [x] Handle concurrent access with proper locking
- [x] Update existing log calls to use per-serverID logging
- [x] Update backend-related log calls with serverID context
- [x] Updated launcher package to use per-serverID logging
- [x] Updated connection.go stderr logging
- [x] Updated unified server tool registration logging
- [x] Keep gateway-wide logs in `mcp-gateway.log`
- [x] Add tests for per-serverID logging
- [x] Test log file creation per serverID
- [x] Test concurrent logging to different serverID files
- [x] Test all log levels
- [x] Test fallback mode
- [x] Test multiple initialization
- [x] Test that unified log is preserved ✨
- [x] Documentation
- [x] Update logger README with per-serverID logging details
- [x] Update main README to highlight the feature
- [x] Update AGENTS.md with logging details
- [x] Document log directory structure
- [x] Verify changes
- [x] Build successful
- [x] All unit tests passing
- [x] Linter passed ✅ (fixed gofmt issue)
- [x] Code review completed - addressed feedback
- [x] Security check (CodeQL) - No vulnerabilities found
### Implementation Summary
**Key Feature - Dual Logging:**
✅ Each per-serverID log function writes to BOTH:
1. Server-specific log file (e.g., `github.log`) - isolated messages
2. Unified `mcp-gateway.log` - all messages with `[serverID]` prefix
This ensures **single-view files are preserved** while adding
per-serverID isolation.
**Log Directory Structure:**
```
/tmp/gh-aw/mcp-logs/
├── mcp-gateway.log # ALL messages (single-view) ✅
├── github.log # Only GitHub server logs
├── slack.log # Only Slack server logs
├── notion.log # Only Notion server logs
├── gateway.md # Markdown format
└── rpc-messages.jsonl # RPC messages
```
**API Usage:**
```go
// Writes to both {serverID}.log AND mcp-gateway.log
logger.LogInfoWithServer("github", "backend", "Server started")
logger.LogWarnWithServer("slack", "backend", "Connection timeout")
```
**Testing:**
- 8 comprehensive test cases including test for unified log preservation
- All existing tests continue to pass
- Verified single-view and per-serverID logs work correctly
- CodeQL security check passed with no vulnerabilities
- All linting checks pass ✅
<!-- START COPILOT CODING AGENT TIPS -->
---
✨ Let Copilot coding agent [set things up for
you](https://github.com/github/gh-aw-mcpg/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot)
— coding agent works faster and does higher quality work when set up for
your repo.
MCPG provides comprehensive logging of all gateway operations to help diagnose issues and monitor activity.
331
336
337
+
### Log Files
338
+
339
+
The gateway creates multiple log files for different purposes:
340
+
341
+
1.**`mcp-gateway.log`** - Unified log with all gateway messages
342
+
2.**`{serverID}.log`** - Per-server logs (e.g., `github.log`, `slack.log`) for easier troubleshooting of specific backend servers
343
+
3.**`gateway.md`** - Markdown-formatted logs for GitHub workflow previews
344
+
4.**`rpc-messages.jsonl`** - Machine-readable JSONL format for RPC message analysis
345
+
332
346
### Log File Location
333
347
334
-
By default, logs are written to `/tmp/gh-aw/mcp-logs/mcp-gateway.log`. This location can be configured using either:
348
+
By default, logs are written to `/tmp/gh-aw/mcp-logs/`. This location can be configured using either:
335
349
336
350
1.**`MCP_GATEWAY_LOG_DIR` environment variable** - Sets the default log directory
337
351
2.**`--log-dir` flag** - Overrides the environment variable and default
338
352
339
353
The precedence order is: `--log-dir` flag → `MCP_GATEWAY_LOG_DIR` env var → default (`/tmp/gh-aw/mcp-logs`)
340
354
355
+
### Per-ServerID Logs
356
+
357
+
Each backend MCP server gets its own log file (e.g., `github.log`, `slack.log`) in addition to the unified `mcp-gateway.log`. This makes it much easier to:
358
+
359
+
- Debug issues with a specific backend server
360
+
- View all activity for one server without filtering
Copy file name to clipboardExpand all lines: internal/logger/README.md
+55Lines changed: 55 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,61 @@ A simple, debug-style logging framework for Go that follows the pattern matching
11
11
-**Automatic color coding**: Each namespace gets a unique color when stderr is a TTY
12
12
-**Zero overhead**: Logger enabled state is computed once at construction time
13
13
-**Thread-safe**: Safe for concurrent use
14
+
-**Per-ServerID Logs**: Separate log files for each backend MCP server for easier troubleshooting
15
+
16
+
## Per-ServerID Logging
17
+
18
+
The logger package supports creating separate log files for each backend MCP server (identified by serverID). This makes it much easier to troubleshoot specific servers without sifting through unified logs.
19
+
20
+
### How It Works
21
+
22
+
- Each serverID gets its own log file: `{serverID}.log` in the log directory
23
+
- Logs are also written to the main `mcp-gateway.log` for a unified view
24
+
- Concurrent writes to different serverID logs are handled safely
25
+
- Fallback to unified logging if per-serverID logging cannot be initialized
0 commit comments