@@ -207,3 +207,60 @@ func TestServerFileLoggerMultipleInit(t *testing.T) {
207207 assert .Contains (t , string (content ), "Message 1" )
208208 assert .Contains (t , string (content ), "Message 2" )
209209}
210+
211+ func TestServerFileLoggerPreservesUnifiedView (t * testing.T ) {
212+ // Create temporary directories for testing
213+ tmpDir := t .TempDir ()
214+ logDir := filepath .Join (tmpDir , "logs" )
215+
216+ // Initialize both the unified file logger and the server file logger
217+ err := InitFileLogger (logDir , "mcp-gateway.log" )
218+ require .NoError (t , err , "InitFileLogger failed" )
219+ defer CloseGlobalLogger ()
220+
221+ err = InitServerFileLogger (logDir )
222+ require .NoError (t , err , "InitServerFileLogger failed" )
223+ defer CloseServerFileLogger ()
224+
225+ // Log messages using per-serverID logging
226+ LogInfoWithServer ("github" , "backend" , "GitHub server started" )
227+ LogWarnWithServer ("slack" , "backend" , "Slack connection timeout" )
228+ LogErrorWithServer ("github" , "backend" , "GitHub authentication failed" )
229+
230+ // Close loggers to flush
231+ err = CloseServerFileLogger ()
232+ require .NoError (t , err )
233+ err = CloseGlobalLogger ()
234+ require .NoError (t , err )
235+
236+ // Verify per-serverID log files exist and contain correct messages
237+ githubLog := filepath .Join (logDir , "github.log" )
238+ githubContent , err := os .ReadFile (githubLog )
239+ require .NoError (t , err , "github.log should exist" )
240+ assert .Contains (t , string (githubContent ), "GitHub server started" )
241+ assert .Contains (t , string (githubContent ), "GitHub authentication failed" )
242+ assert .NotContains (t , string (githubContent ), "Slack connection timeout" , "github.log should not contain Slack messages" )
243+
244+ slackLog := filepath .Join (logDir , "slack.log" )
245+ slackContent , err := os .ReadFile (slackLog )
246+ require .NoError (t , err , "slack.log should exist" )
247+ assert .Contains (t , string (slackContent ), "Slack connection timeout" )
248+ assert .NotContains (t , string (slackContent ), "GitHub" , "slack.log should not contain GitHub messages" )
249+
250+ // CRITICAL: Verify unified log file contains ALL messages from all servers
251+ unifiedLog := filepath .Join (logDir , "mcp-gateway.log" )
252+ unifiedContent , err := os .ReadFile (unifiedLog )
253+ require .NoError (t , err , "mcp-gateway.log should exist" )
254+
255+ // All messages should be in the unified log with serverID prefix
256+ assert .Contains (t , string (unifiedContent ), "[github]" , "unified log should have github prefix" )
257+ assert .Contains (t , string (unifiedContent ), "GitHub server started" , "unified log should contain GitHub message" )
258+ assert .Contains (t , string (unifiedContent ), "[slack]" , "unified log should have slack prefix" )
259+ assert .Contains (t , string (unifiedContent ), "Slack connection timeout" , "unified log should contain Slack message" )
260+ assert .Contains (t , string (unifiedContent ), "GitHub authentication failed" , "unified log should contain GitHub error" )
261+
262+ // Verify unified log has all three messages
263+ lines := strings .Split (strings .TrimSpace (string (unifiedContent )), "\n " )
264+ assert .GreaterOrEqual (t , len (lines ), 3 , "unified log should have at least 3 messages" )
265+ }
266+
0 commit comments