Skip to content

Commit 299713a

Browse files
authored
Merge pull request #111 from KodrAus/chore/docs
Tidy up some docs
2 parents 3be818f + 3dcda3b commit 299713a

File tree

5 files changed

+69
-43
lines changed

5 files changed

+69
-43
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ before_script:
99
- pip install 'travis-cargo<0.2' --user && export PATH=$HOME/.local/bin:$PATH
1010
script:
1111
- cargo run -p ci
12+
- cargo run --example default
1213
after_success:
1314
- travis-cargo --only nightly doc-upload
1415

README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ $ RUST_LOG=my_lib=info cargo test
9393
Running target/debug/my_lib-...
9494

9595
running 2 tests
96-
[2017-11-09T02:12:24Z INFO my_lib::tests] logging from another test
97-
[2017-11-09T02:12:24Z INFO my_lib] add_one called with -8
96+
[INFO my_lib::tests] logging from another test
97+
[INFO my_lib] add_one called with -8
9898
test tests::it_handles_negative_numbers ... ok
99-
[2017-11-09T02:12:24Z INFO my_lib::tests] can log from the test too
100-
[2017-11-09T02:12:24Z INFO my_lib] add_one called with 2
99+
[INFO my_lib::tests] can log from the test too
100+
[INFO my_lib] add_one called with 2
101101
test tests::it_adds_one ... ok
102102

103103
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured
@@ -115,8 +115,8 @@ $ RUST_LOG=my_lib=info cargo test it_adds_one
115115
Running target/debug/my_lib-...
116116

117117
running 1 test
118-
[2017-11-09T02:12:24Z INFO my_lib::tests] can log from the test too
119-
[2017-11-09T02:12:24Z INFO my_lib] add_one called with 2
118+
[INFO my_lib::tests] can log from the test too
119+
[INFO my_lib] add_one called with 2
120120
test tests::it_adds_one ... ok
121121

122122
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
@@ -131,11 +131,9 @@ you can use the `Builder` to change the log target:
131131
use std::env;
132132
use env_logger::{Builder, Target};
133133

134-
let mut builder = Builder::new();
134+
let mut builder = Builder::from_default_env();
135135
builder.target(Target::Stdout);
136-
if env::var("RUST_LOG").is_ok() {
137-
builder.parse(&env::var("RUST_LOG").unwrap());
138-
}
136+
139137
builder.init();
140138
```
141139

src/fmt/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub use self::writer::glob::*;
4646
use self::writer::{Writer, Buffer};
4747

4848
pub(crate) mod glob {
49-
pub use super::{Target, WriteStyle, Formatter};
49+
pub use super::{Target, WriteStyle};
5050
}
5151

5252
/// A formatter to write logs into.
@@ -56,7 +56,8 @@ pub(crate) mod glob {
5656
///
5757
/// # Examples
5858
///
59-
/// Use the [`writeln`] macro to easily format a log record:
59+
/// Use the [`writeln`] macro to format a log record.
60+
/// An instance of a `Formatter` is passed to an `env_logger` format as `buf`:
6061
///
6162
/// ```
6263
/// use std::io::Write;
@@ -172,6 +173,9 @@ type SubtleStyle = StyledValue<'static, &'static str>;
172173
#[cfg(not(feature = "termcolor"))]
173174
type SubtleStyle = &'static str;
174175

176+
/// The default format.
177+
///
178+
/// This format needs to work with any combination of crate features.
175179
struct DefaultFormat<'a> {
176180
timestamp: bool,
177181
module_path: bool,

src/fmt/writer/termcolor/extern_impl.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ impl Formatter {
5252
pub fn default_level_style(&self, level: Level) -> Style {
5353
let mut level_style = self.style();
5454
match level {
55-
Level::Trace => level_style.set_color(Color::White),
56-
Level::Debug => level_style.set_color(Color::Black).set_intense(true),
55+
Level::Trace => level_style.set_color(Color::Black).set_intense(true),
56+
Level::Debug => level_style.set_color(Color::White),
5757
Level::Info => level_style.set_color(Color::Green),
5858
Level::Warn => level_style.set_color(Color::Yellow),
5959
Level::Error => level_style.set_color(Color::Red).set_bold(true),
@@ -332,7 +332,7 @@ impl Style {
332332
}
333333

334334
/// Wrap a value in the style by taking ownership of it.
335-
pub fn into_value<T>(&mut self, value: T) -> StyledValue<'static, T> {
335+
pub(crate) fn into_value<T>(&mut self, value: T) -> StyledValue<'static, T> {
336336
StyledValue {
337337
style: Cow::Owned(self.clone()),
338338
value

src/lib.rs

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -157,24 +157,11 @@
157157
//! The following example excludes the timestamp from the log output:
158158
//!
159159
//! ```
160-
//! #[macro_use] extern crate log;
161-
//! extern crate env_logger;
162-
//!
163-
//! use log::Level;
164-
//!
165-
//! fn main() {
166-
//! env_logger::Builder::from_default_env()
167-
//! .default_format_timestamp(false)
168-
//! .init();
160+
//! use env_logger::Builder;
169161
//!
170-
//! debug!("this is a debug {}", "message");
171-
//! error!("this is printed by default");
172-
//!
173-
//! if log_enabled!(Level::Info) {
174-
//! let x = 3 * 4; // expensive computation
175-
//! info!("the answer was: {}", x);
176-
//! }
177-
//! }
162+
//! Builder::from_default_env()
163+
//! .default_format_timestamp(false)
164+
//! .init();
178165
//! ```
179166
//!
180167
//! ### Stability of the default format
@@ -186,6 +173,24 @@
186173
//! If you want to capture or interpret the output of `env_logger` programmatically
187174
//! then you should use a custom format.
188175
//!
176+
//! ### Using a custom format
177+
//!
178+
//! Custom formats can be provided as closures to the [`Builder`].
179+
//! These closures take a [`Formatter`] and `log::Record` as arguments:
180+
//!
181+
//! ```
182+
//! use std::io::Write;
183+
//! use env_logger::Builder;
184+
//!
185+
//! Builder::from_default_env()
186+
//! .format(|buf, record| {
187+
//! writeln!(buf, "{}: {}", record.level(), record.args())
188+
//! })
189+
//! .init();
190+
//! ```
191+
//!
192+
//! See the [`fmt`] module for more details about custom formats.
193+
//!
189194
//! ## Specifying defaults for environment variables
190195
//!
191196
//! `env_logger` can read configuration from environment variables.
@@ -194,22 +199,15 @@
194199
//! isn't set:
195200
//!
196201
//! ```
197-
//! #[macro_use] extern crate log;
198-
//! extern crate env_logger;
202+
//! use env_logger::{Builder, Env};
199203
//!
200-
//! use log::Level;
201-
//!
202-
//! fn main() {
203-
//! let env = env_logger::Env::default()
204-
//! .filter_or(env_logger::DEFAULT_FILTER_ENV, "warn");
205-
//!
206-
//! env_logger::Builder::from_env(env).init();
207-
//! }
204+
//! Builder::from_env(Env::default().default_filter_or("warn")).init();
208205
//! ```
209206
//!
210207
//! [log-crate-url]: https://docs.rs/log/
211208
//! [`Builder`]: struct.Builder.html
212209
//! [`Env`]: struct.Env.html
210+
//! [`fmt`]: fmt/index.html
213211
214212
#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
215213
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
@@ -244,6 +242,7 @@ pub mod fmt;
244242
pub use self::fmt::glob::*;
245243

246244
use self::filter::Filter;
245+
use self::fmt::Formatter;
247246
use self::fmt::writer::{self, Writer};
248247

249248
/// The default name for the environment variable to read filters from.
@@ -457,7 +456,7 @@ impl Builder {
457456
///
458457
/// let mut builder = Builder::new();
459458
///
460-
/// builder.format(|buf, record| write!(buf, "{}", record.args()));
459+
/// builder.format(|buf, record| writeln!(buf, "{}", record.args()));
461460
/// ```
462461
///
463462
/// [`Formatter`]: fmt/struct.Formatter.html
@@ -836,6 +835,18 @@ impl<'a> Env<'a> {
836835
self
837836
}
838837

838+
/// Use the default environment variable to read the filter from.
839+
///
840+
/// If the variable is not set, the default value will be used.
841+
pub fn default_filter_or<V>(mut self, default: V) -> Self
842+
where
843+
V: Into<Cow<'a, str>>,
844+
{
845+
self.filter = Var::new_with_default(DEFAULT_FILTER_ENV, default);
846+
847+
self
848+
}
849+
839850
fn get_filter(&self) -> Option<String> {
840851
self.filter.get()
841852
}
@@ -863,6 +874,18 @@ impl<'a> Env<'a> {
863874
self
864875
}
865876

877+
/// Use the default environment variable to read the style from.
878+
///
879+
/// If the variable is not set, the default value will be used.
880+
pub fn default_write_style_or<V>(mut self, default: V) -> Self
881+
where
882+
V: Into<Cow<'a, str>>,
883+
{
884+
self.write_style = Var::new_with_default(DEFAULT_WRITE_STYLE_ENV, default);
885+
886+
self
887+
}
888+
866889
fn get_write_style(&self) -> Option<String> {
867890
self.write_style.get()
868891
}

0 commit comments

Comments
 (0)