Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Currently, the only ways to set the default subscriber are
The current API surface makes some use-cases difficult. For example, if I wanted to use a fresh subscriber in each of my tests, I would need to use
@Ralith suggested that we add a method for setting the default subscriber, returning an RAII drop guard that unsets the subscriber when dropped. This could be used to set the subscriber only for the duration of a scope, without having to indent the entire scope as with
I'll add that the current implementation of
This is in order to unset the default subscriber on panics. We could simply make this existing code public API.
Alternatively, we could not add a drop guard for setting the default subscriber. The existing API was chosen for consistency with existing
To be clear, there doesn't seem to be a simple way to set a single global subscriber for tests, so this use case isn't niche. You could start every test with a call that locks a global mutex and installs a global subscriber if one does not yet exist, but that's both uglier and requires more code than the proposed solution.
@Ralith AFAICT this is the state of the art for logging in tests in the
Here's what I think we should do:
This should be fairly straightforward, so I'm marking this as a "good first issue" — if anyone wants to work on this, please feel free to ask me any additional questions that come up.
* Add `tracing::subscriber::set_default` which sets the default subscriber and returns a drop guard. This drop guard will reset the dispatch on drop. * Add `tracing_core::dispatcher::set_default` method which sets the default dispatch and returns a drop guard. * Update `tracing_core::dispatcher::with_default` method to use the new `tracing_core::dispatcher::set_default` method. * Add test to confirm expected behavior * Fixes: tokio-rs#383