-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for updating the tracing subscriber in LogPlugin (#10822)
# Objective This PR is heavily inspired by #7682 It aims to solve the same problem: allowing the user to extend the tracing subscriber with extra layers. (in my case, I'd like to use `use metrics_tracing_context::{MetricsLayer, TracingContextLayer};`) ## Solution I'm proposing a different api where the user has the opportunity to take the existing `subscriber` and apply any transformations on it. --- ## Changelog - Added a `update_subscriber` option on the `LogPlugin` that lets the user modify the `subscriber` (for example to extend it with more tracing `Layers` ## Migration Guide > This section is optional. If there are no breaking changes, you can delete this section. - Added a new field `update_subscriber` in the `LogPlugin` --------- Co-authored-by: Charles Bournhonesque <cbournhonesque@snapchat.com>
- Loading branch information
1 parent
8a523de
commit 8c6d9b8
Showing
5 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//! This example illustrates how to add custom log layers in bevy. | ||
use bevy::{ | ||
log::tracing_subscriber::{layer::SubscriberExt, Layer}, | ||
log::BoxedSubscriber, | ||
prelude::*, | ||
utils::tracing::Subscriber, | ||
}; | ||
|
||
struct CustomLayer; | ||
|
||
impl<S: Subscriber> Layer<S> for CustomLayer { | ||
fn on_event( | ||
&self, | ||
event: &bevy::utils::tracing::Event<'_>, | ||
_ctx: bevy::log::tracing_subscriber::layer::Context<'_, S>, | ||
) { | ||
println!("Got event!"); | ||
println!(" level={:?}", event.metadata().level()); | ||
println!(" target={:?}", event.metadata().target()); | ||
println!(" name={:?}", event.metadata().name()); | ||
} | ||
} | ||
|
||
fn update_subscriber(subscriber: BoxedSubscriber) -> BoxedSubscriber { | ||
Box::new(subscriber.with(CustomLayer)) | ||
} | ||
|
||
fn main() { | ||
App::new() | ||
.add_plugins(DefaultPlugins.set(bevy::log::LogPlugin { | ||
update_subscriber: Some(update_subscriber), | ||
..default() | ||
})) | ||
.add_systems(Update, log_system) | ||
.run(); | ||
} | ||
|
||
fn log_system() { | ||
// here is how you write new logs at each "log level" (in "most import" to | ||
// "least important" order) | ||
error!("something failed"); | ||
warn!("something bad happened that isn't a failure, but thats worth calling out"); | ||
info!("helpful information that is worth printing by default"); | ||
debug!("helpful for debugging"); | ||
trace!("very noisy"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters