Skip to content

refactor: deduplicate code across logger, validation, and launcher packages#1926

Merged
lpcox merged 1 commit intomainfrom
fix/duplicate-code-batch
Mar 14, 2026
Merged

refactor: deduplicate code across logger, validation, and launcher packages#1926
lpcox merged 1 commit intomainfrom
fix/duplicate-code-batch

Conversation

@lpcox
Copy link
Collaborator

@lpcox lpcox commented Mar 14, 2026

Summary

Addresses all 15 open duplicate-code issues by implementing 4 targeted refactorings and closing issues where patterns were already resolved.

Changes

1. formatLogLine helper (logger/common.go)

Centralizes the [timestamp] [level] [category] message log line format used identically by FileLogger.Log() and ServerFileLogger.Log().

2. Session-conditional logging (launcher/log_helpers.go)

Consolidates if sessionID != "" branches in logLaunchStart, logTimeoutError, and logLaunchSuccess using the existing sessionSuffix() helper consistently.

3. InvalidPattern + InvalidValue helpers (config/rules/rules.go)

New reusable helpers for pattern validation and constraint violations. Applied to 5 inline ValidationError constructions in validation_schema.go (container, mount, URL, entrypoint, domain).

4. SchemaValidationError helper (config/rules/rules.go)

New helper for custom schema validation failures. Applied to 8 inline constructions in validation.go with consistent message format.

Already resolved patterns (close only)

Closes

#1882, #1824, #1810, #1766, #1768, #1885, #1811, #1825, #1767, #1883, #1826, #1812, #1884, #1813, #1827

…ckages

Four targeted duplicate code reductions:

1. formatLogLine helper (logger/common.go)
   - Centralizes [timestamp] [level] [category] message formatting
   - Used by FileLogger.Log() and ServerFileLogger.Log()
   - Eliminates identical 3-line blocks in both loggers

2. Session-conditional logging (launcher/log_helpers.go)
   - Consolidates if/else branches in logLaunchStart, logTimeoutError,
     logLaunchSuccess using existing sessionSuffix() helper
   - Reduces 3 functions from ~30 lines to ~18 lines

3. InvalidPattern + InvalidValue helpers (config/rules/rules.go)
   - New helpers for pattern matching and constraint validation errors
   - Applied in validation_schema.go: container, mount, URL, domain checks
   - Reduces 5 inline ValidationError constructions to one-liners

4. SchemaValidationError helper (config/rules/rules.go)
   - New helper for custom schema validation failures (Field: type)
   - Applied in validation.go: 8 inline constructions consolidated
   - Consistent message format: '<detail> for server type <type>'

Total: ~13 lines saved, 3 new reusable helpers added.

Closes #1882, #1824, #1810, #1766, #1768, #1885, #1811, #1825,
#1767, #1883, #1826, #1812, #1884, #1813, #1827

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 14, 2026 23:53
@lpcox lpcox merged commit 5e253d5 into main Mar 14, 2026
14 checks passed
@lpcox lpcox deleted the fix/duplicate-code-batch branch March 14, 2026 23:55
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors several internal packages to remove repeated boilerplate by introducing small, reusable helpers (logger line formatting, launcher session-aware logging formatting, and config validation error constructors) to keep behavior consistent and reduce future churn.

Changes:

  • Centralize file-based logger line formatting via a shared helper used by both FileLogger and ServerFileLogger.
  • Reduce session-conditional branching in launcher logging helpers by composing log strings with sessionSuffix().
  • Introduce reusable ValidationError constructors (InvalidPattern, InvalidValue, SchemaValidationError) and apply them across schema/pattern validation codepaths.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
internal/logger/common.go Adds shared formatLogLine helper to standardize file log output format.
internal/logger/file_logger.go Switches FileLogger.Log() to use formatLogLine (removes duplicated formatting code).
internal/logger/server_file_logger.go Switches ServerFileLogger.Log() to use formatLogLine (removes duplicated formatting code).
internal/launcher/log_helpers.go Refactors launch/timeout/success logging to reduce sessionID != "" branching via sessionSuffix().
internal/config/rules/rules.go Adds reusable ValidationError helper constructors for patterns/values/schema validation.
internal/config/validation_schema.go Replaces inline ValidationError literals with InvalidPattern / InvalidValue.
internal/config/validation.go Replaces inline ValidationError literals with SchemaValidationError / InvalidValue.
Comments suppressed due to low confidence (2)

internal/launcher/log_helpers.go:105

  • The logLauncher.Printf message switched from explicitly logging sessionID=%s to appending sessionSuffix into serverID=%s%s. Since logLauncher also forwards to the file logger (LogDebug), dropping a dedicated sessionID= field makes filtering/grep’ing for a session harder and changes the log semantics. Prefer keeping serverID=%s and adding , sessionID=%s when sessionID != "" (possibly via a helper) rather than reusing the human-readable suffix.
	log.Printf("[LAUNCHER] ⚠️  The server may be hanging or taking too long to initialize")
	log.Printf("[LAUNCHER] ⚠️  Consider increasing 'startupTimeout' in gateway config if server needs more time")
	logLauncher.Printf("Startup timeout occurred: serverID=%s%s, timeout=%v", serverID, suffix, l.startupTimeout)

internal/launcher/log_helpers.go:113

  • Same issue as timeout logging: logLauncher.Printf("Connection established: serverID=%s%s", ...) no longer logs sessionID as a distinct key/value when a session is present. Because these messages are also written to the file logger via Logger.PrintfLogDebug, keeping sessionID=%s explicitly is more useful for log searching and consistency with other launcher debug messages.
	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)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +33 to +34
logger.LogInfoWithServer(serverID, "backend", "Launching MCP backend server%s: server=%s%s, command=%s, args=%v",
suffix, serverID, suffix, serverCfg.Command, sanitize.SanitizeArgs(serverCfg.Args))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants