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

Disable stdout logging for release builds #47

Merged
merged 2 commits into from
Jun 16, 2022

Conversation

ThomasFrans
Copy link
Contributor

  • disable the logo on stdout in release builds
  • disable the log messages on stdout in release builds

- disable the logo on stdout in release builds
- disable the log messages on stdout in release builds
@ThomasFrans
Copy link
Contributor Author

This may be more of a personal preference. Maybe it's not wanted in which case, please ignore this request 🙂 But it's nice not to have unnecessary output to stdout in release builds (it's a pretty valid point in the Unix philosophy).

The debug build can still have output, but it would be better to put release logs in the log file (which also shouldn't be put in the current working directory, but that's an issue for another time 😄 ).

@Builditluc
Copy link
Owner

Thank you for your PR!

Yes, you are completely right, it's nice not to have unnecessary output in stdout in release builds but I've noticed that this will remove the logging completely (also the logging into the log file).

Because the logging gets configured within the config, we have two loggers configured within logging.rs. One works without the config loaded, this logs to stdout and the other one uses the config, this logs to the log file. To disable the stdout logging we just have to set the log level in release builds to Off in the logger that logs to stdout. With this, the log file still gets filled (which is useful when a crash occurs).

To change the directory where the log file is saved, we can just give the logger another directory (currently we use the current working directory). So we could modify the config option log_dir to be paths relative to the home directory. This way, something like

log_dir = ".config/wiki-tui/wiki_tui.log"

would save the log file in the same directory as the config file. Would this way of changing the log directory be better?

Copy link
Owner

@Builditluc Builditluc left a comment

Choose a reason for hiding this comment

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

To disable the logging to stdout in the release build we can change the following things in logging.rs:

diff --git a/src/logging.rs b/src/logging.rs
index 491c3ad..67953aa 100644
--- a/src/logging.rs
+++ b/src/logging.rs
@@ -15,9 +15,15 @@ impl Logger {
         let default_config = Config::builder()
             .appender(Appender::builder().build("wiki_tui", Box::new(wiki_tui)))
             .build(
+                #[cfg(debug_assertions)]
                 Root::builder()
                     .appender("wiki_tui")
                     .build(log::LevelFilter::Info),
+
+                #[cfg(not(debug_assertions))]
+                Root::builder()
+                    .appender("wiki_tui")
+                    .build(log::LevelFilter::Off),
             )
             .unwrap();

This will disable only the stdout logger in release builds

Cargo.toml Outdated
@@ -22,7 +22,7 @@ blt-backend = ["cursive/blt-backend"]
[dependencies]
syn = "=1.0.57"
serde_json = "1.0.61"
log = "0.4.13"
log = { version = "0.4.13", features = ["release_max_level_off"]}
Copy link
Owner

Choose a reason for hiding this comment

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

Here we should remove the "release_max_level_off" feature because this disables logging completely (we need that for crash reports)

@Builditluc Builditluc added the bug label Jun 16, 2022
@Builditluc Builditluc changed the title fix(logging): disable logging for release builds Disable logging for release builds Jun 16, 2022
@ThomasFrans
Copy link
Contributor Author

ThomasFrans commented Jun 16, 2022

Thank you for your PR!

Yes, you are completely right, it's nice not to have unnecessary output in stdout in release builds but I've noticed that this will remove the logging completely (also the logging into the log file).

Because the logging gets configured within the config, we have two loggers configured within logging.rs. One works without the config loaded, this logs to stdout and the other one uses the config, this logs to the log file. To disable the stdout logging we just have to set the log level in release builds to Off in the logger that logs to stdout. With this, the log file still gets filled (which is useful when a crash occurs).

To change the directory where the log file is saved, we can just give the logger another directory (currently we use the current working directory). So we could modify the config option log_dir to be paths relative to the home directory. This way, something like

log_dir = ".config/wiki-tui/wiki_tui.log"

would save the log file in the same directory as the config file. Would this way of changing the log directory be better?

I'll try to make the changes you suggested here. One detail about the recommendation for the log dir, it should probably go in the cache directory, .config is the XDG_USER_CONFIG directory, used for configuration files only. There are some crates on crates.io that provide the standard directories on different platforms, maybe those could be used? https://crates.io/crates/directories

@Builditluc
Copy link
Owner

it should probably go in the cache directory, .config is the XDG_USER_CONFIG directory, used for configuration files only. There are some crates on crates.io that provide the standard directories on different platforms, maybe those could be used? crates.io/crates/directories

Yep, that makes sense. We are already using a crate that provides the standard directories on different platforms (dirs) so it wouldn't be much of a hassle to use it to get the cache directory. I will make a PR that moves the log file in a few hours

- disable logging to stdout
- keep logging to the log file for release builds
Copy link
Owner

@Builditluc Builditluc left a comment

Choose a reason for hiding this comment

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

Looks good, thank you! (I will let GitHub finish the running actions, then merge the PR)

@Builditluc Builditluc changed the title Disable logging for release builds Disable stdout logging for release builds Jun 16, 2022
@Builditluc Builditluc merged commit 62e1324 into Builditluc:main Jun 16, 2022
@ThomasFrans ThomasFrans deleted the quiet-release branch September 18, 2022 14:20
@Builditluc Builditluc added state: approved This issue or pull request was approved and can be worked on type: bug This fixes a bug. Increment the minor version labels Feb 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: approved This issue or pull request was approved and can be worked on type: bug This fixes a bug. Increment the minor version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants