Skip to content

Commit 48944ca

Browse files
committed
fix a few doc errors
1 parent 88b88fe commit 48944ca

5 files changed

Lines changed: 123 additions & 22 deletions

File tree

examples/custom_format.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ Before running this example, try setting the `MY_LOG_LEVEL` environment variable
77
$ export MY_LOG_LEVEL='info'
88
```
99
10-
Also try setting the `MY_LOG_STYLE` environment variable to `0` to disable colors:
10+
Also try setting the `MY_LOG_STYLE` environment variable to `never` to disable colors
11+
or `auto` to enable them:
1112
1213
```no_run,shell
13-
$ export MY_LOG_STYLE=0
14+
$ export MY_LOG_STYLE=never
1415
```
1516
1617
If you want to control the logging output completely, see the `custom_logger` example.

examples/custom_logger.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ Before running this example, try setting the `MY_LOG_LEVEL` environment variable
77
$ export MY_LOG_LEVEL='info'
88
```
99
10-
Also try setting the `RUST_LOG_STYLE` environment variable to `0` to disable colors:
10+
Also try setting the `MY_LOG_STYLE` environment variable to `never` to disable colors
11+
or `auto` to enable them:
1112
1213
```no_run,shell
13-
$ export RUST_LOG_STYLE=0
14+
$ export MY_LOG_STYLE=never
1415
```
1516
1617
If you only want to change the way logs are formatted, look at the `custom_format` example.

examples/default.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ Before running this example, try setting the `MY_LOG_LEVEL` environment variable
77
$ export MY_LOG_LEVEL='info'
88
```
99
10-
Also try setting the `RUST_LOG_STYLE` environment variable to `0` to disable colors:
10+
Also try setting the `MY_LOG_STYLE` environment variable to `never` to disable colors
11+
or `auto` to enable them:
1112
1213
```no_run,shell
13-
$ export RUST_LOG_STYLE=0
14+
$ export MY_LOG_STYLE=never
1415
```
1516
*/
1617

src/fmt.rs

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,33 @@
55
//! about the contents of this module and can use the `Formatter` like an ordinary
66
//! [`Write`].
77
//!
8+
//! # Formatting log records
9+
//!
10+
//! The format used to print log records can be customised using the [`Builder.format`]
11+
//! method.
12+
//! Custom formats can apply different color and weight to printed values using
13+
//! [`Style`] builders.
14+
//!
15+
//! ```
16+
//! use std::io::Write;
17+
//! use env_logger::fmt::Color;
18+
//!
19+
//! let mut builder = env_logger::Builder::new();
20+
//!
21+
//! builder.format(|buf, record| {
22+
//! let mut level_style = buf.style();
23+
//!
24+
//! level_style.set_color(Color::Red).set_bold(true);
25+
//!
26+
//! writeln!(buf, "{}: {}",
27+
//! level_style.value(record.level()),
28+
//! record.args())
29+
//! });
30+
//! ```
31+
//!
832
//! [`Formatter`]: struct.Formatter.html
33+
//! [`Style`]: struct.Style.html
34+
//! [`Builder.format`]: ../struct.Builder.html#method.format
935
//! [`Write`]: https://doc.rust-lang.org/stable/std/io/trait.Write.html
1036
1137
use std::io::prelude::*;
@@ -111,7 +137,16 @@ pub struct StyledValue<'a, T> {
111137
value: T,
112138
}
113139

114-
/// Log target, either stdout or stderr.
140+
/// An [RFC3339] formatted timestamp.
141+
///
142+
/// The timestamp implements [`Display`] and can be written to a [`Formatter`].
143+
///
144+
/// [RFC3339]: https://www.ietf.org/rfc/rfc3339.txt
145+
/// [`Display`]: https://doc.rust-lang.org/stable/std/fmt/trait.Display.html
146+
/// [`Formatter`]: struct.Formatter.html
147+
pub struct Timestamp(DateTime<Utc>);
148+
149+
/// Log target, either `stdout` or `stderr`.
115150
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
116151
pub enum Target {
117152
/// Logs will be sent to standard output.
@@ -131,7 +166,7 @@ impl Default for Target {
131166
pub enum WriteStyle {
132167
/// Try to print styles, but don't force the issue.
133168
Auto,
134-
/// Always print styles.
169+
/// Try very hard to print styles.
135170
Always,
136171
/// Never print styles.
137172
Never,
@@ -143,35 +178,48 @@ impl Default for WriteStyle {
143178
}
144179
}
145180

181+
/// A terminal target with color awareness.
146182
pub(crate) struct Writer(BufferWriter);
147183

184+
/// A builder for a terminal writer.
185+
///
186+
/// The target and style choice can be configured before building.
148187
pub(crate) struct Builder {
149188
target: Target,
150189
write_style: WriteStyle,
151190
}
152191

153192
impl Builder {
193+
/// Initialize the writer builder with defaults.
154194
pub fn new() -> Self {
155195
Builder {
156196
target: Default::default(),
157197
write_style: Default::default(),
158198
}
159199
}
160200

201+
/// Set the target to write to.
161202
pub fn target(&mut self, target: Target) -> &mut Self {
162203
self.target = target;
163204
self
164205
}
165206

207+
/// Parses a style choice string.
208+
///
209+
/// See the [Disabling colors] section for more details.
210+
///
211+
/// [Disabling colors]: ../index.html#disabling-colors
166212
pub fn parse(&mut self, write_style: &str) -> &mut Self {
167213
self.write_style(parse_write_style(write_style))
168214
}
169215

216+
/// Whether or not to print style characters when writing.
170217
pub fn write_style(&mut self, write_style: WriteStyle) -> &mut Self {
171218
self.write_style = write_style;
172219
self
173220
}
174221

222+
/// Build a terminal writer.
175223
pub fn build(&mut self) -> Writer {
176224
let color_choice = match self.write_style {
177225
WriteStyle::Auto => ColorChoice::Auto,
@@ -304,15 +352,6 @@ impl Style {
304352
}
305353
}
306354

307-
/// An [RFC3339] formatted timestamp.
308-
///
309-
/// The timestamp implements [`Display`] and can be written to a [`Formatter`].
310-
///
311-
/// [RFC3339]: https://www.ietf.org/rfc/rfc3339.txt
312-
/// [`Display`]: https://doc.rust-lang.org/stable/std/fmt/trait.Display.html
313-
/// [`Formatter`]: struct.Formatter.html
314-
pub struct Timestamp(DateTime<Utc>);
315-
316355
impl Formatter {
317356
pub(crate) fn new(writer: &Writer) -> Self {
318357
Formatter {

src/lib.rs

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,14 @@
130130
//! ## Disabling colors
131131
//!
132132
//! Colors and other styles can be configured with the `RUST_LOG_STYLE`
133-
//! environment variable.
134-
//! It accepts the following values:
133+
//! environment variable. It accepts the following values:
135134
//!
136135
//! * `auto` (default) will attempt to print style characters, but don't force the issue.
136+
//! If the console isn't available on Windows, or if TERM=dumb, for example, then don't print colors.
137137
//! * `always` will always print style characters even if they aren't supported by the terminal.
138+
//! This includes emitting ANSI colors on Windows if the console API is unavailable.
138139
//! * `never` will never print style characters.
139-
//!
140+
//!
140141
//! [log-crate-url]: https://docs.rs/log/
141142
142143
#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
@@ -281,6 +282,32 @@ impl Builder {
281282
}
282283

283284
/// Initializes the log builder from the environment.
285+
///
286+
/// The variables used to read configuration from can be tweaked before
287+
/// passing in.
288+
///
289+
/// # Examples
290+
///
291+
/// Initialise a logger using the default environment variables:
292+
///
293+
/// ```
294+
/// use env_logger::{Builder, Env};
295+
///
296+
/// let mut builder = Builder::from_env(Env::default());
297+
/// builder.init();
298+
/// ```
299+
///
300+
/// Initialise a logger using the `MY_LOG` variable for filtering and
301+
/// `MY_LOG_STYLE` for whether or not to write styles:
302+
///
303+
/// ```
304+
/// use env_logger::{Builder, Env};
305+
///
306+
/// let env = Env::new().filter("MY_LOG").write_style("MY_LOG_STYLE");
307+
///
308+
/// let mut builder = Builder::from_env(env);
309+
/// builder.init();
310+
/// ```
284311
pub fn from_env<'a, E>(env: E) -> Self
285312
where
286313
E: Into<Env<'a>>
@@ -564,10 +591,29 @@ pub fn init() {
564591
}
565592

566593
/// Attempts to initialize the global logger with an env logger from the given
567-
/// environment variable.
594+
/// environment variables.
568595
///
569596
/// This should be called early in the execution of a Rust program. Any log
570597
/// events that occur before initialization will be ignored.
598+
///
599+
/// # Examples
600+
///
601+
/// Initialise a logger using the `MY_LOG` environment variable for filters
602+
/// and `MY_LOG_STYLE` for writing colors:
603+
///
604+
/// ```
605+
/// # extern crate env_logger;
606+
/// use env_logger::{Builder, Env};
607+
///
608+
/// # fn run() -> Result<(), Box<::std::error::Error>> {
609+
/// let env = Env::new().filter("MY_LOG").write_style("MY_LOG_STYLE");
610+
///
611+
/// env_logger::try_init_from_env(env)?;
612+
///
613+
/// Ok(())
614+
/// # }
615+
/// # fn main() { run().unwrap(); }
616+
/// ```
571617
///
572618
/// # Errors
573619
///
@@ -583,10 +629,23 @@ where
583629
}
584630

585631
/// Initializes the global logger with an env logger from the given environment
586-
/// variable.
632+
/// variables.
587633
///
588634
/// This should be called early in the execution of a Rust program. Any log
589635
/// events that occur before initialization will be ignored.
636+
///
637+
/// # Examples
638+
///
639+
/// Initialise a logger using the `MY_LOG` environment variable for filters
640+
/// and `MY_LOG_STYLE` for writing colors:
641+
///
642+
/// ```
643+
/// use env_logger::{Builder, Env};
644+
///
645+
/// let env = Env::new().filter("MY_LOG").write_style("MY_LOG_STYLE");
646+
///
647+
/// env_logger::init_from_env(env);
648+
/// ```
590649
///
591650
/// # Panics
592651
///

0 commit comments

Comments
 (0)