refactor: deduplicate code across logger, validation, and launcher packages#1926
Merged
refactor: deduplicate code across logger, validation, and launcher packages#1926
Conversation
…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>
This was referenced Mar 14, 2026
This was referenced Mar 14, 2026
Closed
Contributor
There was a problem hiding this comment.
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
FileLoggerandServerFileLogger. - Reduce session-conditional branching in launcher logging helpers by composing log strings with
sessionSuffix(). - Introduce reusable
ValidationErrorconstructors (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.Printfmessage switched from explicitly loggingsessionID=%sto appendingsessionSuffixintoserverID=%s%s. SincelogLauncheralso forwards to the file logger (LogDebug), dropping a dedicatedsessionID=field makes filtering/grep’ing for a session harder and changes the log semantics. Prefer keepingserverID=%sand adding, sessionID=%swhensessionID != ""(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 logssessionIDas a distinct key/value when a session is present. Because these messages are also written to the file logger viaLogger.Printf→LogDebug, keepingsessionID=%sexplicitly 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)) |
This was referenced Mar 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Addresses all 15 open duplicate-code issues by implementing 4 targeted refactorings and closing issues where patterns were already resolved.
Changes
1.
formatLogLinehelper (logger/common.go)Centralizes the
[timestamp] [level] [category] messagelog line format used identically byFileLogger.Log()andServerFileLogger.Log().2. Session-conditional logging (launcher/log_helpers.go)
Consolidates
if sessionID != ""branches inlogLaunchStart,logTimeoutError, andlogLaunchSuccessusing the existingsessionSuffix()helper consistently.3.
InvalidPattern+InvalidValuehelpers (config/rules/rules.go)New reusable helpers for pattern validation and constraint violations. Applied to 5 inline
ValidationErrorconstructions invalidation_schema.go(container, mount, URL, entrypoint, domain).4.
SchemaValidationErrorhelper (config/rules/rules.go)New helper for custom schema validation failures. Applied to 8 inline constructions in
validation.gowith consistent message format.Already resolved patterns (close only)
modifyTaghelper already exists indifc/agent.gowithGlobalLoggerhelper already exists inlogger/global_helpers.goinitLoggergeneric helper already exists inlogger/common.goauth.header.gologFuncsmap + one-liner wrappersCloses
#1882, #1824, #1810, #1766, #1768, #1885, #1811, #1825, #1767, #1883, #1826, #1812, #1884, #1813, #1827