README.OpenSource
1[
2 {
3 "Name": "log",
4 "License": "Apache License V2.0, MIT",
5 "License File": "LICENSE-APACHE, LICENSE-MIT",
6 "Version Number": "0.4.17",
7 "Owner": "fangting12@huawei.com",
8 "Upstream URL": "https://github.com/rust-lang/log",
9 "Description": "A Rust library that provides support for logging."
10 }
11]
README.md
1log
2===
3
4A Rust library providing a lightweight logging *facade*.
5
6[![Build status](https://img.shields.io/github/workflow/status/rust-lang/log/CI/master)](https://github.com/rust-lang/log/actions)
7[![Latest version](https://img.shields.io/crates/v/log.svg)](https://crates.io/crates/log)
8[![Documentation](https://docs.rs/log/badge.svg)](https://docs.rs/log)
9![License](https://img.shields.io/crates/l/log.svg)
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.31.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: {}, retrying", err);
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 of the most popular ones:
62
63* Simple minimal loggers:
64 * [`env_logger`](https://docs.rs/env_logger/*/env_logger/)
65 * [`simple_logger`](https://docs.rs/simple_logger/*/simple_logger/)
66 * [`simplelog`](https://docs.rs/simplelog/*/simplelog/)
67 * [`pretty_env_logger`](https://docs.rs/pretty_env_logger/*/pretty_env_logger/)
68 * [`stderrlog`](https://docs.rs/stderrlog/*/stderrlog/)
69 * [`flexi_logger`](https://docs.rs/flexi_logger/*/flexi_logger/)
70* Complex configurable frameworks:
71 * [`log4rs`](https://docs.rs/log4rs/*/log4rs/)
72 * [`fern`](https://docs.rs/fern/*/fern/)
73* Adaptors for other facilities:
74 * [`syslog`](https://docs.rs/syslog/*/syslog/)
75 * [`systemd-journal-logger`](https://docs.rs/systemd-journal-logger/*/systemd_journal_logger/)
76 * [`slog-stdlog`](https://docs.rs/slog-stdlog/*/slog_stdlog/)
77 * [`android_log`](https://docs.rs/android_log/*/android_log/)
78 * [`win_dbg_logger`](https://docs.rs/win_dbg_logger/*/win_dbg_logger/)
79 * [`db_logger`](https://docs.rs/db_logger/*/db_logger/)
80* For WebAssembly binaries:
81 * [`console_log`](https://docs.rs/console_log/*/console_log/)
82* For dynamic libraries:
83 * You may need to construct [an FFI-safe wrapper over `log`](https://github.com/rust-lang/log/issues/421) to initialize in your libraries.
84* Utilities:
85 * [`log_err`](https://docs.rs/log_err/*/log_err/)
86
87Executables should choose a logger implementation and initialize it early in the
88runtime of the program. Logger implementations will typically include a
89function to do this. Any log messages generated before the logger is
90initialized will be ignored.
91
92The executable itself may use the `log` crate to log as well.
93
94## Structured logging
95
96If you enable the `kv_unstable` feature, you can associate structured data with your log records:
97
98```rust
99use log::{info, trace, warn, as_serde, as_error};
100
101pub fn shave_the_yak(yak: &mut Yak) {
102 trace!(target = "yak_events", yak = as_serde!(yak); "Commencing yak shaving");
103
104 loop {
105 match find_a_razor() {
106 Ok(razor) => {
107 info!(razor = razor; "Razor located");
108 yak.shave(razor);
109 break;
110 }
111 Err(err) => {
112 warn!(err = as_error!(err); "Unable to locate a razor, retrying");
113 }
114 }
115 }
116}
117```
118