Skip to content
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

Bevy Log Output to Files #5233

Open
samcarey opened this issue Jul 6, 2022 · 6 comments
Open

Bevy Log Output to Files #5233

samcarey opened this issue Jul 6, 2022 · 6 comments
Labels
A-Diagnostics Logging, crash handling, error reporting and performance analysis C-Docs An addition or correction to our documentation C-Usability A targeted quality-of-life change that makes Bevy easier to use

Comments

@samcarey
Copy link

samcarey commented Jul 6, 2022

What problem does this solve or what need does it fill?

Bevy log can output to console, but I don't know of a way to easily output this to the filesystem as well.
It seems to me that this would be a common use case, where an app could log what happens to a file for later inspection (especially if it crashes for some reason).

What solution would you like?

I'm imagining an interface something like this:

// Either use a RollingFileAppender that you can configure yourself, or just simply output to some path...
// Maybe someone can think of better options...
enum FileLogger {
    Rolling(RollingFileAppender),
    Path(PathBuf),
}

app.insert_resource(LogSettings {
    ...
    file: vec![
        "log1".into(),
        tracing_appender::rolling::hourly("logs", "log_prefix").into(),
    ]
});

where log output would be duplicated in the console and all FileLogger configurations.

What alternative(s) have you considered?

I currently have a working workaround in an app where I disabled the LogPlugin, and substituted my own that's exactly the same except that I replaced the let subscriber... line with:

let file_appender = tracing_appender::rolling::hourly("logs", "log"); // This should be user configurable
let (non_blocking, worker_guard) = tracing_appender::non_blocking(file_appender);
let file_fmt_layer = tracing_subscriber::fmt::Layer::default()
    .with_ansi(false) // disable terminal color escape sequences 
    .with_writer(non_blocking);
app.insert_resource(worker_guard); // have to keep this from being dropped

let subscriber = subscriber.with(fmt_layer).with(file_fmt_layer);

So it'd be nice if this were built in so I don't have to duplicate the LogPlugin code.

@samcarey samcarey added C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled labels Jul 6, 2022
@alice-i-cecile alice-i-cecile added C-Docs An addition or correction to our documentation C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Diagnostics Logging, crash handling, error reporting and performance analysis and removed C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled labels Jul 6, 2022
@djeedai
Copy link
Contributor

djeedai commented Jul 10, 2022

Since Bevy delegates logging to the tracing and tracing-subscriber crates (at least, on most platforms), shouldn't implementing tracing sinks/subscribers best left to user code, and instead Bevy to expose what's necessary for users to do so?

It seems that implementing file logging internally in Bevy is opening the door to more asks in the same vein (logging to networked file, logging to remote networked host, logging to syslogd, etc.), with all the variants/options each user may want for their app. Not to mention the extra per-platform work.

I'm not disagreeing file logging is needed (I agree that's important to have), but rather how we get there. Instead of the proposed approach, can we rather expose what's necessary for you @samcarey or someone else to simplify writing a file output customized to your use case? What is LogPlugin missing or what is it doing that forced you to avoid/replace it? Is it a matter of exposing the underlying tracing object(s)?

@samcarey
Copy link
Author

Exposing what's necessary would work for me!

In order to factor out my file-logging-specific code, I could imagine having a field on the LogSettings struct like extra_layers: Vec<tracing_subscriber::fmt::fmt_layer::Layer>.
Then the LogPlugin could do something like

for layer in settings.extra_layers.iter() {
    subscriber = subscriber.with(layer);
}

Would that interface be general enough to cover most other cases like you mentioned?

@IceSentry IceSentry mentioned this issue Jul 17, 2022
@brandon-reinhart
Copy link
Contributor

brandon-reinhart commented Jul 26, 2022

I would like to be able to add arbitrary layers to Bevy's tracing subscriber. While logging to files is nice, I would also like to direct trace messages to the console, for example.

@brandon-reinhart
Copy link
Contributor

An alternative might be to raise an event on trace.

@DasEtwas
Copy link

I'm also interested!

johanhelsing added a commit to johanhelsing/bevy that referenced this issue Mar 10, 2023
Add a dependency on tracing-appender

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 year

Closes: bevyengine#5233
johanhelsing added a commit to johanhelsing/bevy that referenced this issue Mar 10, 2023
Add a dependency on tracing-appender

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 year

Closes: bevyengine#5233
johanhelsing added a commit to johanhelsing/bevy that referenced this issue Apr 16, 2023
Add a dependency on tracing-appender

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 year

Closes: bevyengine#5233
@james7132
Copy link
Member

This isn't wholly complete just yet, but the ability to customize the subscriber (added in #10822) seems to allow for this.

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-Docs An addition or correction to our documentation C-Usability A targeted quality-of-life change that makes Bevy easier to use
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants