How to get to tracing-subscriber pub fn current() -> LevelFilter, please?

This is the method I'd like to call: pub fn current().

I just follow the crates' documentation, and following is an example I came up with, which does some logging to hourly rolling log file.

Content of Cargo.toml:
[package]
name = "learn-tracing-log"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
time = { version = "0.3.22", default-features = false, features = ["formatting", "macros", "parsing"] }
tracing = "0.1.40"
tracing-appender = "0.2.2"
tracing-subscriber = {version = "0.3.17", features = ["fmt", "std", "local-time", "time"]}
rand = "0.8.5"
Content of src/main.rs:
use time::macros::format_description;
use tracing::Level;

use tracing_subscriber::fmt::time::LocalTime;

use std::io;
use rand::Rng;

fn main() {
    let file_appender = tracing_appender::rolling::hourly("./", "prefix.log");
    let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);

    let tracing_sub = tracing_subscriber::fmt();

    //tracing_subscriber::fmt()
    tracing_sub
        .with_writer(non_blocking)
        //.with_max_level(Level::TRACE) //ERROR, WARN, INFO, DEBUG, TRACE 
        //.with_max_level(Level::DEBUG) //ERROR, WARN, INFO, DEBUG 
        .with_max_level(Level::INFO) //ERROR, WARN, INFO
        .with_timer(LocalTime::new(format_description!("[hour]:[minute]:[second]")))
        .with_ansi(false)
        .init();

    tracing::info!("Initialisation completed.");

   
    // TO_DO: how to call pub fn current()? 
    // How to get to the active Subscriber?	
    // tracing::info!("tracing_subscriber.current() {}", tracing_sub.???.current());

    loop {
        println!("Please type in something: ");

        let mut input = String::new();

        io::stdin()
            .read_line(&mut input)
            .expect("Failed to read line");

        let trimmed_input = input.trim();

        match rand::thread_rng().gen_range(1..=5) {
            1 => tracing::info!("You typed in: {trimmed_input}"),
            2 => tracing::debug!("You typed in: {trimmed_input}"),
            3 => tracing::warn!("You typed in: {trimmed_input}"),
            4 => tracing::error!("You typed in: {trimmed_input}"),
            5 => tracing::trace!("You typed in: {trimmed_input}"),
            _ => tracing::info!("_: {trimmed_input}"),
        }
    }    
}

Thank you and best regards,

...behai.

tracing_subscriber::filter::LevelFilter::current()

1 Like

short answer: don't do it. you should only check whether a certain level is enabled. you are not supposed to query the current LevelFilter in any case. (because a there might not be any LevelFilter and the Subscriber might do the level comparison without the filter/layer mechanism).


LevelFilter::current() is not a method, it's an associated function of the LevelFilter type itself, just call it directly.

caveat: read the document carefully, it will return the most verbose level that current subscriber can enable. it will not retrieve the actual level associated with the active subscriber, because the current subscriber can be any type that implements Subscriber trait and might not have an associated LevelFilter at all.

you cannot get the current subscriber because Subscriber is a trait, not a type. you can get a erased Dispatch wrapper type by calling Dispatch::default(), although the Subscriber trait only has a max_level_hint() method and Dispatch doesn't forward it.

if you know the concrete type of the current registered subscriber, then you can call Dispatch::downcast_ref() to get a reference to the underlying Subscriber. you'll have to implement your custom Subscriber type though, because the layered structure provided by tracing-subscriber makes it impractical to name the actual type of the subscriber.

2 Likes

I should add, the proper way to check if a level is enabled is to use the tracing::enabled!() macro, like this:

use tracing::{enabled, Level};
if enabled!(Level::DEBUG) {
    // potential expensive calculation for debugging purpose
}
2 Likes

Good evening vague,

Thank you very much for your helps. Obviously, I did not understand the syntax of the documentation very well.

Best regards,

...behai.

Good evening nerditation,

Thank you very much for your times and your helps. That is a lot for me to take in. I understand the essence of what you said, though the detail I need more time to understand fully, I am still a bit new to Rust.

Before this post, I sort of figured out that we should not use this method:

User code should treat this as a hint.

But since it is there, and I did not know how to call it, I felt I should know :slight_smile:

Thank you kindly again, nerditation.

Best regards,

...behai.

Thank you again nerditation, you are very kind.

Best regards,

...behai.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.