-
Notifications
You must be signed in to change notification settings - Fork 20
[duplicate-code] Duplicate Code Pattern: Repeated logger-initialisation error blocks in cmd/root.go and cmd/proxy.go #3311
Description
Part of duplicate code analysis: #3309
Summary
internal/cmd/root.go initialises five loggers using an identical 3-line pattern (if err := logger.InitXxx(...); err != nil { log.Printf("Warning: ...") }). The same structure (abbreviated to two loggers) is also repeated in internal/cmd/proxy.go. This is a classic copy-paste initialisation block that could be extracted into a single helper function.
Duplication Details
Pattern: Repeated logger-initialisation error blocks
- Severity: Medium
- Occurrences: 5 in
root.go+ 2 inproxy.go= 7 blocks (≈21 lines) - Locations:
internal/cmd/root.go(lines 129–151)internal/cmd/proxy.go(lines 144–149)
- Code Sample (
root.golines 129–151):
if err := logger.InitFileLogger(logDir, "mcp-gateway.log"); err != nil {
log.Printf("Warning: Failed to initialize file logger: %v", err)
}
if err := logger.InitServerFileLogger(logDir); err != nil {
log.Printf("Warning: Failed to initialize server file logger: %v", err)
}
if err := logger.InitMarkdownLogger(logDir, "gateway.md"); err != nil {
log.Printf("Warning: Failed to initialize markdown logger: %v", err)
}
if err := logger.InitJSONLLogger(logDir, "rpc-messages.jsonl"); err != nil {
log.Printf("Warning: Failed to initialize JSONL logger: %v", err)
}
if err := logger.InitToolsLogger(logDir, "tools.json"); err != nil {
log.Printf("Warning: Failed to initialize tools logger: %v", err)
}- Code Sample (
proxy.golines 144–149):
if err := logger.InitFileLogger(proxyLogDir, "proxy.log"); err != nil {
log.Printf("Warning: Failed to initialize file logger: %v", err)
}
if err := logger.InitJSONLLogger(proxyLogDir, "rpc-messages.jsonl"); err != nil {
log.Printf("Warning: Failed to initialize JSONL logger: %v", err)
}Impact Analysis
- Maintainability: Adding a new logger type requires editing every callsite; currently two files.
- Bug Risk: If the warning format or error handling changes, all sites must be updated.
- Code Bloat: 21 lines reducible to ~2 calls.
Refactoring Recommendations
-
Add an
InitGatewayLoggers(logDir string)helper ininternal/logger/that initialises the standard set of gateway loggers and returns a combined error or logs warnings internally:// internal/logger/init.go func InitGatewayLoggers(logDir string) { initWithWarning(InitFileLogger(logDir, "mcp-gateway.log"), "file logger") initWithWarning(InitServerFileLogger(logDir), "server file logger") initWithWarning(InitMarkdownLogger(logDir, "gateway.md"), "markdown logger") initWithWarning(InitJSONLLogger(logDir, "rpc-messages.jsonl"), "JSONL logger") initWithWarning(InitToolsLogger(logDir, "tools.json"), "tools logger") } func initWithWarning(err error, name string) { if err != nil { log.Printf("Warning: Failed to initialize %s: %v", name, err) } }
-
root.gobecomes:logger.InitGatewayLoggers(logDir) -
proxy.gocan call a lighterInitProxyLoggers(logDir string)variant.Estimated effort: ~1 hour. Very low regression risk.
Implementation Checklist
- Create
internal/logger/init.gowithInitGatewayLoggersandInitProxyLoggers - Update
internal/cmd/root.go(lines 129–151) to callInitGatewayLoggers - Update
internal/cmd/proxy.go(lines 144–149) to callInitProxyLoggers - Verify all loggers still initialise correctly via
make test-integration - Run
make agent-finished
Parent Issue
See parent analysis report: #3309
Related to #3309
Generated by Duplicate Code Detector · ● 2.2M · ◷
- expires on Apr 14, 2026, 6:05 AM UTC