lib.rs |
Control `log` level with a `--verbose` flag for your CLI
# Examples
To get `--quiet` and `--verbose` flags through your entire program, just `flatten`
[`Verbosity`]:
```rust,no_run
# use clap::Parser;
# use clap_verbosity_flag::Verbosity;
#
# /// Le CLI
# #[derive(Debug, Parser)]
# struct Cli {
#[command(flatten)]
verbose: Verbosity,
# }
```
You can then use this to configure your logger:
```rust,no_run
# use clap::Parser;
# use clap_verbosity_flag::Verbosity;
#
# /// Le CLI
# #[derive(Debug, Parser)]
# struct Cli {
# #[command(flatten)]
# verbose: Verbosity,
# }
let cli = Cli::parse();
# #[cfg(feature = "log")]
env_logger::Builder::new()
.filter_level(cli.verbose.log_level_filter())
.init();
```
By default, this will only report errors.
- `-q` silences output
- `-v` show warnings
- `-vv` show info
- `-vvv` show debug
- `-vvvv` show trace
By default, the log level is set to Error. To customize this to a different level, pass a type
implementing the [`LogLevel`] trait to [`Verbosity`]:
```rust,no_run
# use clap::Parser;
use clap_verbosity_flag::{Verbosity, InfoLevel};
/// Le CLI
#[derive(Debug, Parser)]
struct Cli {
#[command(flatten)]
verbose: Verbosity<InfoLevel>,
}
```
Or implement our [`LogLevel`] trait to customize the default log level and help output. |
12771 |
log.rs |
|
4935 |
tracing.rs |
|
5013 |