Skip to content

Commit 6ca4f7a

Browse files
authored
Merge pull request #143 from joshka/jm/docs-tracing
docs: document tracing support in README and lib.rs
2 parents 3523546 + cda6017 commit 6ca4f7a

File tree

2 files changed

+100
-28
lines changed

2 files changed

+100
-28
lines changed

README.md

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# clap-verbosity-flag for `log`
1+
# clap-verbosity-flag for `log` / `tracing`
22

33
[![Documentation](https://img.shields.io/badge/docs-master-blue.svg)][Documentation]
44
![License](https://img.shields.io/crates/l/clap-verbosity-flag.svg)
@@ -7,27 +7,65 @@
77
[Crates.io]: https://crates.io/crates/clap-verbosity-flag
88
[Documentation]: https://docs.rs/clap-verbosity-flag/
99

10-
Easily add a `--verbose` flag to CLIs using Clap
10+
Easily add `--verbose` and `--quiet` flags to CLIs using [Clap](http://crates.io/crates/clap).
1111

1212
## Examples
1313

14+
```shell
15+
cargo add clap-verbosity-flag
16+
```
17+
1418
```rust
1519
use clap::Parser;
1620

17-
// ...
1821
#[derive(Debug, Parser)]
1922
struct Cli {
2023
#[command(flatten)]
21-
verbose: clap_verbosity_flag::Verbosity,
24+
verbosity: clap_verbosity_flag::Verbosity,
2225
}
26+
27+
fn main() {
28+
let args = Cli::parse();
29+
env_logger::Builder::new()
30+
.filter_level(args.verbosity.into())
31+
.init();
32+
// Your code here
33+
}
34+
```
35+
36+
For [`tracing`](https://crates.io/crates/tracing) support, use the `tracing` feature:
37+
38+
```shell
39+
cargo add clap-verbosity-flag --no-default-features --features tracing
2340
```
2441

25-
By default, it'll only report errors.
26-
- `-q` silences output
27-
- `-v` show warnings
28-
- `-vv` show info
29-
- `-vvv` show debug
30-
- `-vvvv` show trace
42+
```rust
43+
use clap::Parser;
44+
45+
#[derive(Debug, Parser)]
46+
struct Cli {
47+
#[command(flatten)]
48+
verbosity: clap_verbosity_flag::Verbosity,
49+
}
50+
51+
fn main() {
52+
let args = Cli::parse();
53+
tracing_subscriber::fmt()
54+
.with_max_level(args.verbosity)
55+
.init();
56+
// Your code here
57+
}
58+
```
59+
60+
The default verbosity level will cause `log` / `tracing` to only report errors. The flags can be
61+
specified multiple times to increase or decrease the verbosity level. See the [Documentation] for
62+
info on how to change the default verbosity level.
63+
64+
- silence output: `-q` / `--quiet`
65+
- show warnings: `-v` / `--verbose`
66+
- show info: `-vv` / `--verbose --verbose`
67+
- show debug: `-vvv` / `--verbose --verbose --verbose`
68+
- show trace: `-vvvv` / `--verbose --verbose --verbose --verbose`
3169

3270
## License
3371

src/lib.rs

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,80 @@
1-
//! Control `log` level with a `--verbose` flag for your CLI
1+
//! Easily add `--verbose` and `--quiet` flags to CLIs using [Clap](http://crates.io/crates/clap).
22
//!
33
//! # Examples
44
//!
55
//! To get `--quiet` and `--verbose` flags through your entire program, just `flatten`
66
//! [`Verbosity`]:
7+
//!
8+
//! ```rust,no_run
9+
//! use clap::Parser;
10+
//! use clap_verbosity_flag::Verbosity;
11+
//!
12+
//! #[derive(Debug, Parser)]
13+
//! struct Cli {
14+
//! #[command(flatten)]
15+
//! verbosity: Verbosity,
16+
//!
17+
//! // ... other options
18+
//! }
19+
//! ```
20+
//!
21+
//! You can then use this to configure your logger:
22+
//!
723
//! ```rust,no_run
824
//! # use clap::Parser;
925
//! # use clap_verbosity_flag::Verbosity;
1026
//! #
11-
//! # /// Le CLI
1227
//! # #[derive(Debug, Parser)]
1328
//! # struct Cli {
14-
//! #[command(flatten)]
15-
//! verbose: Verbosity,
29+
//! # #[command(flatten)]
30+
//! # verbosity: Verbosity,
1631
//! # }
32+
//! let cli = Cli::parse();
33+
//! # #[cfg(feature = "log")]
34+
//! env_logger::Builder::new()
35+
//! .filter_level(cli.verbosity.log_level_filter())
36+
//! .init();
1737
//! ```
1838
//!
19-
//! You can then use this to configure your logger:
39+
//! ## Use with `tracing`
40+
//!
41+
//! To use with [`tracing`](https://crates.io/crates/tracing), disable the log feature flag and
42+
//! enable the `tracing` feature flag:
43+
//!
44+
//! ```shell
45+
//! cargo add clap_verbosity_flag --no-default features --features tracing
46+
//! ```
47+
//!
48+
//! Then you can use it like this:
49+
//!
2050
//! ```rust,no_run
2151
//! # use clap::Parser;
2252
//! # use clap_verbosity_flag::Verbosity;
2353
//! #
24-
//! # /// Le CLI
2554
//! # #[derive(Debug, Parser)]
2655
//! # struct Cli {
2756
//! # #[command(flatten)]
28-
//! # verbose: Verbosity,
57+
//! # verbosity: Verbosity,
2958
//! # }
3059
//! let cli = Cli::parse();
31-
//! # #[cfg(feature = "log")]
32-
//! env_logger::Builder::new()
33-
//! .filter_level(cli.verbose.log_level_filter())
60+
//! # #[cfg(feature = "tracing")]
61+
//! tracing_subscriber::fmt()
62+
//! .with_max_level(cli.verbosity)
3463
//! .init();
3564
//! ```
3665
//!
37-
//! By default, this will only report errors.
38-
//! - `-q` silences output
39-
//! - `-v` show warnings
40-
//! - `-vv` show info
41-
//! - `-vvv` show debug
42-
//! - `-vvvv` show trace
66+
//! # Using `--verbose` and `--quiet` flags
67+
//!
68+
//! The default verbosity level will cause `log` / `tracing` to only report errors. The flags can be
69+
//! specified multiple times to increase or decrease the verbosity level.
70+
//!
71+
//! - silence output: `-q` / `--quiet`
72+
//! - show warnings: `-v` / `--verbose`
73+
//! - show info: `-vv` / `--verbose --verbose`
74+
//! - show debug: `-vvv` / `--verbose --verbose --verbose`
75+
//! - show trace: `-vvvv` / `--verbose --verbose --verbose --verbose`
76+
//!
77+
//! # Customizing the default log level
4378
//!
4479
//! By default, the log level is set to Error. To customize this to a different level, pass a type
4580
//! implementing the [`LogLevel`] trait to [`Verbosity`]:
@@ -48,7 +83,6 @@
4883
//! # use clap::Parser;
4984
//! use clap_verbosity_flag::{Verbosity, InfoLevel};
5085
//!
51-
//! /// Le CLI
5286
//! #[derive(Debug, Parser)]
5387
//! struct Cli {
5488
//! #[command(flatten)]
@@ -63,7 +97,7 @@
6397
#![warn(clippy::print_stdout)]
6498

6599
#[doc = include_str!("../README.md")]
66-
#[cfg(doctest)]
100+
#[cfg(all(doctest, feature = "log", feature = "tracing"))]
67101
pub struct ReadmeDoctests;
68102

69103
use std::fmt;

0 commit comments

Comments
 (0)