Files
Jan Hohenheim 3683903744 Implement pretty component logging (#21587)
# Objective

`log_components` looks like this:
<img width="1609" height="266" alt="image"
src="https://github.com/user-attachments/assets/6e4e5173-9f9e-4cf6-8ed1-a64d4b991a8a"
/>

eek! what is going on??? I could pretty-print this by fiddling with the
logging filters, but that
- is weird boilerplate, just arcane enough that I would not be able to
do it without looking it up or copy-pasting it from somewhere and
- would change ALL debug prints to use newlines, which would flood my
terminal.

Can I just pretty print the component log plz?

## Solution

Add `log_components_pretty`:

<img width="1480" height="125" alt="image"
src="https://github.com/user-attachments/assets/ffecc45c-8bb9-4156-8aeb-623c3db65900"
/>

much better! 

- The name and the entity ID of the logged entity
- All component names without the `DebugName` wrapper
- Alphanumerically sorted components

And we can go one step further when downstream (thanks Chris!)
```rust
DefaultPlugins.set(bevy::log::LogPlugin {
            fmt_layer: |_| {
                Some(Box::new(
                    bevy::log::tracing_subscriber::fmt::Layer::default()
                        .map_fmt_fields(|f| f.debug_alt())
                        .with_writer(std::io::stderr),
                ))
            },
            ..default()
        })
```

which gives us newlines:

<img width="939" height="339" alt="image"
src="https://github.com/user-attachments/assets/ed4c327e-12ab-46cd-b698-1d911a2d762e"
/>


## Additional info

Added a `debug` feature because the experience of getting blasted with
30 strings telling me to enable a feature is suboptimal.

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
2025-10-18 16:44:29 +00:00

77 lines
2.5 KiB
Rust

//! This example illustrates how to add custom log layers in bevy.
use bevy::{
log::{
tracing::{self, Subscriber},
tracing_subscriber::{field::MakeExt, Layer},
BoxedFmtLayer, BoxedLayer,
},
prelude::*,
};
struct CustomLayer;
impl<S: Subscriber> Layer<S> for CustomLayer {
fn on_event(
&self,
event: &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(),
]))
}
// While `custom_layer` allows you to add _additional_ layers, it won't allow you to override the
// default `tracing_subscriber::fmt::Layer` added by `LogPlugin`. To do that, you can use the
// `fmt_layer` option.
//
// In this example, we're disabling the timestamp in the log output and enabling the alternative debugging format.
// This formatting inserts newlines into logs that use the debug sigil (`?`) like `info!(foo=?bar)`
fn fmt_layer(_app: &mut App) -> Option<BoxedFmtLayer> {
Some(Box::new(
bevy::log::tracing_subscriber::fmt::Layer::default()
.without_time()
.map_fmt_fields(MakeExt::debug_alt)
.with_writer(std::io::stderr),
))
}
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(bevy::log::LogPlugin {
custom_layer,
fmt_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");
let secret_message = "Bevy";
info!(?secret_message, "Here's a log that uses the debug sigil");
debug!("helpful for debugging");
trace!("very noisy");
}