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 in proxy.go = 7 blocks (≈21 lines)
- Locations:
internal/cmd/root.go (lines 129–151)
internal/cmd/proxy.go (lines 144–149)
- Code Sample (
root.go lines 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.go lines 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 in internal/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.go becomes: logger.InitGatewayLoggers(logDir)
-
proxy.go can call a lighter InitProxyLoggers(logDir string) variant.
Estimated effort: ~1 hour. Very low regression risk.
Implementation Checklist
Parent Issue
See parent analysis report: #3309
Related to #3309
Generated by Duplicate Code Detector · ● 2.2M · ◷
Part of duplicate code analysis: #3309
Summary
internal/cmd/root.goinitialises 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 ininternal/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
root.go+ 2 inproxy.go= 7 blocks (≈21 lines)internal/cmd/root.go(lines 129–151)internal/cmd/proxy.go(lines 144–149)root.golines 129–151):proxy.golines 144–149):Impact Analysis
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:root.gobecomes:logger.InitGatewayLoggers(logDir)proxy.gocan call a lighterInitProxyLoggers(logDir string)variant.Estimated effort: ~1 hour. Very low regression risk.
Implementation Checklist
internal/logger/init.gowithInitGatewayLoggersandInitProxyLoggersinternal/cmd/root.go(lines 129–151) to callInitGatewayLoggersinternal/cmd/proxy.go(lines 144–149) to callInitProxyLoggersmake test-integrationmake agent-finishedParent Issue
See parent analysis report: #3309
Related to #3309