-
Hi, I'm trying to port my project from using the log crate to tracing + tracing_subscriber::fmt. I would like to first initialise the global formatting subscriber with_max_level(Level::TRACE), run a bit of code, and then reinitialize (switch the max level) to Level::INFO. According to https://docs.rs/tracing/latest/tracing/dispatcher/fn.set_global_default.html a global subscriber / dispatcher can only be set once, and it doesn't look like there's a way to reconfigure the created dispatcher (at least from my search attempts). With log, I was able to set a different logging destination / backend using the https://docs.rs/log-reroute/latest/log_reroute/ Is it possible to achieve the same somehow with the tracing crate? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You can use the In the specific use-case you described here, though, it's possible there are other solutions that might be nicer:
Rather than setting the max level to TRACE, running some code, and then changing the max level to INFO, I wonder if the goal you're trying to accomplish might be better served by using more granular filtering than just a max level, such as the This would be more idiomatic than just changing the max level for the duration of some of your code. Using this kind of filtering is a much more flexible tool for controlling what diagnostics are collected from different parts of your code. This also includes setting different filter configurations at runtime based on an environment variable or command-line arguments, which can be useful if you want to debug a particular issue or understand a particular code path without always enabling the most verbose diagnostics every time your program runs. However, the Hope that helps! Let me know if you have any follow-up questions? :) |
Beta Was this translation helpful? Give feedback.
You can use the
reload
layer intracing-subscriber
to dynamically rebuild some or all of a global subscriber at runtime. This is similar tolog
, where the logger can only be set a single time, but specific implementations of that global logger/subscriber (e.g.log_reroute
) allow changing its behavior after it's set.In the specific use-case you described here, though, it's possible there are other solutions that might be nicer:
Rather than setting the max level to TRACE, running some code, and then changing the ma…