-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Log to file #5342
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
Log to file #5342
Conversation
I opened tokio-rs/tracing#2225 to fix the lack of suffix customisation in tracing-appender |
224e129
to
0ae3a0c
Compare
I found out it's possible to circumvent some of the type issues by adding an See: IceSentry#3 |
0417cb3
to
d050805
Compare
this will not work in wasm, and it should either be gated or mentioned in the docs |
2ba5958
to
ae4782f
Compare
The diff doesn't show it, but it's in the block that adds layers that don't work in wasm or android. I will add documentation explaining this, but do you also want the field on the struct to be gated? |
I'm not sure why I put it there actually. I don't see why it wouldn't work on android. I'll move it to a new gated section for not wasm |
Documentation should be good enough, something like bevy/crates/bevy_window/src/window.rs Lines 122 to 125 in 695d30b
|
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.
Seems like you accidentally committed the log
file, but otherwise it seems good to me :)
Made a PR to this branch to remove the extra log file: IceSentry#4 |
One caveat with this, is that it won't catch panics. That is if you add a panic to the logs example, you get:
I'm not sure if it is out of scope for this new API, or whether we should try to create a more high-level API, less coupled to file appender? |
Could you check how it works on Android? Aren't apps sandboxed, unless you require additional permissions? Is the log file in a reachable place? |
I think that's fine, if people want to log an error message before a panic they should do that. We could probably provide a panic_print!() or something like that I guess.
I'm not sure how to do that. I guess I should just keep it gated for now. |
That would be quite some boilerplate on Not really sure how this is commonly solved, maybe possible through a panic hook? Currently I'm wrapping my whole executable in a launcher process that pipes both stdout and stderr to files, mainly to catch panics, would be nice if I didn't have to. |
I would like to add that I'm using this PR in my project, and the solution I found to capture panics (which was the only thing the updated LogPlugin wasn't doing) to a file was indeed using a panic hook. If one adds this to the beginning of the
and then the
In this way, The launcher process solution is also interesting, and can be useful. |
bf553e3
to
b6de76a
Compare
I added a panic hook that is only enabled when using the file appender. After some quick testing it does seem to catch panics from systems correctly. |
cb0b5d0
to
94d686f
Compare
Co-authored-by: François <[email protected]>
Co-authored-by: François <[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.
Hi, I was asked to jump in and review this PR. It's my first Bevy review, so hopefully all is good.
The idea looks good, but I have left some suggestions on potential improvements. Hope they are helpful!
/// ## Platform-specific | ||
/// | ||
/// **`WASM`** does not support logging to a file. | ||
pub file_appender_settings: Option<FileAppenderSettings>, |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
let subscriber = subscriber.with(fmt_layer); | ||
let subscriber = subscriber.with(fmt_layer); | ||
|
||
#[cfg(feature = "tracing-chrome")] |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
crates/bevy_log/src/lib.rs
Outdated
} | ||
|
||
if settings.rolling == Rolling::Never && settings.prefix.is_empty() { | ||
panic!("Using the Rolling::Never variant with no prefix will result in an empty filename which is invalid"); |
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.
nit: add a comma before "which"
crates/bevy_log/src/lib.rs
Outdated
.with_ansi(false) | ||
.with_writer(non_blocking); | ||
Some(file_fmt_layer) | ||
} else { |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
let (non_blocking, worker_guard) = tracing_appender::non_blocking(file_appender); | ||
// WARN We need to keep this somewhere so it doesn't get dropped. | ||
// If it gets dropped then it will silently stop writing to the file | ||
app.insert_resource(FileAppenderWorkerGuard(worker_guard)); |
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.
This isn't fully robust as it will mean that logs still get lost after teardown of the current app. This would apply to logs generated later in the teardown, or perhaps in a multi-App
process if the App
with the logging configuration were to be destroyed but others were to continue.
Given that only one logger can ever be configured globally, perhaps it's okay to simply mem::forget
the guard? This applies equally to the existing guards elsewhere in the code.
} else { | ||
None | ||
}; | ||
let subscriber = subscriber.with(file_appender_layer); |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
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.
Iirc the subscriber changes type with each call, and is relatively unergonomic to use, which is why much of this code seems arbitrarily strange.
@@ -8,6 +8,8 @@ fn main() { | |||
// Uncomment this to override the default log settings: | |||
// level: bevy::log::Level::TRACE, | |||
// filter: "wgpu=warn,bevy_ecs=info".to_string(), | |||
// This will let you configure file logging |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
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.
This will generate artifacts which is not ideal and examples shouldn't have to clean up after themselves.
#[derive(Resource)] | ||
struct FileAppenderWorkerGuard(tracing_appender::non_blocking::WorkerGuard); | ||
|
||
/// Settings to control how to log to a file |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
#[cfg(feature = "tracing-chrome")] | ||
let chrome_layer = { | ||
let mut layer = tracing_chrome::ChromeLayerBuilder::new(); | ||
if let Ok(path) = std::env::var("TRACE_CHROME") { |
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.
If I understand correctly, this PR will leave things in a situation where the Chrome tracing is controlled purely by environment variable, and the file logging purely by code. It might be good to unify both of these paths and allow control of both variations by either method, with environment variables taking priority.
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'd rather leave this to be fixed in a future PR but I agree that support for ENV controlled file logging would be good.
I have way too much going on right now and don't have the time to update this PR. If anyone is interested in picking up this work feel free to pick this up. |
Objective
Solution
Notes
For reasons I don't know, tracing-appender didn't support custom suffix. This means it's impossible to log to a file with a
.log
extension. I submitted a fix that was merged to add support for it, but there has been no release of tracing-appender in almost a yearCloses: #5233