tokio-rs / tracing

Application level tracing for Rust.
https://tracing.rs
MIT License
5.43k stars 714 forks source link

When the `tracing-subscriber` exits the block, it loses the file handle. #2815

Closed morugetsm closed 10 months ago

morugetsm commented 11 months ago

Bug Report

Version

time = { version = "0.3", features = ["macros", "formatting"] }
tracing = "0.1"
tracing-appender = "0.2"
tracing-subscriber = { version = "0.3", features = ["local-time", "time", "ansi", "env-filter"] }

Platform

Windows 11 but also occurs on linux (Ubuntu 20.04)

Description

I tried this code: ``` fn main() { { use tracing_subscriber::prelude::*; let pkg_name = env!("CARGO_PKG_NAME"); unsafe { time::util::local_offset::set_soundness(time::util::local_offset::Soundness::Unsound); } let timer = tracing_subscriber::fmt::time::LocalTime::new(time::macros::format_description!( "[year]-[month]-[day]T[hour]:[minute]:[second][offset_hour sign:mandatory]:[offset_minute]" )); let console_layer = tracing_subscriber::fmt::Layer::new() .with_timer(timer.clone()) .with_target(true) .with_filter(tracing_subscriber::filter::LevelFilter::TRACE); let log_file = format!("{}.log", pkg_name); let appender = tracing_appender::rolling::never("./", log_file); let (non_blocking_appender, _guard) = tracing_appender::non_blocking(appender); let file_layer = tracing_subscriber::fmt::Layer::new() .with_timer(timer) .with_target(true) .with_ansi(false) .with_writer(non_blocking_appender) .with_filter(tracing_subscriber::filter::LevelFilter::TRACE); let filter_str = format!("error,{}={}", pkg_name, tracing::Level::INFO); let filter = tracing_subscriber::EnvFilter::new(filter_str); tracing_subscriber::registry() .with(file_layer) .with(console_layer) .with(filter) .init(); let mut chars = pkg_name.chars(); let first = chars.next().unwrap().to_ascii_uppercase(); let label: String = std::iter::once(first).chain(chars).collect(); tracing::info!(">>>>> {} v{} <<<<<", label, env!("CARGO_PKG_VERSION")); } loop { std::thread::sleep(std::time::Duration::from_secs(1)); tracing::info!("hello"); } } ```   I expected to see this happen: The log message 'hello' appears in both the console and the file.     Instead, this happened: After leaving the block where `tracing-subscriber` initialized, the log is not printed to the file.     initialized without block: ![without_block](https://github.com/tokio-rs/tracing/assets/90916540/8baf715a-f98a-4512-8612-097864b58755) initialized within block: ![within_block](https://github.com/tokio-rs/tracing/assets/90916540/d5394ba9-406f-4f23-8bf4-77313526118f)
hubcio commented 10 months ago

I might be wrong, but please make sure that _guard is alive during whole program runtime. Once it's dropped, logging stopped working for me.

davidbarsky commented 10 months ago

you are dropping the _guard at the end of the block, which shuts down the writer. take a look at the documentation here: https://docs.rs/tracing-appender/latest/tracing_appender/non_blocking/struct.WorkerGuard.html.

morugetsm commented 10 months ago

Thank you for explaining the role of the WorkerGuard that I wasn't aware of.