logging/
lib.rs

1#![doc = include_str!("../README.md")]
2
3use bevy::log::tracing_subscriber::Layer;
4use bevy::log::tracing_subscriber::fmt::layer;
5use bevy::log::{BoxedLayer, DEFAULT_FILTER, Level, LogPlugin};
6use bevy::prelude::App;
7use config::LogConfiguration;
8use std::str::FromStr;
9use tracing_appender::rolling::daily;
10
11/// Builds the [`LogPlugin`] for the application.
12#[must_use]
13pub fn log_plugin(config: &LogConfiguration) -> LogPlugin {
14    LogPlugin {
15        filter: format!("{DEFAULT_FILTER},{}", config.filter),
16        level: Level::from_str(config.level.as_str()).unwrap_or(Level::INFO),
17        custom_layer,
18    }
19}
20
21/// This method builds a custom logging layer for `trace` and `log`.
22///
23/// This allows us to configure how the software should log, where and so forth.
24#[allow(clippy::unnecessary_wraps)]
25fn custom_layer(_app: &mut App) -> Option<BoxedLayer> {
26    #[cfg(not(feature = "dev"))]
27    return None;
28
29    #[allow(
30        unreachable_code,
31        reason = "Only unreachable in dev, release mode will have this on"
32    )]
33    Some(Box::new(vec![
34        layer()
35            .with_file(false)
36            .with_thread_names(true)
37            .with_thread_ids(true)
38            .with_level(true)
39            .json()
40            .with_writer(daily("logs", "dungeonrs"))
41            .boxed(),
42    ]))
43}