Skip to content

Commit

Permalink
Support for user defined log formatting (bevyengine#8374)
Browse files Browse the repository at this point in the history
# Objective

Adopted bevyengine#3778.

This pr allows to configure how the bevy log is formatted. The default
bevy log can be a bit verbose for some projects, specially the
timestamp. The objective is to allow developers to select the format
that best suits them.

```
Example of original bevy log:
2023-04-13T14:19:53.674653Z INFO bevy_render::renderer: [important text!]
```

## Solution

Using `tracing_subscriber::fmt::Subscriber`, adds the attribute `layer`
to `LogPlugin`. This allows to pass a `tracing_subscriber::fmt::Layer`
when creating the plugin and modifying the output.

For example this

```rust
App::new()
    .add_plugins(DefaultPlugins.set(bevy::log::LogPlugin {
        layer: Box::new(|| Box::new(bevy::log::tracing_subscriber::fmt::layer().pretty().without_time()))
        ..default()
    }))
    .run()
```

prints

```
INFO bevy_render::renderer: [important text!]
```

The main idea comes from @SonicZentropy and their pull request bevyengine#3778,
but it was stale since may. This has all the changes and a fix since now
`LogSettings` are included in the plugin and not in a resource.

Since plugin builders are stateless and not mutable (bevyengine#8101), the old
solution of using an `Option<Box<Layer>>` no longer works. One solution
to fix this is to use a `Box<Fn() -> Box<Layer>>`. I am unaware of
another more idiomatic fix, so if you have something better please
revise it. Should plugins builders be mutable in the future, this can be
changed back to the original approach.

---

## Changelog

- Adds field `layer` to `LogPlugin` that takes a function returning a
`tracing_subscriber` custom layer.
- Reexports `tracing_subscriber` from log so it can be used to configure
the plugin.
- Modifies the `logs.rs` and `system_piping.rs` examples to include the
new configuration.

## Migration Guide

No breaking changes
  • Loading branch information
eerii authored Apr 22, 2023
1 parent 6b774c0 commit 82f8e93
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
37 changes: 23 additions & 14 deletions crates/bevy_log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ pub use bevy_utils::tracing::{

use bevy_app::{App, Plugin};
use tracing_log::LogTracer;
pub use tracing_subscriber;
#[cfg(feature = "tracing-chrome")]
use tracing_subscriber::fmt::{format::DefaultFields, FormattedFields};
use tracing_subscriber::{prelude::*, registry::Registry, EnvFilter};
use tracing_subscriber::{prelude::*, registry::Registry, EnvFilter, Layer};

/// Adds logging to Apps. This plugin is part of the `DefaultPlugins`. Adding
/// this plugin will setup a collector appropriate to your target platform:
Expand All @@ -61,6 +62,7 @@ use tracing_subscriber::{prelude::*, registry::Registry, EnvFilter};
/// .add_plugins(DefaultPlugins.set(LogPlugin {
/// level: Level::DEBUG,
/// filter: "wgpu=error,bevy_render=info,bevy_ecs=trace".to_string(),
/// layer: Box::new(|| Box::new(tracing_subscriber::fmt::layer().pretty().without_time())),
/// }))
/// .run();
/// }
Expand Down Expand Up @@ -97,13 +99,22 @@ pub struct LogPlugin {
/// Filters out logs that are "less than" the given level.
/// This can be further filtered using the `filter` setting.
pub level: Level,

/// Allows user-defined tracing subscriber layer rather than default formatting.
/// See also: [`tracing_subscriber::fmt::Subscriber`]
pub layer:
Box<dyn (Fn() -> Box<dyn Layer<tracing_subscriber::Registry> + Send + Sync>) + Send + Sync>,
}

impl Default for LogPlugin {
fn default() -> Self {
Self {
filter: "wgpu=error".to_string(),
level: Level::INFO,
layer: Box::new(|| {
#[allow(clippy::box_default)]
Box::new(tracing_subscriber::fmt::Layer::default())
}),
}
}
}
Expand All @@ -122,10 +133,20 @@ impl Plugin for LogPlugin {

let finished_subscriber;
let default_filter = { format!("{},{}", self.level, self.filter) };
let fmt_layer = (self.layer)();

// bevy_render::renderer logs a `tracy.frame_mark` event every frame
// at Level::INFO. Formatted logs should omit it.
#[cfg(feature = "tracing-tracy")]
let fmt_layer = fmt_layer.with_filter(tracing_subscriber::filter::FilterFn::new(|meta| {
meta.fields().field("tracy.frame_mark").is_none()
}));

let filter_layer = EnvFilter::try_from_default_env()
.or_else(|_| EnvFilter::try_new(&default_filter))
.unwrap();
let subscriber = Registry::default().with(filter_layer);

let subscriber = Registry::default().with(fmt_layer).with(filter_layer);

#[cfg(feature = "trace")]
let subscriber = subscriber.with(tracing_error::ErrorLayer::default());
Expand Down Expand Up @@ -159,18 +180,6 @@ impl Plugin for LogPlugin {
#[cfg(feature = "tracing-tracy")]
let tracy_layer = tracing_tracy::TracyLayer::new();

let fmt_layer = tracing_subscriber::fmt::Layer::default();

// bevy_render::renderer logs a `tracy.frame_mark` event every frame
// at Level::INFO. Formatted logs should omit it.
#[cfg(feature = "tracing-tracy")]
let fmt_layer =
fmt_layer.with_filter(tracing_subscriber::filter::FilterFn::new(|meta| {
meta.fields().field("tracy.frame_mark").is_none()
}));

let subscriber = subscriber.with(fmt_layer);

#[cfg(feature = "tracing-chrome")]
let subscriber = subscriber.with(chrome_layer);
#[cfg(feature = "tracing-tracy")]
Expand Down
1 change: 1 addition & 0 deletions examples/app/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ fn main() {
// Uncomment this to override the default log settings:
// level: bevy::log::Level::TRACE,
// filter: "wgpu=warn,bevy_ecs=info".to_string(),
// layer: Box::new(|| Box::new(bevy::log::tracing_subscriber::fmt::layer().pretty().without_time())),
..default()
}))
.add_systems(Update, log_system)
Expand Down
1 change: 1 addition & 0 deletions examples/ecs/system_piping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fn main() {
.add_plugin(LogPlugin {
level: Level::TRACE,
filter: "".to_string(),
..default()
})
.add_systems(
Update,
Expand Down

0 comments on commit 82f8e93

Please sign in to comment.