@@ -42,7 +42,9 @@ use tracing_core::{Event, Subscriber};
42
42
use tracing_subscriber:: filter:: { Directive , EnvFilter , LevelFilter } ;
43
43
use tracing_subscriber:: fmt:: FmtContext ;
44
44
use tracing_subscriber:: fmt:: format:: { self , FormatEvent , FormatFields } ;
45
- use tracing_subscriber:: layer:: SubscriberExt ;
45
+ use tracing_subscriber:: layer:: { Identity , SubscriberExt } ;
46
+ // Re-export tracing_subscriber items so rustc_driver_impl doesn't need to depend on it.
47
+ pub use tracing_subscriber:: { Layer , Registry } ;
46
48
47
49
/// The values of all the environment variables that matter for configuring a logger.
48
50
/// Errors are explicitly preserved so that we can share error handling.
@@ -72,6 +74,15 @@ impl LoggerConfig {
72
74
73
75
/// Initialize the logger with the given values for the filter, coloring, and other options env variables.
74
76
pub fn init_logger ( cfg : LoggerConfig ) -> Result < ( ) , Error > {
77
+ init_logger_with_additional_layer ( cfg, Identity :: new ( ) )
78
+ }
79
+
80
+ /// Initialize the logger with the given values for the filter, coloring, and other options env variables.
81
+ /// Additionally add a custom layer to collect logging and tracing events.
82
+ pub fn init_logger_with_additional_layer (
83
+ cfg : LoggerConfig ,
84
+ additional_tracing_layer : impl Layer < Registry > + Send + Sync ,
85
+ ) -> Result < ( ) , Error > {
75
86
let filter = match cfg. filter {
76
87
Ok ( env) => EnvFilter :: new ( env) ,
77
88
_ => EnvFilter :: default ( ) . add_directive ( Directive :: from ( LevelFilter :: WARN ) ) ,
@@ -104,7 +115,7 @@ pub fn init_logger(cfg: LoggerConfig) -> Result<(), Error> {
104
115
} ;
105
116
106
117
let mut layer = tracing_tree:: HierarchicalLayer :: default ( )
107
- . with_writer ( io:: stderr)
118
+ . with_writer ( io:: stderr as fn ( ) -> io :: Stderr )
108
119
. with_ansi ( color_logs)
109
120
. with_targets ( true )
110
121
. with_verbose_exit ( verbose_entry_exit)
@@ -124,7 +135,8 @@ pub fn init_logger(cfg: LoggerConfig) -> Result<(), Error> {
124
135
Err ( _) => { } // no wraptree
125
136
}
126
137
127
- let subscriber = tracing_subscriber:: Registry :: default ( ) . with ( filter) . with ( layer) ;
138
+ let subscriber =
139
+ Registry :: default ( ) . with ( additional_tracing_layer) . with ( layer. with_filter ( filter) ) ;
128
140
match cfg. backtrace {
129
141
Ok ( backtrace_target) => {
130
142
let fmt_layer = tracing_subscriber:: fmt:: layer ( )
0 commit comments