Skip to content

Commit 5fb114f

Browse files
authored
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.
1 parent e8d2567 commit 5fb114f

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

tracing-subscriber/src/filter/env/directive.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl FromStr for Directive {
181181
^(?P<global_level>trace|TRACE|debug|DEBUG|info|INFO|warn|WARN|error|ERROR|off|OFF|[0-5])$ |
182182
^
183183
(?: # target name or span name
184-
(?P<target>[\w:]+)|(?P<span>\[[^\]]*\])
184+
(?P<target>[\w:-]+)|(?P<span>\[[^\]]*\])
185185
){1,2}
186186
(?: # level or nothing
187187
=(?P<level>trace|TRACE|debug|DEBUG|info|INFO|warn|WARN|error|ERROR|off|OFF|[0-5])?
@@ -1019,4 +1019,13 @@ mod test {
10191019
assert_eq!(dirs[2].level, LevelFilter::DEBUG);
10201020
assert_eq!(dirs[2].in_span, Some("baz".to_string()));
10211021
}
1022+
1023+
#[test]
1024+
fn parse_directives_with_dash_in_target_name() {
1025+
let dirs = parse_directives("target-name=info");
1026+
assert_eq!(dirs.len(), 1, "\nparsed: {:#?}", dirs);
1027+
assert_eq!(dirs[0].target, Some("target-name".to_string()));
1028+
assert_eq!(dirs[0].level, LevelFilter::INFO);
1029+
assert_eq!(dirs[0].in_span, None);
1030+
}
10221031
}

tracing-subscriber/src/filter/env/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ use tracing_core::{
6868
/// - If only a level is provided, it will set the maximum level for all `Span`s and `Event`s
6969
/// that are not enabled by other filters.
7070
/// - A directive without a level will enable anything that it matches. This is equivalent to `=trace`.
71+
/// - When a crate has a dash in its name, the default target for events will be the
72+
/// crate's module path as it appears in Rust. This means every dash will be replaced
73+
/// with an underscore.
74+
/// - A dash in a target will only appear when being specified explicitly:
75+
/// `tracing::info!(target: "target-name", ...);`
7176
///
7277
/// ## Examples
7378
///

0 commit comments

Comments
 (0)