Skip to content

[duplicate-code] Duplicate Code Pattern: Repeated logger-initialisation error blocks in cmd/root.go and cmd/proxy.go #3311

@github-actions

Description

@github-actions

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

  1. 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)
        }
    }
  2. root.go becomes: logger.InitGatewayLoggers(logDir)

  3. proxy.go can call a lighter InitProxyLoggers(logDir string) variant.

    Estimated effort: ~1 hour. Very low regression risk.

Implementation Checklist

  • Create internal/logger/init.go with InitGatewayLoggers and InitProxyLoggers
  • Update internal/cmd/root.go (lines 129–151) to call InitGatewayLoggers
  • Update internal/cmd/proxy.go (lines 144–149) to call InitProxyLoggers
  • 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions