Skip to content

Commit d718555

Browse files
committed
Make log-level setting more granular. Hide spans at most levels. Demo simple timestamp.
Core functionality of counting -v flags was taken from `clap_verbosity_flag`, but we match our own custom verbosity now to allow setting different log levels and other settings. Spans are hidden on the first two levels of verbosity. Pre-existing Chrono dep is used to create a more compact timestamp in logs with strftime-like formatting.
1 parent 267bf5a commit d718555

File tree

4 files changed

+23
-27
lines changed

4 files changed

+23
-27
lines changed

Cargo.lock

Lines changed: 1 addition & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ edition = "2021"
66
[dependencies]
77
anyhow = "1.0.82"
88
clap = { version = "4.5.4", features = ["unicode", "env", "derive"] }
9-
clap-verbosity-flag = "2.2.0"
109
itertools = "0.12.1"
1110
glob = "0.3.1"
1211
serde = { version = "1.0", features = ["derive"] }
1312
serde_yml = "0.0.12"
1413
serde_nested_with = "0.2.5"
1514
tracing = { version = "0.1.41", features = ["attributes"] }
16-
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
15+
tracing-subscriber = { version = "0.3.19", features = ["chrono", "env-filter"] }
1716
fully_pub = "0.1.4"
1817
void = "1"
1918
futures = "0.3.30"

src/cli.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use clap::{Parser, Subcommand};
2-
use clap_verbosity_flag::{InfoLevel, Verbosity};
32

43
#[derive(Parser, Debug)]
54
/// Deployment manager for rCTF/beaverCTF challenges deployed on Kubernetes.
65
pub struct Cli {
7-
#[command(flatten)]
8-
pub verbose: Verbosity<InfoLevel>,
6+
/// Increase output verbosity. One usage increases slightly. Two increases significantly. Three or
7+
/// more maximize output.
8+
#[arg(short = 'v', action = clap::ArgAction::Count, global = true)]
9+
pub verbosity: u8,
910

1011
#[command(subcommand)]
1112
pub command: Commands,

src/main.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
use beavercds_ng::commands;
22
use clap::Parser;
3-
use tracing::trace;
4-
use tracing_subscriber::EnvFilter;
3+
use tracing::{trace, Level};
4+
use tracing_subscriber::{fmt::time, EnvFilter};
55

66
mod cli;
77

88
fn main() {
99
let cli = cli::Cli::parse();
1010

11-
// Use RUST_LOG env variable if it's set
12-
// Otherwise our tracing is filtered by -q|-v* flag, all others always INFO and above
11+
// number of 'v' flags influences our crate's log level, all other log levels, and whether or
12+
// not we display the span stack, respectively
13+
let (brcds_level, dep_level, display_spans) = match cli.verbosity {
14+
0 => (Level::INFO, Level::WARN, false),
15+
1 => (Level::DEBUG, Level::INFO, false),
16+
2 => (Level::TRACE, Level::DEBUG, true),
17+
_ => (Level::TRACE, Level::TRACE, true),
18+
};
19+
20+
// Use RUST_LOG env variable to set log levels if it's set
21+
// Otherwise we use the above levels. Span-stack display always influenced by -v
22+
let timer = time::ChronoLocal::new("%H:%M:%S".to_owned());
1323
tracing_subscriber::fmt()
1424
.with_env_filter(EnvFilter::try_from_default_env().unwrap_or_else(|_| {
15-
format!(
16-
"{}={},INFO",
17-
env!("CARGO_CRATE_NAME"),
18-
cli.verbose.log_level_filter()
19-
)
20-
.into()
25+
format!("{}={brcds_level},{dep_level}", env!("CARGO_CRATE_NAME")).into()
2126
}))
22-
.without_time()
27+
.with_timer(timer)
28+
.with_target(display_spans)
2329
.init();
2430

2531
trace!("args: {:?}", cli);

0 commit comments

Comments
 (0)