Skip to content

Support kebab-case names in parse sepc #177

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ $ RUST_LOG=info ./main
[2018-11-03T06:09:06Z INFO default] starting up
```

To set the log level on a per module basis, module names can be declared as `path::to::module=level` in **`RUST_LOG`**.

```console
$ RUST_LOG=main=info ./main
[2017-11-09T02:12:24Z ERROR main] this is printed by default
[2017-11-09T02:12:24Z INFO main] the answer was: 12
```

When dealing with crate name with hyphens, either the original form (e.g., `my-app`) or the canonical form (e.g., `my_app`) works.

```console
$ RUST_LOG=my-app ./my-app # `RUST_LOG=my_app ./my-app` also works
[2017-11-09T02:12:24Z DEBUG my_app] this is a debug message
[2017-11-09T02:12:24Z ERROR my_app] this is printed by default
[2017-11-09T02:12:24Z INFO my_app] the answer was: 12
```

The letter case is not significant for the logging level names; e.g., `debug`,
`DEBUG`, and `dEbuG` all represent the same logging level. Therefore, the
previous example could also have been written this way, specifying the log
Expand Down
14 changes: 13 additions & 1 deletion src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ fn parse_spec(spec: &str) -> (Vec<Directive>, Option<inner::Filter>) {
}
};
dirs.push(Directive {
name: name.map(|s| s.to_string()),
name: name.map(|s| s.replace("-", "_")),
level: log_level,
});
}
Expand Down Expand Up @@ -860,4 +860,16 @@ mod tests {
assert_eq!(dirs[0].level, LevelFilter::max());
assert!(filter.is_some() && filter.unwrap().to_string() == "a*c");
}

#[test]
fn parse_spec_name_canonicalization() {
// accept binary names in both styles (`snake_case` and `kebab-case`)
let (dirs, _) = parse_spec("snake_case_crate=info,kebab-case-crate=debug");
assert_eq!(dirs.len(), 2);
assert_eq!(dirs[0].name, Some("snake_case_crate".to_string()));
assert_eq!(dirs[0].level, LevelFilter::Info);

assert_eq!(dirs[1].name, Some("kebab_case_crate".to_string()));
assert_eq!(dirs[1].level, LevelFilter::Debug);
}
}
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@
//! [2017-11-09T02:12:24Z INFO main] the answer was: 12
//! ```
//!
//! If the binary name contains hyphens, you will need to replace
//! them with underscores:
//! Some package names may contain hyphens, this logger does the conversion for
//! you so you don't have to. You are free to use the original form (`my-app`)
//! or the canonical form (`my_app`).
//!
//! ```{.bash}
//! $ RUST_LOG=my_app ./my-app
//! $ RUST_LOG=my-app ./my-app
//! [2017-11-09T02:12:24Z DEBUG my_app] this is a debug message
//! [2017-11-09T02:12:24Z ERROR my_app] this is printed by default
//! [2017-11-09T02:12:24Z INFO my_app] the answer was: 12
Expand Down