From 5f75847beba4c46bb9a83e5cb05b7d417df29ac8 Mon Sep 17 00:00:00 2001 From: Charles Bournhonesque Date: Thu, 30 Nov 2023 21:30:10 -0500 Subject: [PATCH 1/5] add option to add layers to log subscriber --- Cargo.toml | 10 ++++++++ crates/bevy_log/src/lib.rs | 17 ++++++++++++- examples/app/log_layers.rs | 50 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 examples/app/log_layers.rs diff --git a/Cargo.toml b/Cargo.toml index cc9c26056daa9..dce36875222c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1013,6 +1013,16 @@ description = "Illustrate how to use generate log output" category = "Application" wasm = true +[[example]] +name = "log_layers" +path = "examples/app/log_layers.rs" + +[package.metadata.example.log_layers] +name = "Log layers" +description = "Illustrate how to add custom log layers" +category = "Application" +wasm = false + [[example]] name = "plugin" path = "examples/app/plugin.rs" diff --git a/crates/bevy_log/src/lib.rs b/crates/bevy_log/src/lib.rs index cc8d3e7a47100..ba41e895be873 100644 --- a/crates/bevy_log/src/lib.rs +++ b/crates/bevy_log/src/lib.rs @@ -34,12 +34,15 @@ pub use bevy_utils::tracing::{ debug, debug_span, error, error_span, info, info_span, trace, trace_span, warn, warn_span, Level, }; +pub use tracing_subscriber; + use bevy_app::{App, Plugin}; use tracing_log::LogTracer; #[cfg(feature = "tracing-chrome")] use tracing_subscriber::fmt::{format::DefaultFields, FormattedFields}; use tracing_subscriber::{prelude::*, registry::Registry, EnvFilter}; +use bevy_utils::tracing::Subscriber; /// Adds logging to Apps. This plugin is part of the `DefaultPlugins`. Adding /// this plugin will setup a collector appropriate to your target platform: @@ -60,6 +63,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(), +/// update_subscriber: None, /// })) /// .run(); /// } @@ -96,13 +100,20 @@ 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, + + /// Optionally apply extra transformations to the tracing subscriber. + /// For example add [`Layers`](tracing_subscriber::layer::Layer) + pub update_subscriber: Option BoxedSubscriber> } +pub type BoxedSubscriber = Box; + impl Default for LogPlugin { fn default() -> Self { Self { filter: "wgpu=error,naga=warn".to_string(), level: Level::INFO, + update_subscriber: None, } } } @@ -175,7 +186,11 @@ impl Plugin for LogPlugin { #[cfg(feature = "tracing-tracy")] let subscriber = subscriber.with(tracy_layer); - finished_subscriber = subscriber; + if let Some(update_subscriber) = self.update_subscriber { + finished_subscriber = update_subscriber(Box::new(subscriber)); + } else { + finished_subscriber = Box::new(subscriber); + } } #[cfg(target_arch = "wasm32")] diff --git a/examples/app/log_layers.rs b/examples/app/log_layers.rs new file mode 100644 index 0000000000000..91aa842f513f5 --- /dev/null +++ b/examples/app/log_layers.rs @@ -0,0 +1,50 @@ +//! This example illustrates how to add custom log layers in bevy. + +use bevy::{ + log::tracing_subscriber::{Layer, layer::SubscriberExt}, + prelude::*, + utils::tracing::{Subscriber}, +}; +use bevy_internal::log::{BoxedSubscriber}; + +struct CustomLayer; + +impl Layer 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"); +} + From e28224f9e915b936c7cb229a7dea08b2d7ad3ee6 Mon Sep 17 00:00:00 2001 From: Charles Bournhonesque Date: Thu, 30 Nov 2023 21:45:19 -0500 Subject: [PATCH 2/5] fix ci --- crates/bevy_log/src/lib.rs | 1 + examples/README.md | 1 + examples/app/log_layers.rs | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/bevy_log/src/lib.rs b/crates/bevy_log/src/lib.rs index ba41e895be873..7ecf366977797 100644 --- a/crates/bevy_log/src/lib.rs +++ b/crates/bevy_log/src/lib.rs @@ -106,6 +106,7 @@ pub struct LogPlugin { pub update_subscriber: Option BoxedSubscriber> } +/// Alias for a boxed [`tracing_subscriber::Subscriber`]. pub type BoxedSubscriber = Box; impl Default for LogPlugin { diff --git a/examples/README.md b/examples/README.md index 0450ef88159b8..dae8bcc588695 100644 --- a/examples/README.md +++ b/examples/README.md @@ -169,6 +169,7 @@ Example | Description [Empty](../examples/app/empty.rs) | An empty application (does nothing) [Empty with Defaults](../examples/app/empty_defaults.rs) | An empty application with default plugins [Headless](../examples/app/headless.rs) | An application that runs without default plugins +[Log layers](../examples/app/log_layers.rs) | Illustrate how to add custom log layers [Logs](../examples/app/logs.rs) | Illustrate how to use generate log output [No Renderer](../examples/app/no_renderer.rs) | An application that runs with default plugins and displays an empty window, but without an actual renderer [Plugin](../examples/app/plugin.rs) | Demonstrates the creation and registration of a custom plugin diff --git a/examples/app/log_layers.rs b/examples/app/log_layers.rs index 91aa842f513f5..1ba9fae68c061 100644 --- a/examples/app/log_layers.rs +++ b/examples/app/log_layers.rs @@ -1,11 +1,11 @@ //! This example illustrates how to add custom log layers in bevy. use bevy::{ + log::BoxedSubscriber, log::tracing_subscriber::{Layer, layer::SubscriberExt}, prelude::*, utils::tracing::{Subscriber}, }; -use bevy_internal::log::{BoxedSubscriber}; struct CustomLayer; From 6de4bb9cc9a4599972b6c637dc1d2fcf427c0883 Mon Sep 17 00:00:00 2001 From: Charles Bournhonesque Date: Thu, 30 Nov 2023 21:48:32 -0500 Subject: [PATCH 3/5] fmt --- crates/bevy_log/src/lib.rs | 5 ++--- examples/app/log_layers.rs | 7 ++----- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/crates/bevy_log/src/lib.rs b/crates/bevy_log/src/lib.rs index 7ecf366977797..d4812ceb4a35d 100644 --- a/crates/bevy_log/src/lib.rs +++ b/crates/bevy_log/src/lib.rs @@ -36,13 +36,12 @@ pub use bevy_utils::tracing::{ }; pub use tracing_subscriber; - use bevy_app::{App, Plugin}; +use bevy_utils::tracing::Subscriber; use tracing_log::LogTracer; #[cfg(feature = "tracing-chrome")] use tracing_subscriber::fmt::{format::DefaultFields, FormattedFields}; use tracing_subscriber::{prelude::*, registry::Registry, EnvFilter}; -use bevy_utils::tracing::Subscriber; /// Adds logging to Apps. This plugin is part of the `DefaultPlugins`. Adding /// this plugin will setup a collector appropriate to your target platform: @@ -103,7 +102,7 @@ pub struct LogPlugin { /// Optionally apply extra transformations to the tracing subscriber. /// For example add [`Layers`](tracing_subscriber::layer::Layer) - pub update_subscriber: Option BoxedSubscriber> + pub update_subscriber: Option BoxedSubscriber>, } /// Alias for a boxed [`tracing_subscriber::Subscriber`]. diff --git a/examples/app/log_layers.rs b/examples/app/log_layers.rs index 1ba9fae68c061..144fac28bd741 100644 --- a/examples/app/log_layers.rs +++ b/examples/app/log_layers.rs @@ -1,10 +1,10 @@ //! This example illustrates how to add custom log layers in bevy. use bevy::{ + log::tracing_subscriber::{layer::SubscriberExt, Layer}, log::BoxedSubscriber, - log::tracing_subscriber::{Layer, layer::SubscriberExt}, prelude::*, - utils::tracing::{Subscriber}, + utils::tracing::Subscriber, }; struct CustomLayer; @@ -36,8 +36,6 @@ fn main() { .run(); } - - fn log_system() { // here is how you write new logs at each "log level" (in "most import" to // "least important" order) @@ -47,4 +45,3 @@ fn log_system() { debug!("helpful for debugging"); trace!("very noisy"); } - From 079e9d0cce18e12c7b1d197577aac311f30fe2f3 Mon Sep 17 00:00:00 2001 From: Charles Bournhonesque Date: Thu, 30 Nov 2023 21:52:54 -0500 Subject: [PATCH 4/5] fix example --- examples/ecs/system_piping.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/ecs/system_piping.rs b/examples/ecs/system_piping.rs index 7cd5f2c1bd475..63579a2b61073 100644 --- a/examples/ecs/system_piping.rs +++ b/examples/ecs/system_piping.rs @@ -14,6 +14,7 @@ fn main() { .add_plugins(LogPlugin { level: Level::TRACE, filter: "".to_string(), + ..default() }) .add_systems( Update, From c0ffc52c09ff293983c3f8a5ebec8904ed49545b Mon Sep 17 00:00:00 2001 From: Charles Bournhonesque Date: Fri, 1 Dec 2023 16:12:51 -0500 Subject: [PATCH 5/5] fix docstring --- crates/bevy_log/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_log/src/lib.rs b/crates/bevy_log/src/lib.rs index d4812ceb4a35d..a369e02c47b77 100644 --- a/crates/bevy_log/src/lib.rs +++ b/crates/bevy_log/src/lib.rs @@ -105,7 +105,7 @@ pub struct LogPlugin { pub update_subscriber: Option BoxedSubscriber>, } -/// Alias for a boxed [`tracing_subscriber::Subscriber`]. +/// Alias for a boxed [`Subscriber`]. pub type BoxedSubscriber = Box; impl Default for LogPlugin {