@@ -105,20 +105,32 @@ where
105105/// Initialize the dual-layer tracing subscriber.
106106///
107107/// Sets up:
108- /// - File layer: writes to /tmp/djls.log with daily rotation
108+ /// - File layer: writes to XDG cache directory (e.g., ~/.cache/djls/djls.log on Linux) with daily rotation.
109+ /// Falls back to /tmp/djls.log if XDG cache directory is not available.
110+ /// If file logging cannot be initialized, falls back to stderr.
109111/// - LSP layer: forwards INFO+ messages to the client
110112/// - `EnvFilter`: respects `RUST_LOG` env var, defaults to "info"
111113///
112- /// Returns a `WorkerGuard` that must be kept alive for the file logging to work.
114+ /// Returns a `WorkerGuard` that must be kept alive for the logging to work.
113115pub fn init_tracing < F > ( send_message : F ) -> WorkerGuard
114116where
115117 F : Fn ( lsp_types:: MessageType , String ) + Send + Sync + ' static ,
116118{
117- let file_appender = tracing_appender:: rolling:: daily ( "/tmp" , "djls.log" ) ;
118- let ( non_blocking, guard) = tracing_appender:: non_blocking ( file_appender) ;
119-
120119 let env_filter = EnvFilter :: try_from_default_env ( ) . unwrap_or_else ( |_| EnvFilter :: new ( "info" ) ) ;
121- let file_layer = fmt:: layer ( )
120+
121+ let ( non_blocking, guard) = match djls_conf:: log_dir ( ) {
122+ Ok ( log_dir) => {
123+ let file_appender = tracing_appender:: rolling:: daily ( log_dir. as_std_path ( ) , "djls.log" ) ;
124+ tracing_appender:: non_blocking ( file_appender)
125+ }
126+ Err ( e) => {
127+ eprintln ! ( "Warning: Failed to initialize file logging: {e}" ) ;
128+ eprintln ! ( "Falling back to stderr logging..." ) ;
129+ tracing_appender:: non_blocking ( std:: io:: stderr ( ) )
130+ }
131+ } ;
132+
133+ let log_layer = fmt:: layer ( )
122134 . with_writer ( non_blocking)
123135 . with_ansi ( false )
124136 . with_thread_ids ( true )
@@ -131,7 +143,7 @@ where
131143 let lsp_layer =
132144 LspLayer :: new ( send_message) . with_filter ( tracing_subscriber:: filter:: LevelFilter :: INFO ) ;
133145
134- Registry :: default ( ) . with ( file_layer ) . with ( lsp_layer) . init ( ) ;
146+ Registry :: default ( ) . with ( log_layer ) . with ( lsp_layer) . init ( ) ;
135147
136148 guard
137149}
0 commit comments