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* Complex configurable frameworks: 75 * [`log4rs`](https://docs.rs/log4rs/*/log4rs/) 76 * [`fern`](https://docs.rs/fern/*/fern/) 77* Adaptors for other facilities: 78 * [`syslog`](https://docs.rs/syslog/*/syslog/) 79 * [`systemd-journal-logger`](https://docs.rs/systemd-journal-logger/*/systemd_journal_logger/) 80 * [`slog-stdlog`](https://docs.rs/slog-stdlog/*/slog_stdlog/) 81 * [`android_log`](https://docs.rs/android_log/*/android_log/) 82 * [`win_dbg_logger`](https://docs.rs/win_dbg_logger/*/win_dbg_logger/) 83 * [`db_logger`](https://docs.rs/db_logger/*/db_logger/) 84 * [`log-to-defmt`](https://docs.rs/log-to-defmt/*/log_to_defmt/) 85 * [`logcontrol-log`](https://docs.rs/logcontrol-log/*/logcontrol_log/) 86* For WebAssembly binaries: 87 * [`console_log`](https://docs.rs/console_log/*/console_log/) 88* For dynamic libraries: 89 * You may need to construct [an FFI-safe wrapper over `log`](https://github.com/rust-lang/log/issues/421) to initialize in your libraries. 90* Utilities: 91 * [`log_err`](https://docs.rs/log_err/*/log_err/) 92 * [`log-reload`](https://docs.rs/log-reload/*/log_reload/) 93 * [`alterable_logger`](https://docs.rs/alterable_logger/*/alterable_logger) 94 95Executables should choose a logger implementation and initialize it early in the 96runtime of the program. Logger implementations will typically include a 97function to do this. Any log messages generated before the logger is 98initialized will be ignored. 99 100The executable itself may use the `log` crate to log as well. 101 102## Structured logging 103 104If you enable the `kv` feature, you can associate structured data with your log records: 105 106```rust 107use log::{info, trace, warn}; 108 109pub fn shave_the_yak(yak: &mut Yak) { 110 // `yak:serde` will capture `yak` using its `serde::Serialize` impl 111 // 112 // You could also use `:?` for `Debug`, or `:%` for `Display`. For a 113 // full list, see the `log` crate documentation 114 trace!(target = "yak_events", yak:serde; "Commencing yak shaving"); 115 116 loop { 117 match find_a_razor() { 118 Ok(razor) => { 119 info!(razor; "Razor located"); 120 yak.shave(razor); 121 break; 122 } 123 Err(e) => { 124 // `e:err` will capture `e` using its `std::error::Error` impl 125 warn!(e:err; "Unable to locate a razor, retrying"); 126 } 127 } 128 } 129} 130``` 131