From 0ec5e389b06b9160c063259242febaa90224cecb Mon Sep 17 00:00:00 2001 From: Andy Goetz Date: Sun, 6 Aug 2023 20:15:35 -0700 Subject: [PATCH] Do not log to stderr by default Jolly no longer logs to stderr to default. This cleans up the default text that appears when running Jolly from the terminal. --- docs/config.md | 24 +++++++++++++----------- src/log.rs | 39 ++++++++++++++++++++++++++------------- 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/docs/config.md b/docs/config.md index 0cbb636..7f57adc 100644 --- a/docs/config.md +++ b/docs/config.md @@ -277,22 +277,21 @@ As a general rule, the jolly build script will warn if the # [config.log] The `[config.log]` table contains settings that control error logging -and debugging of Jolly. By default, Jolly will display error messages -in the UI and print them to `stderr`, but this behavior can be -customized. +and debugging of Jolly. By default, Jolly does not perform any +logging, but this behavior can be customized. **Important Note** *When you are trying to troubleshoot a bug in -Jolly, you may be asked to supply logfiles from operating -Jolly. Please be aware that at higher log levels, Jolly will include -the Jolly entry targets, and whichever text was entered in the search -window. This may be considered sensitive information and you should -always review logs before sharing them.* +Jolly, you may be asked to supply logfiles. Please be aware that at +higher log levels, Jolly will include the Jolly entry targets, and +whichever text was entered in the search window. This may be +considered sensitive information and you should always review logs +before sharing them.* To customize behavior, use the following fields: | field name | data type | description | |------------|----------------------------|------------------------------------------------------------------------------------------------------------------------------------------| -| `file` | *string* | Additional filename to write logs to. Always appends. | +| `file` | *string* | Filename to write logs to. Always appends. Can be `'stdout'` or `'stderr'` to write to that stream | | `filters` | *string* OR *string array* | [env_logger](https://docs.rs/env_logger/latest/env_logger/index.html#enabling-logging) filters for logging, by default, only log `error` | @@ -314,8 +313,11 @@ filters = ["jolly=debug", "cosmic_text=trace"] Specify a filename for Jolly to write logs to. If the file cannot be accessed then an error is returned. Jolly will always append to this -file if it already exists. Jolly will also continue to write trace -messages to `stderr`. +file if it already exists. + +Jolly treats a file named `'stderr'` `'stdout'` as special: If file is +set to one of these values, it will write to the corresponding stream. + ## `filters` — *string* OR *string array* diff --git a/src/log.rs b/src/log.rs index cba9a23..ad72e5c 100644 --- a/src/log.rs +++ b/src/log.rs @@ -14,26 +14,38 @@ pub struct LogSettings { impl LogSettings { pub fn init_logger(&self) -> Result<(), error::Error> { - self.build_logger().map(|mut b| b.init()) + self.build_logger().map(|b| { + if let Some(mut b) = b { + b.init() + } + }) } - fn build_logger(&self) -> Result { + fn build_logger(&self) -> Result, error::Error> { + use env_logger::fmt::Target; + let mut builder = Builder::new(); builder .parse_filters(&self.filters.join(",")) - .format_timestamp_micros() - .target(env_logger::fmt::Target::Stderr); + .format_timestamp_micros(); - if let Some(fname) = &self.file { - let f = std::fs::OpenOptions::new() - .append(true) - .create(true) - .open(fname) - .map_err(|e| error::Error::IoError(self.file.clone(), e))?; - builder.target(env_logger::fmt::Target::Pipe(Box::new(f))); + if let Some(filename) = &self.file { + if filename == "stdout" { + builder.target(Target::Stdout); + } else if filename == "stderr" { + builder.target(Target::Stderr); + } else { + let f = std::fs::OpenOptions::new() + .append(true) + .create(true) + .open(filename) + .map_err(|e| error::Error::IoError(self.file.clone(), e))?; + builder.target(env_logger::fmt::Target::Pipe(Box::new(f))); + } + Ok(Some(builder)) + } else { + Ok(None) } - - Ok(builder) } } @@ -52,6 +64,7 @@ mod tests { filters: vec!["trace".into()], } .build_logger() + .map(Option::unwrap) } #[test]