Skip to content

Commit 7ea09e9

Browse files
authored
subscriber: implement Clone for filter::Directive (#1985)
Using a crate like Clap, it's possible to support directive validation: ```rust pub(crate) struct Intrumentation { // ... /// Tracing directives (see https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives) #[clap(long, global = true, group = "verbosity", multiple_occurrences = true)] pub(crate) log_level: Vec<Directive>, } ``` Resulting in nice runtime validation errors: ``` $ cargo run -- --log-level h2=tra Finished dev [unoptimized + debuginfo] target(s) in 0.06s Running `target/debug/refuge --log-level h2=tra` error: Invalid value "h2=tra" for '--log-level <LOG_LEVEL>': invalid filter directive For more information try --help ``` Then later, we can build up an `EnvFilter` with these directives: ```rust let mut filter_layer = match EnvFilter::try_from_default_env() { Ok(layer) => layer, Err(_) => EnvFilter::try_new(&format!("{}={}", env!("CARGO_PKG_NAME"), self.log_level()))?, }; for directive in &self.log_level { // 'clone' the directive. let directive_str = directive.to_string(); let directive = Directive::from_str(&directive_str)?; filter_layer = filter_layer.add_directive(directive) } ``` This change allows cloning the `Directive` instead of doing the `to_string()` then `from_str()` dance. ```rust for directive in &self.log_level { filter_layer = filter_layer.add_directive(directive.clone()) } ``` Signed-off-by: Ana Hobden <[email protected]>
1 parent 1dc94fa commit 7ea09e9

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use tracing_core::{span, Level, Metadata};
77

88
/// A single filtering directive.
99
// TODO(eliza): add a builder for programmatically constructing directives?
10-
#[derive(Debug, Eq, PartialEq)]
10+
#[derive(Debug, Eq, PartialEq, Clone)]
1111
#[cfg_attr(docsrs, doc(cfg(feature = "env-filter")))]
1212
pub struct Directive {
1313
in_span: Option<String>,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::{
1313
use super::{FieldMap, LevelFilter};
1414
use tracing_core::field::{Field, Visit};
1515

16-
#[derive(Debug, Eq, PartialEq)]
16+
#[derive(Debug, Eq, PartialEq, Clone)]
1717
pub(crate) struct Match {
1818
pub(crate) name: String, // TODO: allow match patterns for names?
1919
pub(crate) value: Option<ValueMatch>,

0 commit comments

Comments
 (0)