Skip to content

[duplicate-code] Duplicate Code Pattern: Session-Conditional Logging Branches in log_helpers.go #1812

@github-actions

Description

@github-actions

Part of duplicate code analysis: #1810

Summary

Four functions in internal/launcher/log_helpers.go repeat an identical if sessionID != "" { ... } else { ... } branching pattern for logging. A sessionSuffix() helper already exists but is only partially used — three of the four functions still manually fork every log statement.

Duplication Details

Pattern: if sessionID != "" log branches

  • Severity: Medium

  • Occurrences: 4 functions (logLaunchStart, logTimeoutError, logLaunchSuccess, logLaunchError)

  • Locations:

    • internal/launcher/log_helpers.go lines 31–47 (logLaunchStart)
    • internal/launcher/log_helpers.go lines 97–113 (logTimeoutError)
    • internal/launcher/log_helpers.go lines 116–126 (logLaunchSuccess)
    • internal/launcher/log_helpers.go lines 70–95 (logLaunchError — uses sessionSuffix() for logger.* calls but still branches for log.Printf and logLauncher.Printf)
  • Code Sample:

// logTimeoutError (lines 101–112) — exact if/else on sessionID for log.Printf and logLauncher
if sessionID != "" {
    log.Printf("[LAUNCHER] ❌ Server '%s' (session: %s) startup timed out after %v", serverID, sessionID, l.startupTimeout)
} else {
    log.Printf("[LAUNCHER] ❌ Server '%s' startup timed out after %v", serverID, l.startupTimeout)
}
// ...
if sessionID != "" {
    logLauncher.Printf("Startup timeout occurred: serverID=%s, sessionID=%s, timeout=%v", serverID, sessionID, l.startupTimeout)
} else {
    logLauncher.Printf("Startup timeout occurred: serverID=%s, timeout=%v", serverID, l.startupTimeout)
}

// logLaunchSuccess (lines 117–125) — same pattern again
if sessionID != "" {
    logger.LogInfoWithServer(serverID, "backend", "Successfully launched MCP backend server for session: server=%s, session=%s", serverID, sessionID)
    log.Printf("[LAUNCHER] Successfully launched: %s (session: %s)", serverID, sessionID)
    logLauncher.Printf("Session connection established: serverID=%s, sessionID=%s", serverID, sessionID)
} else {
    logger.LogInfoWithServer(serverID, "backend", "Successfully launched MCP backend server: %s", serverID)
    log.Printf("[LAUNCHER] Successfully launched: %s", serverID)
    logLauncher.Printf("Connection established: serverID=%s", serverID)
}

Impact Analysis

  • Maintainability: Every change to the session-aware log format must be applied in 4 places (8+ individual if/else branches). The existing sessionSuffix() helper shows the right intent but is not consistently applied to all three log channels (logger.*, log.Printf, logLauncher.Printf).
  • Bug Risk: Inconsistent format strings between session and non-session branches (e.g., missing session ID in one logger while present in another) could produce confusing log output.
  • Code Bloat: ~35 extra lines of branching that could collapse into simple format-string composition.

Refactoring Recommendations

  1. Extend sessionSuffix usage consistently — use it in all three log channels uniformly:

    func (l *Launcher) logLaunchSuccess(serverID, sessionID string) {
        suffix := sessionSuffix(sessionID)
        logger.LogInfoWithServer(serverID, "backend",
            "Successfully launched MCP backend server%s: server=%s%s", suffix, serverID, suffix)
        log.Printf("[LAUNCHER] Successfully launched: %s%s", serverID, suffix)
        logLauncher.Printf("Connection established: serverID=%s%s", serverID, suffix)
    }
  2. Consider a sessionKeyvals helper returning []any for structured loggers:

    func sessionKeyvals(serverID, sessionID string) string {
        if sessionID == "" {
            return fmt.Sprintf("serverID=%s", serverID)
        }
        return fmt.Sprintf("serverID=%s, sessionID=%s", serverID, sessionID)
    }

Implementation Checklist

  • Review all if sessionID != "" branches in log_helpers.go
  • Extend sessionSuffix() or introduce a second helper for consistent key-value formatting
  • Collapse branches in logLaunchStart, logTimeoutError, logLaunchSuccess, and logLaunchError
  • Run make test-unit to confirm no behaviour change
  • Verify log output format still matches existing tests

Parent Issue

See parent analysis report: #1810
Related to #1810

Generated by Duplicate Code Detector ·

  • expires on Mar 19, 2026, 3:00 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