@@ -53,6 +53,7 @@ pub struct LogArgs<T: AsRef<Path>> {
5353pub struct LogGuard {
5454 _file_guard : Option < WorkerGuard > ,
5555 _stdout_guard : Option < WorkerGuard > ,
56+ _mcp_file_guard : Option < WorkerGuard > ,
5657}
5758
5859/// Initialize our application level logging using the given LogArgs.
@@ -65,6 +66,7 @@ pub fn initialize_logging<T: AsRef<Path>>(args: LogArgs<T>) -> Result<LogGuard,
6566 let filter_layer = create_filter_layer ( ) ;
6667 let ( reloadable_filter_layer, reloadable_handle) = tracing_subscriber:: reload:: Layer :: new ( filter_layer) ;
6768 ENV_FILTER_RELOADABLE_HANDLE . lock ( ) . unwrap ( ) . replace ( reloadable_handle) ;
69+ let mut mcp_path = None ;
6870
6971 // First we construct the file logging layer if a file name was provided.
7072 let ( file_layer, _file_guard) = match args. log_file_path {
@@ -73,6 +75,9 @@ pub fn initialize_logging<T: AsRef<Path>>(args: LogArgs<T>) -> Result<LogGuard,
7375
7476 // Make the log path parent directory if it doesn't exist.
7577 if let Some ( parent) = log_path. parent ( ) {
78+ if log_path. ends_with ( "chat.log" ) {
79+ mcp_path = Some ( parent. to_path_buf ( ) ) ;
80+ }
7681 std:: fs:: create_dir_all ( parent) ?;
7782 }
7883
@@ -119,20 +124,63 @@ pub fn initialize_logging<T: AsRef<Path>>(args: LogArgs<T>) -> Result<LogGuard,
119124 ( None , None )
120125 } ;
121126
127+ // Set up for mcp servers layer if we are in chat
128+ let ( mcp_server_layer, _mcp_file_guard) = if let Some ( parent) = mcp_path {
129+ let mcp_path = parent. join ( "mcp.log" ) ;
130+ if args. delete_old_log_file {
131+ std:: fs:: remove_file ( & mcp_path) . ok ( ) ;
132+ } else if mcp_path. exists ( ) && std:: fs:: metadata ( & mcp_path) ?. len ( ) > MAX_FILE_SIZE {
133+ std:: fs:: remove_file ( & mcp_path) ?;
134+ }
135+ let file = if args. delete_old_log_file {
136+ File :: create ( & mcp_path) ?
137+ } else {
138+ File :: options ( ) . append ( true ) . create ( true ) . open ( & mcp_path) ?
139+ } ;
140+ #[ cfg( unix) ]
141+ {
142+ use std:: os:: unix:: fs:: PermissionsExt ;
143+ if let Ok ( metadata) = file. metadata ( ) {
144+ let mut permissions = metadata. permissions ( ) ;
145+ permissions. set_mode ( 0o600 ) ;
146+ file. set_permissions ( permissions) . ok ( ) ;
147+ }
148+ }
149+ let ( non_blocking, guard) = tracing_appender:: non_blocking ( file) ;
150+ let file_layer = fmt:: layer ( )
151+ . with_line_number ( true )
152+ . with_writer ( non_blocking)
153+ . with_filter ( EnvFilter :: new ( "mcp=trace" ) ) ;
154+ ( Some ( file_layer) , Some ( guard) )
155+ } else {
156+ ( None , None )
157+ } ;
158+
122159 if let Some ( level) = args. log_level {
123160 set_log_level ( level) ?;
124161 }
125162
126163 // Finally, initialize our logging
127- tracing_subscriber:: registry ( )
164+ let subscriber = tracing_subscriber:: registry ( )
128165 . with ( reloadable_filter_layer)
129166 . with ( file_layer)
130- . with ( stdout_layer)
131- . init ( ) ;
167+ . with ( stdout_layer) ;
168+
169+ if let Some ( mcp_server_layer) = mcp_server_layer {
170+ subscriber. with ( mcp_server_layer) . init ( ) ;
171+ return Ok ( LogGuard {
172+ _file_guard,
173+ _stdout_guard,
174+ _mcp_file_guard,
175+ } ) ;
176+ }
177+
178+ subscriber. init ( ) ;
132179
133180 Ok ( LogGuard {
134181 _file_guard,
135182 _stdout_guard,
183+ _mcp_file_guard,
136184 } )
137185}
138186
0 commit comments