@@ -2,7 +2,7 @@ use crate::telemetry::log::init::{apply_filters_to_drain, LogHarness};
22use crate :: telemetry:: log:: internal:: LoggerWithKvNestingTracking ;
33use crate :: telemetry:: settings:: LoggingSettings ;
44use parking_lot:: RwLock as ParkingRwLock ;
5- use slog:: { Drain , Key , Level , Logger , Never , OwnedKVList , Record , Serializer , KV } ;
5+ use slog:: { Discard , Drain , Key , Level , Logger , Never , OwnedKVList , Record , Serializer , KV } ;
66use std:: fmt:: Arguments ;
77use std:: sync:: { Arc , RwLock } ;
88
@@ -36,11 +36,16 @@ impl Serializer for TestFieldSerializer {
3636 }
3737}
3838
39- struct TestLogDrain {
39+ struct TestLogDrain < D > {
4040 records : TestLogRecords ,
41+ /// Optional drain to forward logs to after recording in the test log
42+ forward : Option < D > ,
4143}
4244
43- impl Drain for TestLogDrain {
45+ impl < D > Drain for TestLogDrain < D >
46+ where
47+ D : Drain < Ok = ( ) , Err = Never > ,
48+ {
4449 type Ok = ( ) ;
4550 type Err = Never ;
4651
@@ -56,6 +61,10 @@ impl Drain for TestLogDrain {
5661 fields : serializer. fields ,
5762 } ) ;
5863
64+ if let Some ( forward_drain) = & self . forward {
65+ let _ = forward_drain. log ( record, kv) ;
66+ }
67+
5968 Ok ( ( ) )
6069 }
6170}
@@ -65,8 +74,43 @@ pub(crate) fn create_test_log(
6574) -> ( LoggerWithKvNestingTracking , TestLogRecords ) {
6675 let log_records = Arc :: new ( RwLock :: new ( vec ! [ ] ) ) ;
6776
77+ let drain: TestLogDrain < Discard > = TestLogDrain {
78+ records : Arc :: clone ( & log_records) ,
79+ forward : None ,
80+ } ;
81+
82+ let drain = Arc :: new ( apply_filters_to_drain ( drain, settings) ) ;
83+ let log = LoggerWithKvNestingTracking :: new ( Logger :: root ( Arc :: clone ( & drain) , slog:: o!( ) ) ) ;
84+
85+ let _ = LogHarness :: override_for_testing ( LogHarness {
86+ root_log : Arc :: new ( ParkingRwLock :: new ( log. clone ( ) ) ) ,
87+ root_drain : drain,
88+ settings : settings. clone ( ) ,
89+ log_scope_stack : Default :: default ( ) ,
90+ } ) ;
91+
92+ ( log, log_records)
93+ }
94+
95+ /// Create a test log with a tracing-rs compat drain installed in addition to the test drain
96+ #[ cfg( feature = "tracing-rs-compat" ) ]
97+ pub ( crate ) fn create_test_log_for_tracing_compat (
98+ settings : & LoggingSettings ,
99+ ) -> ( LoggerWithKvNestingTracking , TestLogRecords ) {
100+ use tracing_slog:: TracingSlogDrain ;
101+
102+ assert ! ( matches!(
103+ settings. output,
104+ crate :: telemetry:: settings:: LogOutput :: TracingRsCompat
105+ ) ) ;
106+
107+ let base_drain = TracingSlogDrain { } ;
108+
109+ let log_records = Arc :: new ( RwLock :: new ( vec ! [ ] ) ) ;
110+
68111 let drain = TestLogDrain {
69112 records : Arc :: clone ( & log_records) ,
113+ forward : Some ( base_drain. fuse ( ) ) ,
70114 } ;
71115
72116 let drain = Arc :: new ( apply_filters_to_drain ( drain, settings) ) ;
0 commit comments