Skip to content

Commit

Permalink
Do not log to stderr by default
Browse files Browse the repository at this point in the history
Jolly no longer logs to stderr to default. This cleans up the default
text that appears when running Jolly from the terminal.
  • Loading branch information
apgoetz committed Aug 7, 2023
1 parent b1e3d01 commit 0ec5e38
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 24 deletions.
24 changes: 13 additions & 11 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,22 +277,21 @@ As a general rule, the jolly build script will warn if the
# <a name="log"></a> [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` |


Expand All @@ -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` &mdash; *string* OR *string array*

Expand Down
39 changes: 26 additions & 13 deletions src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Builder, error::Error> {
fn build_logger(&self) -> Result<Option<Builder>, 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)
}
}

Expand All @@ -52,6 +64,7 @@ mod tests {
filters: vec!["trace".into()],
}
.build_logger()
.map(Option::unwrap)
}

#[test]
Expand Down

0 comments on commit 0ec5e38

Please sign in to comment.