Skip to content

Commit e3fb23d

Browse files
committed
add documentation on LogPlugin and more log usage (#1973)
Fixes #1895 Changed most `println` to `info` in examples, some to `warn` when it was useful to differentiate from other more noisy logs. Added doc on `LogPlugin`, how to configure it, and why (and how) you may need to disable it
1 parent 6508b4e commit e3fb23d

27 files changed

+173
-78
lines changed
Lines changed: 72 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,107 @@
11
use bevy_app::{PluginGroup, PluginGroupBuilder};
22

3+
use bevy_app::ScheduleRunnerPlugin;
4+
use bevy_asset::AssetPlugin;
5+
#[cfg(feature = "bevy_audio")]
6+
use bevy_audio::AudioPlugin;
7+
use bevy_core::CorePlugin;
8+
use bevy_diagnostic::DiagnosticsPlugin;
9+
#[cfg(feature = "bevy_gilrs")]
10+
use bevy_gilrs::GilrsPlugin;
11+
#[cfg(feature = "bevy_gltf")]
12+
use bevy_gltf::GltfPlugin;
13+
use bevy_input::InputPlugin;
14+
use bevy_log::LogPlugin;
15+
#[cfg(feature = "bevy_pbr")]
16+
use bevy_pbr::PbrPlugin;
17+
#[cfg(feature = "bevy_render")]
18+
use bevy_render::RenderPlugin;
19+
use bevy_scene::ScenePlugin;
20+
#[cfg(feature = "bevy_sprite")]
21+
use bevy_sprite::SpritePlugin;
22+
#[cfg(feature = "bevy_text")]
23+
use bevy_text::TextPlugin;
24+
use bevy_transform::TransformPlugin;
25+
#[cfg(feature = "bevy_ui")]
26+
use bevy_ui::UiPlugin;
27+
#[cfg(feature = "bevy_wgpu")]
28+
use bevy_wgpu::WgpuPlugin;
29+
use bevy_window::WindowPlugin;
30+
#[cfg(feature = "bevy_winit")]
31+
use bevy_winit::WinitPlugin;
32+
33+
/// This plugin group will add all the default plugins:
34+
/// * [`LogPlugin`]
35+
/// * [`CorePlugin`]
36+
/// * [`TransformPlugin`]
37+
/// * [`DiagnosticsPlugin`]
38+
/// * [`InputPlugin`]
39+
/// * [`WindowPlugin`]
40+
/// * [`AssetPlugin`]
41+
/// * [`ScenePlugin`]
42+
/// * [`RenderPlugin`] - with feature `bevy_render`
43+
/// * [`SpritePlugin`] - with feature `bevy_sprite`
44+
/// * [`PbrPlugin`] - with feature `bevy_pbr`
45+
/// * [`UiPlugin`] - with feature `bevy_ui`
46+
/// * [`TextPlugin`] - with feature `bevy_text`
47+
/// * [`AudioPlugin`] - with feature `bevy_audio`
48+
/// * [`GilrsPlugin`] - with feature `bevy_gilrs`
49+
/// * [`GltfPlugin`] - with feature `bevy_gltf`
50+
/// * [`WinitPlugin`] - with feature `bevy_winit`
51+
/// * [`WgpuPlugin`] - with feature `bevy_wgpu`
352
pub struct DefaultPlugins;
453

554
impl PluginGroup for DefaultPlugins {
655
fn build(&mut self, group: &mut PluginGroupBuilder) {
7-
group.add(bevy_log::LogPlugin::default());
8-
group.add(bevy_core::CorePlugin::default());
9-
group.add(bevy_transform::TransformPlugin::default());
10-
group.add(bevy_diagnostic::DiagnosticsPlugin::default());
11-
group.add(bevy_input::InputPlugin::default());
12-
group.add(bevy_window::WindowPlugin::default());
13-
group.add(bevy_asset::AssetPlugin::default());
14-
group.add(bevy_scene::ScenePlugin::default());
56+
group.add(LogPlugin::default());
57+
group.add(CorePlugin::default());
58+
group.add(TransformPlugin::default());
59+
group.add(DiagnosticsPlugin::default());
60+
group.add(InputPlugin::default());
61+
group.add(WindowPlugin::default());
62+
group.add(AssetPlugin::default());
63+
group.add(ScenePlugin::default());
1564

1665
#[cfg(feature = "bevy_render")]
17-
group.add(bevy_render::RenderPlugin::default());
66+
group.add(RenderPlugin::default());
1867

1968
#[cfg(feature = "bevy_sprite")]
20-
group.add(bevy_sprite::SpritePlugin::default());
69+
group.add(SpritePlugin::default());
2170

2271
#[cfg(feature = "bevy_pbr")]
23-
group.add(bevy_pbr::PbrPlugin::default());
72+
group.add(PbrPlugin::default());
2473

2574
#[cfg(feature = "bevy_ui")]
26-
group.add(bevy_ui::UiPlugin::default());
75+
group.add(UiPlugin::default());
2776

2877
#[cfg(feature = "bevy_text")]
29-
group.add(bevy_text::TextPlugin::default());
78+
group.add(TextPlugin::default());
3079

3180
#[cfg(feature = "bevy_audio")]
32-
group.add(bevy_audio::AudioPlugin::default());
81+
group.add(AudioPlugin::default());
3382

3483
#[cfg(feature = "bevy_gilrs")]
35-
group.add(bevy_gilrs::GilrsPlugin::default());
84+
group.add(GilrsPlugin::default());
3685

3786
#[cfg(feature = "bevy_gltf")]
38-
group.add(bevy_gltf::GltfPlugin::default());
87+
group.add(GltfPlugin::default());
3988

4089
#[cfg(feature = "bevy_winit")]
41-
group.add(bevy_winit::WinitPlugin::default());
90+
group.add(WinitPlugin::default());
4291

4392
#[cfg(feature = "bevy_wgpu")]
44-
group.add(bevy_wgpu::WgpuPlugin::default());
93+
group.add(WgpuPlugin::default());
4594
}
4695
}
4796

97+
/// Minimal plugin group that will add the following plugins:
98+
/// * [`CorePlugin`]
99+
/// * [`ScheduleRunnerPlugin`]
48100
pub struct MinimalPlugins;
49101

50102
impl PluginGroup for MinimalPlugins {
51103
fn build(&mut self, group: &mut PluginGroupBuilder) {
52-
group.add(bevy_core::CorePlugin::default());
53-
group.add(bevy_app::ScheduleRunnerPlugin::default());
104+
group.add(CorePlugin::default());
105+
group.add(ScheduleRunnerPlugin::default());
54106
}
55107
}

crates/bevy_log/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ android_log-sys = "0.2.0"
2626
[target.'cfg(target_arch = "wasm32")'.dependencies]
2727
console_error_panic_hook = "0.1.6"
2828
tracing-wasm = "0.2"
29+
30+
[dev-dependencies]
31+
bevy_internal = { path = "../bevy_internal", version = "0.5.0" }

crates/bevy_log/src/lib.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,53 @@ use bevy_app::{AppBuilder, Plugin};
1616
use tracing_subscriber::fmt::{format::DefaultFields, FormattedFields};
1717
use tracing_subscriber::{prelude::*, registry::Registry, EnvFilter};
1818

19-
/// Adds logging to Apps.
19+
/// Adds logging to Apps. This plugin is part of the `DefaultPlugins`. Adding
20+
/// this plugin will setup a collector appropriate to your target platform:
21+
/// * Using [`tracing-subscriber`](https://crates.io/crates/tracing-subscriber) by default,
22+
/// logging to `stdout`.
23+
/// * Using [`android_log-sys`](https://crates.io/crates/android_log-sys) on Android,
24+
/// logging to Android logs.
25+
/// * Using [`tracing-wasm`](https://crates.io/crates/tracing-wasm) in WASM, logging
26+
/// to the browser console.
27+
///
28+
/// You can configure this plugin using the resource [`LogSettings`].
29+
/// ```no_run
30+
/// # use bevy_internal::DefaultPlugins;
31+
/// # use bevy_app::App;
32+
/// # use bevy_log::LogSettings;
33+
/// # use bevy_utils::tracing::Level;
34+
/// fn main() {
35+
/// App::build()
36+
/// .insert_resource(LogSettings {
37+
/// level: Level::DEBUG,
38+
/// filter: "wgpu=error,bevy_render=info".to_string(),
39+
/// })
40+
/// .add_plugins(DefaultPlugins)
41+
/// .run();
42+
/// }
43+
/// ```
44+
///
45+
/// Log level can also be changed using the `RUST_LOG` environment variable.
46+
/// It has the same syntax has the field [`LogSettings::filter`], see [`EnvFilter`].
47+
///
48+
/// If you want to setup your own tracing collector, you should disable this
49+
/// plugin from `DefaultPlugins` with [`AppBuilder::add_plugins_with`]:
50+
/// ```no_run
51+
/// # use bevy_internal::DefaultPlugins;
52+
/// # use bevy_app::App;
53+
/// # use bevy_log::LogPlugin;
54+
/// fn main() {
55+
/// App::build()
56+
/// .add_plugins_with(DefaultPlugins, |group| group.disable::<LogPlugin>())
57+
/// .run();
58+
/// }
59+
/// ```
2060
#[derive(Default)]
2161
pub struct LogPlugin;
2262

2363
/// LogPlugin settings
2464
pub struct LogSettings {
25-
/// Filters logs using the [EnvFilter] format
65+
/// Filters logs using the [`EnvFilter`] format
2666
pub filter: String,
2767

2868
/// Filters out logs that are "less than" the given level.

examples/2d/many_sprites.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fn tick(time: Res<Time>, sprites: Query<&Sprite>, mut query: Query<&mut PrintTim
9090
timer.0.tick(time.delta());
9191

9292
if timer.0.just_finished() {
93-
println!("Sprites: {}", sprites.iter().count(),);
93+
info!("Sprites: {}", sprites.iter().count(),);
9494
}
9595
}
9696
}

examples/app/drag_and_drop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ fn main() {
99

1010
fn file_drag_and_drop_system(mut events: EventReader<FileDragAndDrop>) {
1111
for event in events.iter() {
12-
println!("{:?}", event);
12+
info!("{:?}", event);
1313
}
1414
}

examples/app/plugin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ struct PrintMessageState {
4040

4141
fn print_message_system(mut state: ResMut<PrintMessageState>, time: Res<Time>) {
4242
if state.timer.tick(time.delta()).finished() {
43-
println!("{}", state.message);
43+
info!("{}", state.message);
4444
}
4545
}

examples/app/plugin_group.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl Plugin for PrintHelloPlugin {
3434
}
3535

3636
fn print_hello_system() {
37-
println!("hello");
37+
info!("hello");
3838
}
3939

4040
pub struct PrintWorldPlugin;
@@ -46,5 +46,5 @@ impl Plugin for PrintWorldPlugin {
4646
}
4747

4848
fn print_world_system() {
49-
println!("world");
49+
info!("world");
5050
}

examples/asset/asset_loading.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ fn setup(
3131
// You might notice that this doesn't run! This is because assets load in parallel without
3232
// blocking. When an asset has loaded, it will appear in relevant Assets<T>
3333
// collection.
34-
println!("{:?}", sphere.primitive_topology());
34+
info!("{:?}", sphere.primitive_topology());
3535
} else {
36-
println!("sphere hasn't loaded yet");
36+
info!("sphere hasn't loaded yet");
3737
}
3838

3939
// You can load all assets in a folder like this. They will be loaded in parallel without

examples/asset/custom_asset.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ fn print_on_load(mut state: ResMut<State>, custom_assets: ResMut<Assets<CustomAs
6060
return;
6161
}
6262

63-
println!("Custom asset loaded: {:?}", custom_asset.unwrap());
63+
info!("Custom asset loaded: {:?}", custom_asset.unwrap());
6464
state.printed = true;
6565
}

examples/asset/custom_asset_io.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,30 @@ struct CustomAssetIo(Box<dyn AssetIo>);
1414

1515
impl AssetIo for CustomAssetIo {
1616
fn load_path<'a>(&'a self, path: &'a Path) -> BoxedFuture<'a, Result<Vec<u8>, AssetIoError>> {
17-
println!("load_path({:?})", path);
17+
info!("load_path({:?})", path);
1818
self.0.load_path(path)
1919
}
2020

2121
fn read_directory(
2222
&self,
2323
path: &Path,
2424
) -> Result<Box<dyn Iterator<Item = PathBuf>>, AssetIoError> {
25-
println!("read_directory({:?})", path);
25+
info!("read_directory({:?})", path);
2626
self.0.read_directory(path)
2727
}
2828

2929
fn is_directory(&self, path: &Path) -> bool {
30-
println!("is_directory({:?})", path);
30+
info!("is_directory({:?})", path);
3131
self.0.is_directory(path)
3232
}
3333

3434
fn watch_path_for_changes(&self, path: &Path) -> Result<(), AssetIoError> {
35-
println!("watch_path_for_changes({:?})", path);
35+
info!("watch_path_for_changes({:?})", path);
3636
self.0.watch_path_for_changes(path)
3737
}
3838

3939
fn watch_for_changes(&self) -> Result<(), AssetIoError> {
40-
println!("watch_for_changes()");
40+
info!("watch_for_changes()");
4141
self.0.watch_for_changes()
4242
}
4343
}

0 commit comments

Comments
 (0)