Skip to content

Commit

Permalink
subscriber: support dash in target names (#1012)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bkchr authored Oct 2, 2020
1 parent e8d2567 commit 5fb114f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
11 changes: 10 additions & 1 deletion tracing-subscriber/src/filter/env/directive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl FromStr for Directive {
^(?P<global_level>trace|TRACE|debug|DEBUG|info|INFO|warn|WARN|error|ERROR|off|OFF|[0-5])$ |
^
(?: # target name or span name
(?P<target>[\w:]+)|(?P<span>\[[^\]]*\])
(?P<target>[\w:-]+)|(?P<span>\[[^\]]*\])
){1,2}
(?: # level or nothing
=(?P<level>trace|TRACE|debug|DEBUG|info|INFO|warn|WARN|error|ERROR|off|OFF|[0-5])?
Expand Down Expand Up @@ -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);
}
}
5 changes: 5 additions & 0 deletions tracing-subscriber/src/filter/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down

0 comments on commit 5fb114f

Please sign in to comment.