Skip to content

[duplicate-code] Multi-Level Logging Calls DuplicationΒ #931

@github-actions

Description

@github-actions

πŸ” Duplicate Code Pattern: Multi-Level Logging Calls

Part of duplicate code analysis: #929

Summary

The internal/launcher/log_helpers.go file exhibits a repetitive triple-logging pattern where every logging operation calls three different loggers in sequence: logger.LogInfoWithServer() (file logger), log.Printf() (stdout), and logLauncher.Printf() (debug logger). This pattern appears 6+ times across different functions.

Duplication Details

Pattern: Triple Logging Calls

  • Severity: Medium

  • Occurrences: 6+ instances across 6 functions

  • Locations:

    • internal/launcher/log_helpers.go lines 23-27 (logSecurityWarning)
    • internal/launcher/log_helpers.go lines 33-35, 37-40 (logLaunchStart - 2x)
    • internal/launcher/log_helpers.go lines 69-75 (logLaunchError)
    • internal/launcher/log_helpers.go lines 96-105 (logTimeoutError - 2x)
    • internal/launcher/log_helpers.go lines 111-117 (logLaunchSuccess - 2x)
  • Code Sample:

    // Pattern repeated throughout:
    logger.LogInfoWithServer(serverID, "backend", "Launching MCP backend server: %s, command=%s, args=%v", serverID, serverCfg.Command, sanitize.SanitizeArgs(serverCfg.Args))
    log.Printf("[LAUNCHER] Starting MCP server: %s", serverID)
    logLauncher.Printf("Launching new server: serverID=%s, command=%s, inContainer=%v, isDirectCommand=%v", ...)
  • Another Example:

    logger.LogErrorWithServer(serverID, "backend", "Failed to launch MCP backend server%s: server=%s%s, error=%v", ...)
    log.Printf("[LAUNCHER] ❌ FAILED to launch server '%s'%s", serverID, sessionSuffix(sessionID))
    log.Printf("[LAUNCHER] Error: %v", err)

Impact Analysis

  • Maintainability: Medium impact - Adding/removing a logging level requires changes in 6+ locations
  • Bug Risk: Low-Medium - Inconsistent logging if one call is modified but others aren't
  • Code Bloat: ~30-35 lines of repetitive logging invocations
  • Readability: Reduces clarity - obscures the actual logic with verbose logging calls

Refactoring Recommendations

Option 1: Unified Launcher Logger Wrapper (Recommended)

  • Extract common pattern to: internal/launcher/launcher_logger.go
  • Estimated effort: 3-4 hours
  • Benefits:
    • Single interface for all launcher logging
    • Easier to add/remove logging levels
    • Consistent formatting across all log destinations
    • Better testability (mock single logger instead of three)
  • Implementation:
    type LauncherLogger struct {
        launcher *Launcher
    }
    
    func (ll *LauncherLogger) LogStart(serverID, sessionID string, cfg *config.ServerConfig, isDirect bool) {
        // Encapsulates triple logging with proper formatting
        logger.LogInfoWithServer(serverID, "backend", "Launching MCP backend server: ...")
        log.Printf("[LAUNCHER] Starting MCP server: %s", serverID)
        logLauncher.Printf("Launching new server: serverID=%s", serverID)
    }
    
    func (ll *LauncherLogger) LogError(serverID, sessionID string, err error, cfg *config.ServerConfig, isDirect bool) {
        // Encapsulates error logging with diagnostics
        logger.LogErrorWithServer(serverID, "backend", "Failed to launch: ...")
        log.Printf("[LAUNCHER] ❌ FAILED to launch server '%s'", serverID)
        logLauncher.Printf("Launch failed: serverID=%s, error=%v", serverID, err)
    }

Option 2: Structured Logging Event System

  • Use a single logging call with metadata that fans out to destinations
  • Estimated effort: 6-8 hours (requires broader refactoring)
  • Benefits: More flexible, supports dynamic log routing
  • Trade-off: Larger scope, affects more of the codebase

Option 3: Logging Middleware/Interceptor

  • Create a logging interceptor that handles multi-destination logging
  • Estimated effort: 4-5 hours
  • Benefits: Centralized logging control, easier to modify behavior
  • Trade-off: Adds abstraction layer

Implementation Checklist

  • Review duplication findings
  • Decide on refactoring approach (Option 1 recommended)
  • Design LauncherLogger interface
  • Implement wrapper methods for each logging scenario
  • Refactor all 6 functions to use new logger
  • Update tests to verify behavior
  • Consider extending to other launcher modules
  • Document logging patterns in AGENTS.md

Parent Issue

See parent analysis report: #929
Related to #929

AI generated by Duplicate Code Detector

  • expires on Feb 21, 2026, 10:12 AM UTC

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions