Skip to content

Instantly share code, notes, and snippets.

@jfear
Created December 21, 2024 23:14
Show Gist options
  • Save jfear/c8b1a7ca27b29fc1054017c186faa40e to your computer and use it in GitHub Desktop.
Save jfear/c8b1a7ca27b29fc1054017c186faa40e to your computer and use it in GitHub Desktop.
Copied from the clap-verbosity examples folder. This shows how to use the verbosity trait to set tracing log level.
use clap::Parser;
use clap_verbosity_flag::Verbosity;
/// Foo
#[derive(Debug, Parser)]
struct Cli {
#[command(flatten)]
verbosity: Verbosity,
}
fn main() {
let cli = Cli::parse();
tracing_subscriber::fmt()
.with_max_level(cli.verbosity)
.init();
tracing::error!("Engines exploded");
tracing::warn!("Engines smoking");
tracing::info!("Engines exist");
tracing::debug!("Engine temperature is 200 degrees");
tracing::trace!("Engine subsection is 300 degrees");
}
@jfear
Copy link
Author

jfear commented Dec 21, 2024

You can also set the default level to Info using:

use clap_verbosity::{Verbosity, InfoLevel};

/// The CLI
#[derive(Debug, Parser)]
struct Cli {
    #[command(flatten)]
    verbose: Verbosity<InfoLevel>,
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment