-
I'm currently using tracing for a program where 10 threads are run all at the same time. A ton of logs are emitted asynchronously so there's a lot of noise from each thread's logs being shoved together out of order. I wanted to ask if there's an easy way to separate logs by thread? I need to do this in order to debug my program. I hacked together some code to accomplish this but it randomly crashes because it tries to My current approach is to just generate a subscriber like so: pub(crate) fn create_subscriber(&self) -> impl Subscriber {
// Create non-blocking writer for file logging
let file_appender = tracing_appender::rolling::RollingFileAppender::new(
tracing_appender::rolling::Rotation::NEVER,
&self.log_path,
"TESTING",
);
let (non_blocking_file, _guard) = tracing_appender::non_blocking(file_appender);
// Necessary due to the filter closure below.
let name = self.name.clone();
// Create a format for specific logs
let file_layer = fmt::layer()
.with_target(true)
.with_thread_ids(true)
.with_level(true)
.with_ansi(false)
.with_file(true)
.with_line_number(true)
.with_writer(non_blocking_file)
// Filter based on name in the target or fields
.with_filter(filter_fn(move |metadata| {
if let Some(field) = metadata.fields().field("name") {
field.to_string().contains(&name)
} else {
false
}
}))
.with_filter(LevelFilter::INFO);
Registry::default().with(file_layer)
}
} and use it in the method that each thread runs like so: // Create a scoped logger for this repository
let _repo_guard = tracing::subscriber::set_default(my_logger.create_subscriber()); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You could just create a custom Layer (https://docs.rs/tracing-subscriber/latest/tracing_subscriber/layer/index.html) and then check a task_local storage (https://docs.rs/tokio/latest/tokio/macro.task_local.html) in the struct CustomSubscriber {}
impl<S: Subscriber> Layer<S> for CustomSubscriber {
fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>) {
LOG.with(|flag| {
if *flag {
// log
}
})
}
}
fn main() {
tracing_subscriber::registry()
.with(CustomSubscriber {})
.init();
let _ = std::thread::spawn(|| {
LOG.sync_scope(true, || {
tracing::info!("info");
})
})
.join();
} |
Beta Was this translation helpful? Give feedback.
You could just create a custom Layer (https://docs.rs/tracing-subscriber/latest/tracing_subscriber/layer/index.html) and then check a task_local storage (https://docs.rs/tokio/latest/tokio/macro.task_local.html) in the
on_event
function. e.g. something like this: