@@ -11,13 +11,13 @@ use tokio::runtime::Runtime;
11
11
use tracing:: subscriber:: Interest ;
12
12
use tracing:: { Instrument , Metadata , debug_span, metadata:: LevelFilter } ;
13
13
use tracing_appender:: rolling:: Rotation ;
14
+ use tracing_bunyan_formatter:: { BunyanFormattingLayer , JsonStorageLayer } ;
14
15
use tracing_subscriber:: {
15
- Layer ,
16
16
layer:: { Context , Filter } ,
17
17
prelude:: * ,
18
18
registry,
19
19
} ;
20
- use tracing_tree:: HierarchicalLayer ;
20
+ use tracing_tree:: { HierarchicalLayer , time :: UtcDateTime } ;
21
21
22
22
pub ( crate ) fn start (
23
23
session : CliSession ,
@@ -78,8 +78,10 @@ pub(crate) fn run_server(
78
78
config_path : Option < PathBuf > ,
79
79
log_path : Option < PathBuf > ,
80
80
log_file_name_prefix : Option < String > ,
81
+ log_level : Option < String > ,
82
+ log_kind : Option < String > ,
81
83
) -> Result < ( ) , CliDiagnostic > {
82
- setup_tracing_subscriber ( log_path, log_file_name_prefix) ;
84
+ setup_tracing_subscriber ( log_path, log_file_name_prefix, log_level , log_kind ) ;
83
85
84
86
let rt = Runtime :: new ( ) ?;
85
87
let factory = ServerFactory :: new ( stop_on_disconnect) ;
@@ -181,28 +183,54 @@ async fn start_lsp_proxy(
181
183
/// is written to log files rotated on a hourly basis (in
182
184
/// `pgt-logs/server.log.yyyy-MM-dd-HH` files inside the system temporary
183
185
/// directory)
184
- fn setup_tracing_subscriber ( log_path : Option < PathBuf > , log_file_name_prefix : Option < String > ) {
186
+ fn setup_tracing_subscriber (
187
+ log_path : Option < PathBuf > ,
188
+ log_file_name_prefix : Option < String > ,
189
+ log_level : Option < String > ,
190
+ log_kind : Option < String > ,
191
+ ) {
185
192
let pgt_log_path = log_path. unwrap_or ( pgt_fs:: ensure_cache_dir ( ) . join ( "pgt-logs" ) ) ;
193
+
186
194
let appender_builder = tracing_appender:: rolling:: RollingFileAppender :: builder ( ) ;
195
+
187
196
let file_appender = appender_builder
188
197
. filename_prefix ( log_file_name_prefix. unwrap_or ( String :: from ( "server.log" ) ) )
189
198
. max_log_files ( 7 )
190
199
. rotation ( Rotation :: HOURLY )
191
200
. build ( pgt_log_path)
192
201
. expect ( "Failed to start the logger for the daemon." ) ;
193
202
194
- registry ( )
195
- . with (
196
- HierarchicalLayer :: default ( )
197
- . with_indent_lines ( true )
198
- . with_indent_amount ( 2 )
199
- . with_bracketed_fields ( true )
200
- . with_targets ( true )
201
- . with_ansi ( false )
202
- . with_writer ( file_appender)
203
- . with_filter ( LoggingFilter ) ,
204
- )
205
- . init ( ) ;
203
+ let filter = PgtLoggingFilter :: from ( log_level) ;
204
+
205
+ let log_kind = log_kind. unwrap_or ( "hierarchical" . into ( ) ) ;
206
+
207
+ match log_kind. as_str ( ) {
208
+ "bunyan" => {
209
+ registry ( )
210
+ . with ( JsonStorageLayer )
211
+ . with (
212
+ BunyanFormattingLayer :: new ( "pgt_logs" . into ( ) , file_appender)
213
+ . with_filter ( filter) ,
214
+ )
215
+ . init ( ) ;
216
+ }
217
+
218
+ _ => registry ( )
219
+ . with (
220
+ HierarchicalLayer :: default ( )
221
+ . with_indent_lines ( true )
222
+ . with_indent_amount ( 2 )
223
+ . with_bracketed_fields ( true )
224
+ . with_targets ( true )
225
+ . with_ansi ( false )
226
+ . with_timer ( UtcDateTime {
227
+ higher_precision : false ,
228
+ } )
229
+ . with_writer ( file_appender)
230
+ . with_filter ( filter) ,
231
+ )
232
+ . init ( ) ,
233
+ }
206
234
}
207
235
208
236
pub fn default_pgt_log_path ( ) -> PathBuf {
@@ -212,22 +240,34 @@ pub fn default_pgt_log_path() -> PathBuf {
212
240
}
213
241
}
214
242
215
- /// Tracing filter enabling :
216
- /// - All spans and events at level info or higher
217
- /// - All spans and events at level debug in crates whose name starts with `pgt`
218
- struct LoggingFilter ;
243
+ /// Tracing Filter with two rules :
244
+ /// For all crates starting with pgt*, use `PGT_LOG_LEVEL` or CLI option or "info" as default
245
+ /// For all other crates, use "info"
246
+ struct PgtLoggingFilter ( LevelFilter ) ;
219
247
220
- /// Tracing filter used for spans emitted by `pgt*` crates
221
- const SELF_FILTER : LevelFilter = if cfg ! ( debug_assertions) {
222
- LevelFilter :: TRACE
223
- } else {
224
- LevelFilter :: DEBUG
225
- } ;
248
+ impl From < Option < String > > for PgtLoggingFilter {
249
+ fn from ( value : Option < String > ) -> Self {
250
+ Self (
251
+ value
252
+ . map ( |lv_filter| match lv_filter. as_str ( ) {
253
+ "trace" => LevelFilter :: TRACE ,
254
+ "debug" => LevelFilter :: DEBUG ,
255
+ "info" => LevelFilter :: INFO ,
256
+ "warn" => LevelFilter :: WARN ,
257
+ "error" => LevelFilter :: ERROR ,
258
+ "off" => LevelFilter :: OFF ,
259
+
260
+ _ => LevelFilter :: INFO ,
261
+ } )
262
+ . unwrap_or ( LevelFilter :: INFO ) ,
263
+ )
264
+ }
265
+ }
226
266
227
- impl LoggingFilter {
267
+ impl PgtLoggingFilter {
228
268
fn is_enabled ( & self , meta : & Metadata < ' _ > ) -> bool {
229
269
let filter = if meta. target ( ) . starts_with ( "pgt" ) {
230
- SELF_FILTER
270
+ self . 0
231
271
} else {
232
272
LevelFilter :: INFO
233
273
} ;
@@ -236,7 +276,7 @@ impl LoggingFilter {
236
276
}
237
277
}
238
278
239
- impl < S > Filter < S > for LoggingFilter {
279
+ impl < S > Filter < S > for PgtLoggingFilter {
240
280
fn enabled ( & self , meta : & Metadata < ' _ > , _cx : & Context < ' _ , S > ) -> bool {
241
281
self . is_enabled ( meta)
242
282
}
@@ -250,6 +290,6 @@ impl<S> Filter<S> for LoggingFilter {
250
290
}
251
291
252
292
fn max_level_hint ( & self ) -> Option < LevelFilter > {
253
- Some ( SELF_FILTER )
293
+ Some ( self . 0 )
254
294
}
255
295
}
0 commit comments