Skip to content

feat(log): support customizing default log formatting #17722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 5, 2025

Conversation

JeanMertz
Copy link
Contributor

The LogPlugin now allows overriding the default
tracing_subscriber::fmt::Layer through a new fmt_layer option. This enables customization of the default log output format without having to replace the entire logging system.

For example, to disable timestamps in the log output:

fn fmt_layer(_app: &mut App) -> Option<bevy::log::BoxedFmtLayer> {
    Some(Box::new(
        bevy::log::tracing_subscriber::fmt::Layer::default()
            .without_time()
            .with_writer(std::io::stderr),
    ))
}

fn main() {
    App::new()
        .add_plugins(DefaultPlugins.set(bevy::log::LogPlugin {
            fmt_layer,
            ..default()
        }))
        .run();
}

This is different from the existing custom_layer option, because that option adds additional layers to the subscriber, but can't modify the default formatter layer (at least, not to my knowledge).

I almost always disable timestamps in my Bevy logs, and usually also tweak other default log formatting (such as with_span_events), which made it so that I always had to disable the default logger. This allows me to use everything the Bevy logger supports (including tracy support), while still formatting the default logs the way I like them.

The LogPlugin now allows overriding the default
`tracing_subscriber::fmt::Layer` through a new `fmt_layer` option. This
enables customization of the default log output format without having to
replace the entire logging system.

Signed-off-by: Jean Mertz <[email protected]>
Comment on lines +256 to +268
#[cfg(feature = "trace")]
type BaseSubscriber =
Layered<EnvFilter, Layered<Option<Box<dyn Layer<Registry> + Send + Sync>>, Registry>>;

#[cfg(feature = "trace")]
type PreFmtSubscriber = Layered<tracing_error::ErrorLayer<BaseSubscriber>, BaseSubscriber>;

#[cfg(not(feature = "trace"))]
type PreFmtSubscriber =
Layered<EnvFilter, Layered<Option<Box<dyn Layer<Registry> + Send + Sync>>, Registry>>;

/// A boxed [`Layer`] that can be used with [`LogPlugin::fmt_layer`].
pub type BoxedFmtLayer = Box<dyn Layer<PreFmtSubscriber> + Send + Sync + 'static>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't get this to work in a generic way, but if anyone can, I'd love to swap this out for something more maintainable (these type aliases would break if we changed ordering of existing layers or add additional layers).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ha...
This is due to

#[cfg(feature = "trace")]
let subscriber = subscriber.with(tracing_error::ErrorLayer::default());

?

Yes in general it's hard to get things working correctly with tracing

Copy link
Contributor Author

@JeanMertz JeanMertz Feb 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, I couldn't find a way to have a generic layer type definition that works for any stack of layers, and it has to work with certain layers included or removed, depending on the trace feature.

I've had my eye on https://github.com/fast/fastrace for a while actually (combined with https://github.com/fast/logcall and https://github.com/fast/logforth).

@ickshonpe ickshonpe added A-Diagnostics Logging, crash handling, error reporting and performance analysis C-Feature A new feature, making something new possible S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Feb 8, 2025
Copy link
Contributor

@cBournhonesque cBournhonesque left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this works for now but in general the custom_layer and fmt_layer feel a bit hacky; it would be nice to figure out some cleaner way to override/add extra layers

@JeanMertz
Copy link
Contributor Author

I think this works for now but in general the custom_layer and fmt_layer feel a bit hacky; it would be nice to figure out some cleaner way to override/add extra layers

I agree, but I would prefer that to be a follow-up PR.

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Apr 23, 2025
@alice-i-cecile alice-i-cecile added this pull request to the merge queue May 5, 2025
Merged via the queue into bevyengine:main with commit 3b24f52 May 5, 2025
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Diagnostics Logging, crash handling, error reporting and performance analysis C-Feature A new feature, making something new possible S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants