-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds `OffLevel`, `DebugLevel`, and `TraceLevel` to the list of log levels that can be set as the default log level for the logger. A new example, `log_level.rs`, demonstrates how to set the default log level for Verbosity to something other than the default. It can be run with `cargo run --example=log_level [default level] [flags]`. E.g.,: `cargo run --example=log_level off -vvvv` will set the default log level to `OffLevel` and then apply the verbosity flags to set the log level to `Debug`. Fixes: #122
- Loading branch information
Showing
2 changed files
with
198 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//! Demonstrates how to set the default log level for the logger to something other than the default | ||
//! (ErrorLevel). This is done with multiple subcommands, each with their own verbosity level. | ||
Check failure Code scanning / clippy item in documentation is missing backticks Error
item in documentation is missing backticks
|
||
use clap::{Parser, Subcommand}; | ||
use clap_verbosity_flag::{ | ||
DebugLevel, ErrorLevel, InfoLevel, OffLevel, TraceLevel, Verbosity, WarnLevel, | ||
}; | ||
|
||
#[derive(Debug, Parser)] | ||
struct Cli { | ||
#[command(subcommand)] | ||
command: Command, | ||
} | ||
|
||
#[derive(Debug, Subcommand)] | ||
enum Command { | ||
Off { | ||
#[command(flatten)] | ||
verbose: Verbosity<OffLevel>, | ||
}, | ||
Error { | ||
#[command(flatten)] | ||
verbose: Verbosity<ErrorLevel>, | ||
}, | ||
Warn { | ||
#[command(flatten)] | ||
verbose: Verbosity<WarnLevel>, | ||
}, | ||
Info { | ||
#[command(flatten)] | ||
verbose: Verbosity<InfoLevel>, | ||
}, | ||
Debug { | ||
#[command(flatten)] | ||
verbose: Verbosity<DebugLevel>, | ||
}, | ||
Trace { | ||
#[command(flatten)] | ||
verbose: Verbosity<TraceLevel>, | ||
}, | ||
} | ||
|
||
impl Command { | ||
fn log_level_filter(&self) -> log::LevelFilter { | ||
match self { | ||
Command::Off { verbose } => verbose.log_level_filter(), | ||
Command::Error { verbose } => verbose.log_level_filter(), | ||
Command::Warn { verbose } => verbose.log_level_filter(), | ||
Command::Info { verbose } => verbose.log_level_filter(), | ||
Command::Debug { verbose } => verbose.log_level_filter(), | ||
Command::Trace { verbose } => verbose.log_level_filter(), | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
let cli = Cli::parse(); | ||
dbg!(&cli.command); | ||
Check failure Code scanning / clippy the dbg! macro is intended as a debugging tool Error
the dbg! macro is intended as a debugging tool
|
||
env_logger::Builder::new() | ||
.filter_level(cli.command.log_level_filter()) | ||
.init(); | ||
|
||
log::error!("Engines exploded"); | ||
log::warn!("Engines smoking"); | ||
log::info!("Engines exist"); | ||
log::debug!("Engine temperature is 200 degrees"); | ||
log::trace!("Engine subsection is 300 degrees"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters