@@ -37,6 +37,7 @@ use lib::core::rewrite::MergeConflictRemediation;
3737use lib:: git:: Repo ;
3838use lib:: git:: RepoError ;
3939use lib:: util:: ExitCode ;
40+ use tracing:: level_filters:: LevelFilter ;
4041use tracing_chrome:: ChromeLayerBuilder ;
4142use tracing_error:: ErrorLayer ;
4243use tracing_subscriber:: fmt as tracing_fmt;
@@ -94,8 +95,6 @@ fn rewrite_args(args: Vec<OsString>) -> Vec<OsString> {
9495/// Wrapper function for `main` to ensure that `Drop` is called for local
9596/// variables, since `std::process::exit` will skip them.
9697fn do_main_and_drop_locals ( ) -> eyre:: Result < i32 > {
97- let _tracing_guard = install_tracing ( ) ;
98-
9998 let args = rewrite_args ( std:: env:: args_os ( ) . collect_vec ( ) ) ;
10099 let Opts {
101100 working_directory,
@@ -126,6 +125,8 @@ fn do_main_and_drop_locals() -> eyre::Result<i32> {
126125 } ;
127126 let effects = Effects :: new ( color) ;
128127
128+ let _tracing_guard = install_tracing ( effects. clone ( ) ) ;
129+
129130 if let Some ( ExitCode ( exit_code) ) = check_unsupported_config_options ( & effects) ? {
130131 let exit_code: i32 = exit_code. try_into ( ) ?;
131132 return Ok ( exit_code) ;
@@ -406,27 +407,11 @@ pub fn main() {
406407}
407408
408409#[ must_use = "This function returns a guard object to flush traces. Dropping it immediately is probably incorrect. Make sure that the returned value lives until tracing has finished." ]
409- fn install_tracing ( ) -> eyre:: Result < impl Drop > {
410- let ( filter_layer, fmt_layer) = match EnvFilter :: try_from_default_env ( ) {
411- Ok ( filter_layer) => {
412- let fmt_layer = tracing_fmt:: layer ( )
413- . with_span_events ( tracing_fmt:: format:: FmtSpan :: CLOSE )
414- . with_target ( false ) ;
415- ( Some ( filter_layer) , Some ( fmt_layer) )
416- }
417- Err ( _) => {
418- // We would like the filter layer to apply *only* to the formatting
419- // layer. That way, the logging output is suppressed, but we still
420- // get spantraces for use with `color-eyre`. However, it's currently
421- // not possible (?), at least not without writing some a custom
422- // subscriber. See https://github.com/tokio-rs/tracing/pull/1523
423- //
424- // The workaround is to only display logging messages if `RUST_LOG`
425- // is set (which is unfortunate, because we'll miss out on
426- // `WARN`-level messages by default).
427- ( None , None )
428- }
429- } ;
410+ fn install_tracing ( effects : Effects ) -> eyre:: Result < impl Drop > {
411+ let env_filter = EnvFilter :: builder ( )
412+ . with_default_directive ( LevelFilter :: WARN . into ( ) )
413+ . from_env_lossy ( ) ;
414+ let fmt_layer = tracing_fmt:: layer ( ) . with_writer ( move || effects. clone ( ) . get_error_stream ( ) ) ;
430415
431416 let ( profile_layer, flush_guard) : ( _ , Box < dyn Any > ) = {
432417 // We may invoke a hook that calls back into `git-branchless`. In that case,
@@ -475,8 +460,7 @@ fn install_tracing() -> eyre::Result<impl Drop> {
475460
476461 tracing_subscriber:: registry ( )
477462 . with ( ErrorLayer :: default ( ) )
478- . with ( filter_layer)
479- . with ( fmt_layer)
463+ . with ( fmt_layer. with_filter ( env_filter) )
480464 . with ( profile_layer)
481465 . try_init ( ) ?;
482466
0 commit comments