-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Conversation
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]>
#[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>; |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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).
Signed-off-by: Jean Mertz <[email protected]>
There was a problem hiding this 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
Signed-off-by: Jean Mertz <[email protected]>
I agree, but I would prefer that to be a follow-up PR. |
The LogPlugin now allows overriding the default
tracing_subscriber::fmt::Layer
through a newfmt_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:
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.