Skip to content

Commit 25b4dd4

Browse files
committed
Add tracing crate
Fixes #706
1 parent 408e7bf commit 25b4dd4

File tree

5 files changed

+76
-1
lines changed

5 files changed

+76
-1
lines changed

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ tempfile = "3.14"
6161
thiserror = "2"
6262
anyhow = "1.0"
6363
threadpool = "1.8"
64-
toml = "0.8"
6564
tokio = { version = "1", features = ["full"] }
65+
toml = "0.8"
66+
tracing = "0.1"
67+
tracing-subscriber = "0.3"
6668
unicode-segmentation = "1.2.1"
6769
url = "2.5"
6870
walkdir = "2.5"

src/SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
[Table of Contents](intro.md)
44
[About](about.md)
5+
56
- [Algorithms](algorithms.md)
67
- [Generate Random Values](algorithms/randomness.md)
78
- [Sort a Vector](algorithms/sorting.md)
@@ -28,6 +29,7 @@
2829
- [Debugging](development_tools/debugging.md)
2930
- [Log Messages](development_tools/debugging/log.md)
3031
- [Configure Logging](development_tools/debugging/config_log.md)
32+
- [Tracing Messages](development_tools/debugging/tracing.md)
3133
- [Versioning](development_tools/versioning.md)
3234
- [Build Time Tooling](development_tools/build_tools.md)
3335
- [Encoding](encoding.md)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Tracing
2+
3+
[`tracing`](https://crates.io/crates/tracing) is a framework for instrumenting Rust programs to
4+
collect structured, event-based diagnostic information. It is alternative to the older
5+
[`log`](https://crates.io/crates/log) crate and has adapters to be backwards compatible.
6+
7+
To enable tracing in your applicaiont, add the following crates to your project:
8+
9+
```shell
10+
cargo add tracing tracing-subscriber
11+
```
12+
13+
For libraries, tracing-subscriber is usually not required.
14+
15+
{{#include tracing/tracing-console.md}}
16+
17+
{{#include ../../links.md}}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## Log messages to the console
2+
3+
[![tracing-badge]][tracing] [![tracing-subscriber-badge]][tracing-subscriber] [![cat-debugging-badge]][cat-debugging]
4+
5+
The `tracing` crate provides macros to emit log events to a tracing subscriber. The
6+
`tracing-subscriber` crate configures where to send the events. To install the default tracing
7+
subscriber, call [`tracing_subscriber::fmt::init()`].
8+
9+
[`tracing_subscriber::fmt::init()`]: https://docs.rs/tracing-subscriber/latest/tracing_subscriber/fmt/fn.init.html
10+
11+
```rust
12+
use tracing::{debug, error, info, trace, warn};
13+
14+
fn main() {
15+
tracing_subscriber::fmt::init();
16+
17+
error!("This is an error!");
18+
warn!("This is a warning.");
19+
info!("This is an informational message.");
20+
21+
// with the default configuration, debug! and trace! messages are not shown
22+
debug!("This is a debug message.");
23+
trace!("This is a trace message.");
24+
}
25+
```
26+
27+
The default log level is `INFO`. Tracing will drop events logged at lower levels. Running this code
28+
prints the following to the console:
29+
30+
<!--
31+
Generated using https://crates.io/crates/to-html, running:
32+
RUST_LOG=trace to-html --no-prompt "cargo run --quiet --example=tracing-console
33+
-->
34+
35+
<pre class="terminal"><code><span style='opacity:0.67'>2024-12-01T07:56:14.778440Z</span> <span style='color:var(--red,#a00)'>ERROR</span> <span style='opacity:0.67'>tracing_console:</span> This is an error!
36+
<span style='opacity:0.67'>2024-12-01T07:56:14.778568Z</span> <span style='color:var(--yellow,#a60)'> WARN</span> <span style='opacity:0.67'>tracing_console:</span> This is a warning.
37+
<span style='opacity:0.67'>2024-12-01T07:56:14.778596Z</span> <span style='color:var(--green,#0a0)'> INFO</span> <span style='opacity:0.67'>tracing_console:</span> This is an informational message.
38+
</code></pre>
39+
40+
To configure a more verbose default level, set the `RUST_LOG` environment variable:
41+
42+
```shell
43+
RUST_LOG=trace cargo run --example log-debug
44+
```
45+
46+
Cargo prints these following extra lines in addition to the ones above:
47+
48+
<pre class="terminal"><code><span style='opacity:0.67'>2024-12-01T07:56:14.778613Z</span> <span style='color:var(--blue,#00a)'>DEBUG</span> <span style='opacity:0.67'>tracing_console:</span> This is a debug message.
49+
<span style='opacity:0.67'>2024-12-01T07:56:14.778640Z</span> <span style='color:var(--magenta,#a0a)'>TRACE</span> <span style='opacity:0.67'>tracing_console:</span> This is a trace message.
50+
</code></pre>

src/links.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ Keep lines sorted.
143143
[threadpool]: https://docs.rs/threadpool/
144144
[toml-badge]: https://badge-cache.kominick.com/crates/v/toml.svg?label=toml
145145
[toml]: https://docs.rs/toml/
146+
[tracing-badge]: https://badge-cache.kominick.com/crates/v/tracing.svg?label=tracing
147+
[tracing]: https://docs.rs/tracing/
148+
[tracing-subscriber-badge]: https://badge-cache.kominick.com/crates/v/tracing-subscriber.svg?label=tracing-subscriber
149+
[tracing-subscriber]: https://docs.rs/tracing-subscriber/
146150
[url-badge]: https://badge-cache.kominick.com/crates/v/url.svg?label=url
147151
[url]: https://docs.rs/url/
148152
[unicode-segmentation-badge]: https://badge-cache.kominick.com/crates/v/unicode-segmentation.svg?label=unicode-segmentation

0 commit comments

Comments
 (0)