Commit 7ea09e9
authored
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
2 files changed
+2
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| |||
0 commit comments