mirror of
https://github.com/bevyengine/bevy
synced 2024-11-23 05:03:47 +00:00
ded5d523bd
# Objective - Fixes https://github.com/bevyengine/bevy/issues/12597 The current tracing customization option (the `update_subscriber` field) was basically unusable because it provides a `dyn Subscriber` and most layers require a `Subscriber` that also implements `for<'a> LookupSpan<'a, Data=Data<'a>>`, so it was impossible to add a layer on top of the `dyn Subscriber`. This PR provides an alternative way of adding additional tracing layers to the LogPlugin by instead creating an `Option<Layer>`. This is enough for most situations because `Option<Layer>` and `Vec<Layer>` both implement `Layer`. ## Solution - Replace the `update_subscriber` field of `LogPlugin` with a `custom_layer` field which is function pointer returning an `Option<BoxedLayer>` - Update the examples to showcase that this works: - with multiple additional layers - with Layers that were previously problematic, such as `bevy::log::tracing_subscriber::fmt::layer().with_file(true)` (mentioned in the issue) Note that in the example this results in duplicate logs, since we have our own layer on top of the default `fmt_layer` added in the LogPlugin; maybe in the future we might want to provide a single one? Or to let the user customize the default `fmt_layer` ? I still think this change is an improvement upon the previous solution, which was basically broken. --- ## Changelog > This section is optional. If this was a trivial fix, or has no externally-visible impact, you can delete this section. - The `LogPlugin`'s `update_subscriber` field has been replaced with `custom_layer` to allow the user to flexibly add a `tracing::Layer` to the layer stack ## Migration Guide - The `LogPlugin`'s `update_subscriber` field has been replaced with `custom_layer` --------- Co-authored-by: BD103 <59022059+BD103@users.noreply.github.com>
52 lines
1.6 KiB
Rust
52 lines
1.6 KiB
Rust
//! This example illustrates how to add custom log layers in bevy.
|
|
|
|
use bevy::log::BoxedLayer;
|
|
use bevy::{log::tracing_subscriber::Layer, 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());
|
|
}
|
|
}
|
|
|
|
// We don't need App for this example, as we are just printing log information.
|
|
// For an example that uses App, see log_layers_ecs.
|
|
fn custom_layer(_app: &mut App) -> Option<BoxedLayer> {
|
|
// You can provide multiple layers like this, since Vec<Layer> is also a layer:
|
|
Some(Box::new(vec![
|
|
bevy::log::tracing_subscriber::fmt::layer()
|
|
.with_file(true)
|
|
.boxed(),
|
|
CustomLayer.boxed(),
|
|
]))
|
|
}
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins.set(bevy::log::LogPlugin {
|
|
custom_layer,
|
|
|
|
..default()
|
|
}))
|
|
.add_systems(Update, log_system)
|
|
.run();
|
|
}
|
|
|
|
fn log_system() {
|
|
// here is how you write new logs at each "log level" (in "most important" 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");
|
|
}
|