Skip to content

Commit da3dd1f

Browse files
committed
add docs
Signed-off-by: tison <[email protected]>
1 parent 11af9f1 commit da3dd1f

File tree

1 file changed

+68
-23
lines changed

1 file changed

+68
-23
lines changed

src/macros.rs

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,56 @@
1313
/// This macro will generically log with the specified `Level` and `format!`
1414
/// based argument list.
1515
///
16-
/// # Examples
17-
///
1816
/// ```
1917
/// use log::{log, Level};
2018
///
21-
/// # fn main() {
2219
/// let data = (42, "Forty-two");
2320
/// let private_data = "private";
2421
///
2522
/// log!(Level::Error, "Received errors: {}, {}", data.0, data.1);
26-
/// log!(target: "app_events", Level::Warn, "App warning: {}, {}, {}",
27-
/// data.0, data.1, private_data);
28-
/// # }
2923
/// ```
24+
///
25+
/// Optionally, you can specify a `target` argument to attach a specific target
26+
/// to the log record. By default, the target is the module path of the caller.
27+
///
28+
/// ```
29+
/// use log::{log, Level};
30+
///
31+
/// let data = (42, "Forty-two");
32+
/// let private_data = "private";
33+
///
34+
/// log!(
35+
/// target: "app_events",
36+
/// Level::Error,
37+
/// "Received errors: {}, {}",
38+
/// data.0, data.1
39+
/// );
40+
/// ```
41+
///
42+
/// And optionally, you can specify a `logger` argument to use a specific logger
43+
/// instead of the default global logger.
44+
///
45+
/// ```
46+
/// # struct MyLogger {}
47+
/// # impl Log for MyLogger {
48+
/// # fn enabled(&self, _metadata: &log::Metadata) -> bool {
49+
/// # false
50+
/// # }
51+
/// # fn log(&self, _record: &log::Record) {}
52+
/// # fn flush(&self) {}
53+
/// # }
54+
/// use log::{log, Level, Log};
55+
///
56+
/// let data = (42, "Forty-two");
57+
/// let private_data = "private";
58+
///
59+
/// let my_logger = MyLogger {};
60+
/// log!(
61+
/// logger: &my_logger,
62+
/// Level::Error,
63+
/// "Received errors: {}, {}",
64+
/// data.0, data.1
65+
/// );
3066
#[macro_export]
3167
#[clippy::format_args]
3268
macro_rules! log {
@@ -81,14 +117,14 @@ macro_rules! log {
81117
/// # Examples
82118
///
83119
/// ```
84-
/// use log::error;
120+
/// use log::{error, GlobalLogger};
85121
///
86-
/// # fn main() {
87122
/// let (err_info, port) = ("No connection", 22);
123+
/// let my_logger = GlobalLogger; // can be any logger implementing the Log trait
88124
///
89125
/// error!("Error: {err_info} on port {port}");
90126
/// error!(target: "app_events", "App Error: {err_info}, Port: {port}");
91-
/// # }
127+
/// error!(logger: my_logger, "App Error: {err_info}, Port: {port}");
92128
/// ```
93129
#[macro_export]
94130
#[clippy::format_args]
@@ -120,14 +156,14 @@ macro_rules! error {
120156
/// # Examples
121157
///
122158
/// ```
123-
/// use log::warn;
159+
/// use log::{warn, GlobalLogger};
124160
///
125-
/// # fn main() {
126161
/// let warn_description = "Invalid Input";
162+
/// let my_logger = GlobalLogger; // can be any logger implementing the Log trait
127163
///
128164
/// warn!("Warning! {warn_description}!");
129165
/// warn!(target: "input_events", "App received warning: {warn_description}");
130-
/// # }
166+
/// warn!(logger: my_logger, "App received warning: {warn_description}");
131167
/// ```
132168
#[macro_export]
133169
#[clippy::format_args]
@@ -159,16 +195,23 @@ macro_rules! warn {
159195
/// # Examples
160196
///
161197
/// ```
162-
/// use log::info;
198+
/// use log::{info, GlobalLogger};
163199
///
164-
/// # fn main() {
165200
/// # struct Connection { port: u32, speed: f32 }
166201
/// let conn_info = Connection { port: 40, speed: 3.20 };
202+
/// let my_logger = GlobalLogger; // can be any logger implementing the Log trait
167203
///
168204
/// info!("Connected to port {} at {} Mb/s", conn_info.port, conn_info.speed);
169-
/// info!(target: "connection_events", "Successful connection, port: {}, speed: {}",
170-
/// conn_info.port, conn_info.speed);
171-
/// # }
205+
/// info!(
206+
/// target: "connection_events",
207+
/// "Successful connection, port: {}, speed: {}",
208+
/// conn_info.port, conn_info.speed
209+
/// );
210+
/// info!(
211+
/// logger: my_logger,
212+
/// "Successful connection, port: {}, speed: {}",
213+
/// conn_info.port, conn_info.speed
214+
/// );
172215
/// ```
173216
#[macro_export]
174217
#[clippy::format_args]
@@ -200,15 +243,15 @@ macro_rules! info {
200243
/// # Examples
201244
///
202245
/// ```
203-
/// use log::debug;
246+
/// use log::{debug, GlobalLogger};
204247
///
205-
/// # fn main() {
206248
/// # struct Position { x: f32, y: f32 }
207249
/// let pos = Position { x: 3.234, y: -1.223 };
250+
/// let my_logger = GlobalLogger; // can be any logger implementing the Log trait
208251
///
209252
/// debug!("New position: x: {}, y: {}", pos.x, pos.y);
210253
/// debug!(target: "app_events", "New position: x: {}, y: {}", pos.x, pos.y);
211-
/// # }
254+
/// debug!(logger: my_logger, "New position: x: {}, y: {}", pos.x, pos.y);
212255
/// ```
213256
#[macro_export]
214257
#[clippy::format_args]
@@ -240,17 +283,19 @@ macro_rules! debug {
240283
/// # Examples
241284
///
242285
/// ```
243-
/// use log::trace;
286+
/// use log::{trace, GlobalLogger};
244287
///
245-
/// # fn main() {
246288
/// # struct Position { x: f32, y: f32 }
247289
/// let pos = Position { x: 3.234, y: -1.223 };
290+
/// let my_logger = GlobalLogger; // can be any logger implementing the Log trait
248291
///
249292
/// trace!("Position is: x: {}, y: {}", pos.x, pos.y);
250293
/// trace!(target: "app_events", "x is {} and y is {}",
251294
/// if pos.x >= 0.0 { "positive" } else { "negative" },
252295
/// if pos.y >= 0.0 { "positive" } else { "negative" });
253-
/// # }
296+
/// trace!(logger: my_logger, "x is {} and y is {}",
297+
/// if pos.x >= 0.0 { "positive" } else { "negative" },
298+
/// if pos.y >= 0.0 { "positive" } else { "negative" });
254299
/// ```
255300
#[macro_export]
256301
#[clippy::format_args]

0 commit comments

Comments
 (0)