From 5fb114f889cee784fb81763f1bed5725fd6b7e8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Sat, 3 Oct 2020 00:33:35 +0200 Subject: [PATCH] subscriber: support dash in target names (#1012) This adds support for having dashes in log target names, like: `target-name=info` ## Motivation We are using log targets with dashes in our project. ## Solution Extend the target parsing regex to support dashes. --- tracing-subscriber/src/filter/env/directive.rs | 11 ++++++++++- tracing-subscriber/src/filter/env/mod.rs | 5 +++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tracing-subscriber/src/filter/env/directive.rs b/tracing-subscriber/src/filter/env/directive.rs index 06565efa4d..b6db3c052d 100644 --- a/tracing-subscriber/src/filter/env/directive.rs +++ b/tracing-subscriber/src/filter/env/directive.rs @@ -181,7 +181,7 @@ impl FromStr for Directive { ^(?Ptrace|TRACE|debug|DEBUG|info|INFO|warn|WARN|error|ERROR|off|OFF|[0-5])$ | ^ (?: # target name or span name - (?P[\w:]+)|(?P\[[^\]]*\]) + (?P[\w:-]+)|(?P\[[^\]]*\]) ){1,2} (?: # level or nothing =(?Ptrace|TRACE|debug|DEBUG|info|INFO|warn|WARN|error|ERROR|off|OFF|[0-5])? @@ -1019,4 +1019,13 @@ mod test { assert_eq!(dirs[2].level, LevelFilter::DEBUG); assert_eq!(dirs[2].in_span, Some("baz".to_string())); } + + #[test] + fn parse_directives_with_dash_in_target_name() { + let dirs = parse_directives("target-name=info"); + assert_eq!(dirs.len(), 1, "\nparsed: {:#?}", dirs); + assert_eq!(dirs[0].target, Some("target-name".to_string())); + assert_eq!(dirs[0].level, LevelFilter::INFO); + assert_eq!(dirs[0].in_span, None); + } } diff --git a/tracing-subscriber/src/filter/env/mod.rs b/tracing-subscriber/src/filter/env/mod.rs index ac3fac4bbf..84548c0a07 100644 --- a/tracing-subscriber/src/filter/env/mod.rs +++ b/tracing-subscriber/src/filter/env/mod.rs @@ -68,6 +68,11 @@ use tracing_core::{ /// - If only a level is provided, it will set the maximum level for all `Span`s and `Event`s /// that are not enabled by other filters. /// - A directive without a level will enable anything that it matches. This is equivalent to `=trace`. +/// - When a crate has a dash in its name, the default target for events will be the +/// crate's module path as it appears in Rust. This means every dash will be replaced +/// with an underscore. +/// - A dash in a target will only appear when being specified explicitly: +/// `tracing::info!(target: "target-name", ...);` /// /// ## Examples ///