Skip to content

Commit 30aad86

Browse files
Claudelpcox
andcommitted
Refactor: Extract triple-logging helpers to eliminate duplicate logging patterns
- Add inline helper functions (tripleLogInfo, tripleLogWarn, tripleLogError) - Reduces code duplication by 25% (44 to 33 logging calls) - Refactor logSecurityWarning, logLaunchStart, logLaunchError, logTimeoutError, logLaunchSuccess - All helpers are inline in log_helpers.go following marshalToResponse pattern - Maintains identical logging behavior while improving maintainability Co-authored-by: lpcox <[email protected]>
1 parent 9cf6096 commit 30aad86

File tree

1 file changed

+94
-35
lines changed

1 file changed

+94
-35
lines changed

internal/launcher/log_helpers.go

Lines changed: 94 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,62 @@ func sessionSuffix(sessionID string) string {
1919
return fmt.Sprintf(" for session '%s'", sessionID)
2020
}
2121

22+
// tripleLogInfo logs an informational message to all three loggers (file, stdout, debug).
23+
// This helper reduces code duplication for the common pattern of triple-logging in the launcher.
24+
func tripleLogInfo(serverID, category, fileMsg string, stdoutMsg string, debugMsg string) {
25+
logger.LogInfoWithServer(serverID, category, "%s", fileMsg)
26+
log.Printf("[LAUNCHER] %s", stdoutMsg)
27+
logLauncher.Printf("%s", debugMsg)
28+
}
29+
30+
// tripleLogWarn logs a warning message to all three loggers (file, stdout, debug).
31+
// This helper reduces code duplication for the common pattern of triple-logging in the launcher.
32+
func tripleLogWarn(serverID, category, fileMsg string, stdoutMsgs ...string) {
33+
logger.LogWarnWithServer(serverID, category, "%s", fileMsg)
34+
for _, msg := range stdoutMsgs {
35+
log.Printf("[LAUNCHER] ⚠️ %s", msg)
36+
}
37+
}
38+
39+
// tripleLogError logs an error message to all three loggers (file, stdout, debug).
40+
// This helper reduces code duplication for the common pattern of triple-logging in the launcher.
41+
func tripleLogError(serverID, category, fileMsg string, stdoutMsgs []string, debugMsg string) {
42+
logger.LogErrorWithServer(serverID, category, "%s", fileMsg)
43+
for _, msg := range stdoutMsgs {
44+
log.Printf("[LAUNCHER] %s", msg)
45+
}
46+
if debugMsg != "" {
47+
logLauncher.Printf("%s", debugMsg)
48+
}
49+
}
50+
2251
// logSecurityWarning logs container security warnings
2352
func (l *Launcher) logSecurityWarning(serverID string, serverCfg *config.ServerConfig) {
24-
logger.LogWarnWithServer(serverID, "backend", "Server '%s' uses direct command execution inside a container (command: %s)", serverID, serverCfg.Command)
25-
log.Printf("[LAUNCHER] ⚠️ WARNING: Server '%s' uses direct command execution inside a container", serverID)
26-
log.Printf("[LAUNCHER] ⚠️ Security Notice: Command '%s' will execute with the same privileges as the gateway", serverCfg.Command)
27-
log.Printf("[LAUNCHER] ⚠️ Consider using 'container' field instead for better isolation")
53+
tripleLogWarn(
54+
serverID, "backend",
55+
fmt.Sprintf("Server '%s' uses direct command execution inside a container (command: %s)", serverID, serverCfg.Command),
56+
fmt.Sprintf("WARNING: Server '%s' uses direct command execution inside a container", serverID),
57+
fmt.Sprintf("Security Notice: Command '%s' will execute with the same privileges as the gateway", serverCfg.Command),
58+
"Consider using 'container' field instead for better isolation",
59+
)
2860
}
2961

3062
// logLaunchStart logs server launch initiation
3163
func (l *Launcher) logLaunchStart(serverID, sessionID string, serverCfg *config.ServerConfig, isDirectCommand bool) {
3264
if sessionID != "" {
33-
logger.LogInfoWithServer(serverID, "backend", "Launching MCP backend server for session: server=%s, session=%s, command=%s, args=%v", serverID, sessionID, serverCfg.Command, sanitize.SanitizeArgs(serverCfg.Args))
34-
log.Printf("[LAUNCHER] Starting MCP server for session: %s (session: %s)", serverID, sessionID)
35-
logLauncher.Printf("Launching new session server: serverID=%s, sessionID=%s, command=%s", serverID, sessionID, serverCfg.Command)
65+
tripleLogInfo(
66+
serverID, "backend",
67+
fmt.Sprintf("Launching MCP backend server for session: server=%s, session=%s, command=%s, args=%v", serverID, sessionID, serverCfg.Command, sanitize.SanitizeArgs(serverCfg.Args)),
68+
fmt.Sprintf("Starting MCP server for session: %s (session: %s)", serverID, sessionID),
69+
fmt.Sprintf("Launching new session server: serverID=%s, sessionID=%s, command=%s", serverID, sessionID, serverCfg.Command),
70+
)
3671
} else {
37-
logger.LogInfoWithServer(serverID, "backend", "Launching MCP backend server: %s, command=%s, args=%v", serverID, serverCfg.Command, sanitize.SanitizeArgs(serverCfg.Args))
38-
log.Printf("[LAUNCHER] Starting MCP server: %s", serverID)
39-
logLauncher.Printf("Launching new server: serverID=%s, command=%s, inContainer=%v, isDirectCommand=%v",
40-
serverID, serverCfg.Command, l.runningInContainer, isDirectCommand)
72+
tripleLogInfo(
73+
serverID, "backend",
74+
fmt.Sprintf("Launching MCP backend server: %s, command=%s, args=%v", serverID, serverCfg.Command, sanitize.SanitizeArgs(serverCfg.Args)),
75+
fmt.Sprintf("Starting MCP server: %s", serverID),
76+
fmt.Sprintf("Launching new server: serverID=%s, command=%s, inContainer=%v, isDirectCommand=%v", serverID, serverCfg.Command, l.runningInContainer, isDirectCommand),
77+
)
4178
}
4279
log.Printf("[LAUNCHER] Command: %s", serverCfg.Command)
4380
log.Printf("[LAUNCHER] Args: %v", sanitize.SanitizeArgs(serverCfg.Args))
@@ -66,17 +103,24 @@ func (l *Launcher) logEnvPassthrough(args []string) {
66103

67104
// logLaunchError logs detailed launch failure diagnostics
68105
func (l *Launcher) logLaunchError(serverID, sessionID string, err error, serverCfg *config.ServerConfig, isDirectCommand bool) {
69-
logger.LogErrorWithServer(serverID, "backend", "Failed to launch MCP backend server%s: server=%s%s, error=%v",
70-
sessionSuffix(sessionID), serverID, sessionSuffix(sessionID), err)
71-
log.Printf("[LAUNCHER] ❌ FAILED to launch server '%s'%s", serverID, sessionSuffix(sessionID))
72-
log.Printf("[LAUNCHER] Error: %v", err)
73-
log.Printf("[LAUNCHER] Debug Information:")
74-
log.Printf("[LAUNCHER] - Command: %s", serverCfg.Command)
75-
log.Printf("[LAUNCHER] - Args: %v", sanitize.SanitizeArgs(serverCfg.Args))
76-
log.Printf("[LAUNCHER] - Env vars: %v", sanitize.TruncateSecretMap(serverCfg.Env))
77-
log.Printf("[LAUNCHER] - Running in container: %v", l.runningInContainer)
78-
log.Printf("[LAUNCHER] - Is direct command: %v", isDirectCommand)
79-
log.Printf("[LAUNCHER] - Startup timeout: %v", l.startupTimeout)
106+
stdoutMsgs := []string{
107+
fmt.Sprintf("❌ FAILED to launch server '%s'%s", serverID, sessionSuffix(sessionID)),
108+
fmt.Sprintf("Error: %v", err),
109+
"Debug Information:",
110+
fmt.Sprintf(" - Command: %s", serverCfg.Command),
111+
fmt.Sprintf(" - Args: %v", sanitize.SanitizeArgs(serverCfg.Args)),
112+
fmt.Sprintf(" - Env vars: %v", sanitize.TruncateSecretMap(serverCfg.Env)),
113+
fmt.Sprintf(" - Running in container: %v", l.runningInContainer),
114+
fmt.Sprintf(" - Is direct command: %v", isDirectCommand),
115+
fmt.Sprintf(" - Startup timeout: %v", l.startupTimeout),
116+
}
117+
118+
tripleLogError(
119+
serverID, "backend",
120+
fmt.Sprintf("Failed to launch MCP backend server%s: server=%s%s, error=%v", sessionSuffix(sessionID), serverID, sessionSuffix(sessionID), err),
121+
stdoutMsgs,
122+
"",
123+
)
80124

81125
if isDirectCommand && l.runningInContainer {
82126
log.Printf("[LAUNCHER] ⚠️ Possible causes:")
@@ -93,27 +137,42 @@ func (l *Launcher) logLaunchError(serverID, sessionID string, err error, serverC
93137

94138
// logTimeoutError logs startup timeout diagnostics
95139
func (l *Launcher) logTimeoutError(serverID, sessionID string) {
96-
logger.LogErrorWithServer(serverID, "backend", "MCP backend server startup timeout%s: server=%s%s, timeout=%v",
97-
sessionSuffix(sessionID), serverID, sessionSuffix(sessionID), l.startupTimeout)
98-
log.Printf("[LAUNCHER] ❌ Server startup timed out after %v", l.startupTimeout)
99-
log.Printf("[LAUNCHER] ⚠️ The server may be hanging or taking too long to initialize")
100-
log.Printf("[LAUNCHER] ⚠️ Consider increasing 'startupTimeout' in gateway config if server needs more time")
140+
stdoutMsgs := []string{
141+
fmt.Sprintf("❌ Server startup timed out after %v", l.startupTimeout),
142+
"⚠️ The server may be hanging or taking too long to initialize",
143+
"⚠️ Consider increasing 'startupTimeout' in gateway config if server needs more time",
144+
}
145+
146+
debugMsg := ""
101147
if sessionID != "" {
102-
logLauncher.Printf("Startup timeout occurred: serverID=%s, sessionID=%s, timeout=%v", serverID, sessionID, l.startupTimeout)
148+
debugMsg = fmt.Sprintf("Startup timeout occurred: serverID=%s, sessionID=%s, timeout=%v", serverID, sessionID, l.startupTimeout)
103149
} else {
104-
logLauncher.Printf("Startup timeout occurred: serverID=%s, timeout=%v", serverID, l.startupTimeout)
150+
debugMsg = fmt.Sprintf("Startup timeout occurred: serverID=%s, timeout=%v", serverID, l.startupTimeout)
105151
}
152+
153+
tripleLogError(
154+
serverID, "backend",
155+
fmt.Sprintf("MCP backend server startup timeout%s: server=%s%s, timeout=%v", sessionSuffix(sessionID), serverID, sessionSuffix(sessionID), l.startupTimeout),
156+
stdoutMsgs,
157+
debugMsg,
158+
)
106159
}
107160

108161
// logLaunchSuccess logs successful server launch
109162
func (l *Launcher) logLaunchSuccess(serverID, sessionID string) {
110163
if sessionID != "" {
111-
logger.LogInfoWithServer(serverID, "backend", "Successfully launched MCP backend server for session: server=%s, session=%s", serverID, sessionID)
112-
log.Printf("[LAUNCHER] Successfully launched: %s (session: %s)", serverID, sessionID)
113-
logLauncher.Printf("Session connection established: serverID=%s, sessionID=%s", serverID, sessionID)
164+
tripleLogInfo(
165+
serverID, "backend",
166+
fmt.Sprintf("Successfully launched MCP backend server for session: server=%s, session=%s", serverID, sessionID),
167+
fmt.Sprintf("Successfully launched: %s (session: %s)", serverID, sessionID),
168+
fmt.Sprintf("Session connection established: serverID=%s, sessionID=%s", serverID, sessionID),
169+
)
114170
} else {
115-
logger.LogInfoWithServer(serverID, "backend", "Successfully launched MCP backend server: %s", serverID)
116-
log.Printf("[LAUNCHER] Successfully launched: %s", serverID)
117-
logLauncher.Printf("Connection established: serverID=%s", serverID)
171+
tripleLogInfo(
172+
serverID, "backend",
173+
fmt.Sprintf("Successfully launched MCP backend server: %s", serverID),
174+
fmt.Sprintf("Successfully launched: %s", serverID),
175+
fmt.Sprintf("Connection established: serverID=%s", serverID),
176+
)
118177
}
119178
}

0 commit comments

Comments
 (0)