Skip to content

[duplicate-code] Duplicate Code Pattern: Mixed log.Printf / logger.LogInfo Parallel Logging in connection.go #3633

@github-actions

Description

@github-actions

Part of duplicate code analysis: #3631

Summary

Three transport-connection success paths in internal/mcp/connection.go each emit two log entries for the same event: one via the structured logger.LogInfo pipeline (which writes to mcp-gateway.log) and a second via the standard log.Printf (which writes only to stderr). This creates redundant log noise and partial bypass of the structured logging pipeline — the problem the most recent commit (a82e67c) addressed for SSE deprecation warnings but did not fully resolve for the other transport paths.

Duplication Details

Pattern: logger.LogInfo + log.Printf paired for the same operational event

  • Severity: Medium
  • Occurrences: 3 paired call sites + 2 additional solo log.Printf calls
  • Locations:
    • internal/mcp/connection.go lines 240–241 (streamable HTTP success)
    • internal/mcp/connection.go lines 263–264 (plain JSON-RPC success)
    • internal/mcp/connection.go lines 143–144 (stdio server start)
    • internal/mcp/connection.go lines 148 and 178 (solo log.Printf with no structured counterpart)

Code Sample — duplicate pair at lines 240–241:

logger.LogInfo("backend", "Successfully connected using streamable HTTP transport, url=%s", url)
log.Printf("Configured HTTP MCP server with streamable transport: %s", url)   // ← redundant

Code Sample — duplicate pair at lines 263–264:

logger.LogInfo("backend", "Successfully connected using plain JSON-RPC transport, url=%s", url)
log.Printf("Configured HTTP MCP server with plain JSON-RPC transport: %s", url) // ← redundant

Code Sample — duplicate pair at lines 143–144:

logger.LogInfo("backend", "Starting MCP backend server, command=%s, args=%v", command, sanitize.SanitizeArgs(expandedArgs))
log.Printf("Starting MCP server command: %s %v", command, sanitize.SanitizeArgs(expandedArgs)) // ← redundant

Code Sample — solo log.Printf (no structured counterpart) at lines 148, 178:

log.Printf("Connecting to MCP server...")           // line 148
log.Printf("Started MCP server: %s %v", ...)       // line 178

Impact Analysis

  • Maintainability: Messages that appear in two formats (structured log file vs. raw stderr) diverge over time — the log.Printf version already uses slightly different wording than the logger.LogInfo version in all three pairs.
  • Bug Risk: Operators relying on mcp-gateway.log for monitoring miss events logged only via log.Printf; operators watching stderr receive duplicate lines.
  • Code Bloat: 5 extra log.Printf lines; also requires the "log" standard-library import in connection.go to remain.

Refactoring Recommendations

  1. Remove the redundant log.Printf calls (preferred)

    • Delete lines 144, 148, 178, 241, 264 in connection.go.
    • Convert the solo log.Printf at line 148 ("Connecting to MCP server...") and line 178 to logger.LogInfo or logConn.Printf (the debug-gated logger already used in the same function).
    • Estimated effort: 15 minutes
    • Benefits: consistent structured logging; "log" import can be removed from connection.go entirely (it is only used for these five calls in the non-test code)
  2. Replace remaining log.Printf calls with logConn.Printf for purely diagnostic messages (lines 148)

    • These are debug-level traces that are better gated by the DEBUG env var than always-on stderr.

Implementation Checklist

  • Remove redundant log.Printf at lines 144, 241, 264 (have structured counterpart)
  • Replace log.Printf at lines 148, 178 with logConn.Printf or logger.LogInfo
  • Remove "log" from imports in connection.go if no remaining uses
  • Run make lint to verify no unused imports
  • Run make test to verify no regressions

Parent Issue

See parent analysis report: #3631
Related to #3631

Generated by Duplicate Code Detector · ● 1.3M ·

  • expires on Apr 19, 2026, 6:07 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