README.md
1log
2===
3
4A Rust library providing a lightweight logging *facade*.
5
6[](https://github.com/rust-lang/log/actions)
7[](https://crates.io/crates/log)
8[](https://docs.rs/log)
9
10
11* [`log` documentation](https://docs.rs/log)
12
13A logging facade provides a single logging API that abstracts over the actual
14logging implementation. Libraries can use the logging API provided by this
15crate, and the consumer of those libraries can choose the logging
16implementation that is most suitable for its use case.
17
18
19## Minimum supported `rustc`
20
21`1.60.0+`
22
23This version is explicitly tested in CI and may be bumped in any release as needed. Maintaining compatibility with older compilers is a priority though, so the bar for bumping the minimum supported version is set very high. Any changes to the supported minimum version will be called out in the release notes.
24
25## Usage
26
27### In libraries
28
29Libraries should link only to the `log` crate, and use the provided macros to
30log whatever information will be useful to downstream consumers:
31
32```toml
33[dependencies]
34log = "0.4"
35```
36
37```rust
38use log::{info, trace, warn};
39
40pub fn shave_the_yak(yak: &mut Yak) {
41 trace!("Commencing yak shaving");
42
43 loop {
44 match find_a_razor() {
45 Ok(razor) => {
46 info!("Razor located: {razor}");
47 yak.shave(razor);
48 break;
49 }
50 Err(err) => {
51 warn!("Unable to locate a razor: {err}, retrying");
52 }
53 }
54 }
55}
56```
57
58### In executables
59
60In order to produce log output, executables have to use a logger implementation compatible with the facade.
61There are many available implementations to choose from, here are some options:
62
63* Simple minimal loggers:
64 * [`env_logger`](https://docs.rs/env_logger/*/env_logger/)
65 * [`colog`](https://docs.rs/colog/*/colog/)
66 * [`simple_logger`](https://docs.rs/simple_logger/*/simple_logger/)
67 * [`simplelog`](https://docs.rs/simplelog/*/simplelog/)
68 * [`pretty_env_logger`](https://docs.rs/pretty_env_logger/*/pretty_env_logger/)
69 * [`stderrlog`](https://docs.rs/stderrlog/*/stderrlog/)
70 * [`flexi_logger`](https://docs.rs/flexi_logger/*/flexi_logger/)
71 * [`call_logger`](https://docs.rs/call_logger/*/call_logger/)
72 * [`std-logger`](https://docs.rs/std-logger/*/std_logger/)
73 * [`structured-logger`](https://docs.rs/structured-logger/latest/structured_logger/)
74 * [`clang_log`](https://docs.rs/clang_log/latest/clang_log)
75 * [`ftail`](https://docs.rs/ftail/latest/ftail/)
76* Complex configurable frameworks:
77 * [`log4rs`](https://docs.rs/log4rs/*/log4rs/)
78 * [`logforth`](https://docs.rs/logforth/*/logforth/)
79 * [`fern`](https://docs.rs/fern/*/fern/)
80 * [`spdlog-rs`](https://docs.rs/spdlog-rs/*/spdlog/)
81* Adaptors for other facilities:
82 * [`syslog`](https://docs.rs/syslog/*/syslog/)
83 * [`systemd-journal-logger`](https://docs.rs/systemd-journal-logger/*/systemd_journal_logger/)
84 * [`slog-stdlog`](https://docs.rs/slog-stdlog/*/slog_stdlog/)
85 * [`android_log`](https://docs.rs/android_log/*/android_log/)
86 * [`win_dbg_logger`](https://docs.rs/win_dbg_logger/*/win_dbg_logger/)
87 * [`db_logger`](https://docs.rs/db_logger/*/db_logger/)
88 * [`log-to-defmt`](https://docs.rs/log-to-defmt/*/log_to_defmt/)
89 * [`logcontrol-log`](https://docs.rs/logcontrol-log/*/logcontrol_log/)
90* For WebAssembly binaries:
91 * [`console_log`](https://docs.rs/console_log/*/console_log/)
92* For dynamic libraries:
93 * You may need to construct [an FFI-safe wrapper over `log`](https://github.com/rust-lang/log/issues/421) to initialize in your libraries.
94* Utilities:
95 * [`log_err`](https://docs.rs/log_err/*/log_err/)
96 * [`log-reload`](https://docs.rs/log-reload/*/log_reload/)
97 * [`alterable_logger`](https://docs.rs/alterable_logger/*/alterable_logger)
98
99Executables should choose a logger implementation and initialize it early in the
100runtime of the program. Logger implementations will typically include a
101function to do this. Any log messages generated before the logger is
102initialized will be ignored.
103
104The executable itself may use the `log` crate to log as well.
105
106## Structured logging
107
108If you enable the `kv` feature, you can associate structured data with your log records:
109
110```rust
111use log::{info, trace, warn};
112
113pub fn shave_the_yak(yak: &mut Yak) {
114 // `yak:serde` will capture `yak` using its `serde::Serialize` impl
115 //
116 // You could also use `:?` for `Debug`, or `:%` for `Display`. For a
117 // full list, see the `log` crate documentation
118 trace!(target = "yak_events", yak:serde; "Commencing yak shaving");
119
120 loop {
121 match find_a_razor() {
122 Ok(razor) => {
123 info!(razor; "Razor located");
124 yak.shave(razor);
125 break;
126 }
127 Err(e) => {
128 // `e:err` will capture `e` using its `std::error::Error` impl
129 warn!(e:err; "Unable to locate a razor, retrying");
130 }
131 }
132 }
133}
134```
135