diff --git a/README.md b/README.md index bc54171d..6f5ee2da 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/filter/mod.rs b/src/filter/mod.rs index 8f7787f0..1d476b39 100644 --- a/src/filter/mod.rs +++ b/src/filter/mod.rs @@ -336,7 +336,7 @@ fn parse_spec(spec: &str) -> (Vec, Option) { } }; dirs.push(Directive { - name: name.map(|s| s.to_string()), + name: name.map(|s| s.replace("-", "_")), level: log_level, }); } @@ -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); + } } diff --git a/src/lib.rs b/src/lib.rs index 494ccb1d..8a2c9021 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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