Skip to content

[duplicate-code] Duplicate Code Pattern: Dual stdout/file logging pairs in cmd/root.go #3310

@github-actions

Description

@github-actions

Part of duplicate code analysis: #3309

Summary

internal/cmd/root.go contains ~10 consecutive pairs where the same startup event is logged twice — once to stdout via log.Printf and again to the structured logger via logger.LogInfoMd / logger.LogWarn. This pattern spans approximately 20 lines and introduces a maintenance burden: any message change must be applied in two places, creating a risk of divergence.

Duplication Details

Pattern: log.Printf + logger.LogXxxMd pairs for identical startup messages

  • Severity: Medium
  • Occurrences: ~10 pairs (≈20 affected lines)
  • Location: internal/cmd/root.go (lines 236–412)
  • Code Sample:
// Pattern repeated ~10 times:
log.Printf("Guard policy override configured (source=%s)", policySource)
logger.LogInfoMd("startup", "Guard policy override configured (source=%s)", policySource)

log.Printf("MCP_GATEWAY_GUARDS_SINK_SERVER_IDS=%q", envSinkServerIDs)
logger.LogInfoMd("startup", "MCP_GATEWAY_GUARDS_SINK_SERVER_IDS=%q", envSinkServerIDs)

log.Printf("Guards sink server IDs configured for JSONL tag enrichment: %v", resolvedSinkServerIDs)
logger.LogInfoMd("startup", "Guards sink server IDs configured for JSONL tag enrichment: %v", resolvedSinkServerIDs)

log.Printf("Warning: guards sink server ID '%s' is not configured in mcpServers", sinkServerID)
logger.LogWarn("startup", "Guards sink server ID '%s' is not configured in mcpServers", sinkServerID)

log.Printf("Warning: failed to initialize tracing provider: %v", err)
logger.LogWarn("startup", "Failed to initialize tracing provider: %v", err)

log.Printf("Starting MCPG in ROUTED mode on %s", listenAddr)
logger.LogInfoMd("startup", "Starting in ROUTED mode on %s", listenAddr)

Impact Analysis

  • Maintainability: Every message needs to be edited in two places; wording frequently diverges (e.g. log.Printf("Warning: ...") vs logger.LogWarn(...)).
  • Bug Risk: Diverged messages make log correlation harder during incident investigation.
  • Code Bloat: ~20 lines that could be ~10.

Refactoring Recommendations

  1. Extend logger.LogInfoMd / logger.LogWarnMd to also echo to stdout (or add a tee mode) so callers need only one call. The MarkdownLogger already writes to both file and markdown — adding a stdout tee for startup messages would eliminate the pair.

  2. Alternatively, wrap the pair in a helper such as:

    // internal/logger/startup.go
    func StartupInfo(format string, args ...interface{}) {
        log.Printf(format, args...)
        LogInfoMd("startup", format, args...)
    }

    Estimated effort: 1–2 hours. Callers in root.go become single-line.

  3. At minimum, remove the redundant log.Printf calls where logger.LogInfoMd already writes a sufficiently descriptive message, since the file logger also writes to stdout via its fallback path.

Implementation Checklist

  • Decide on approach (tee-mode vs startup helper vs remove redundant calls)
  • Implement chosen approach in internal/logger/
  • Update internal/cmd/root.go to use the new helper (~10 sites)
  • Verify no startup messages are lost in tests
  • 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