| 
139 | 139 | //! * `error,hello=warn/[0-9]scopes` turn on global error logging and also  | 
140 | 140 | //!   warn for hello. In both cases the log message must include a single digit  | 
141 | 141 | //!   number followed by 'scopes'.  | 
 | 142 | +//!   | 
 | 143 | +//! ## Capturing logs in tests  | 
 | 144 | +//!   | 
 | 145 | +//! Records logged during `cargo test` will not be captured by the test harness by default.  | 
 | 146 | +//! The [`Builder::is_test`] method can be used in unit tests to ensure logs will be captured:  | 
 | 147 | +//!   | 
 | 148 | +//! ```  | 
 | 149 | +//! # #[macro_use] extern crate log;  | 
 | 150 | +//! # extern crate env_logger;  | 
 | 151 | +//! # fn main() {}  | 
 | 152 | +//! #[cfg(test)]  | 
 | 153 | +//! mod tests {  | 
 | 154 | +//!     fn init() {  | 
 | 155 | +//!         let _ = env_logger::builder().is_test(true).try_init();  | 
 | 156 | +//!     }  | 
 | 157 | +//!   | 
 | 158 | +//!     #[test]  | 
 | 159 | +//!     fn it_works() {  | 
 | 160 | +//!         info!("This record will be captured by `cargo test`");  | 
 | 161 | +//!   | 
 | 162 | +//!         assert_eq!(2, 1 + 1);  | 
 | 163 | +//!     }  | 
 | 164 | +//! }  | 
 | 165 | +//! ```  | 
 | 166 | +//!   | 
 | 167 | +//! Enabling test capturing comes at the expense of color and other style support  | 
 | 168 | +//! and may have performance implications.  | 
142 | 169 | //!  | 
143 | 170 | //! ## Disabling colors  | 
144 | 171 | //!  | 
 | 
157 | 184 | //! The following example excludes the timestamp from the log output:  | 
158 | 185 | //!   | 
159 | 186 | //! ```  | 
160 |  | -//! use env_logger::Builder;  | 
161 |  | -//!  | 
162 |  | -//! Builder::from_default_env()  | 
 | 187 | +//! env_logger::builder()  | 
163 | 188 | //!     .default_format_timestamp(false)  | 
164 | 189 | //!     .init();  | 
165 | 190 | //! ```  | 
 | 
180 | 205 | //!   | 
181 | 206 | //! ```  | 
182 | 207 | //! use std::io::Write;  | 
183 |  | -//! use env_logger::Builder;  | 
184 | 208 | //!  | 
185 |  | -//! Builder::from_default_env()  | 
 | 209 | +//! env_logger::builder()  | 
186 | 210 | //!     .format(|buf, record| {  | 
187 | 211 | //!         writeln!(buf, "{}: {}", record.level(), record.args())  | 
188 | 212 | //!     })  | 
 | 
199 | 223 | //! isn't set:  | 
200 | 224 | //!   | 
201 | 225 | //! ```  | 
202 |  | -//! use env_logger::{Builder, Env};  | 
 | 226 | +//! use env_logger::Env;  | 
203 | 227 | //!  | 
204 |  | -//! Builder::from_env(Env::default().default_filter_or("warn")).init();  | 
 | 228 | +//! env_logger::from_env(Env::default().default_filter_or("warn")).init();  | 
205 | 229 | //! ```  | 
206 | 230 | //!   | 
207 | 231 | //! [log-crate-url]: https://docs.rs/log/  | 
208 | 232 | //! [`Builder`]: struct.Builder.html  | 
 | 233 | +//! [`Builder::is_test`]: struct.Builder.html#method.is_test  | 
209 | 234 | //! [`Env`]: struct.Env.html  | 
210 | 235 | //! [`fmt`]: fmt/index.html  | 
211 | 236 | 
  | 
@@ -404,7 +429,7 @@ impl Builder {  | 
404 | 429 |         let env = env.into();  | 
405 | 430 | 
 
  | 
406 | 431 |         if let Some(s) = env.get_filter() {  | 
407 |  | -            builder.parse(&s);  | 
 | 432 | +            builder.parse_filters(&s);  | 
408 | 433 |         }  | 
409 | 434 | 
 
  | 
410 | 435 |         if let Some(s) = env.get_write_style() {  | 
@@ -579,7 +604,16 @@ impl Builder {  | 
579 | 604 |     /// environment variable.  | 
580 | 605 |     ///  | 
581 | 606 |     /// See the module documentation for more details.  | 
 | 607 | +    #[deprecated(since = "0.6.0", note = "use `parse_filters` instead.")]  | 
582 | 608 |     pub fn parse(&mut self, filters: &str) -> &mut Self {  | 
 | 609 | +        self.parse_filters(filters)  | 
 | 610 | +    }  | 
 | 611 | + | 
 | 612 | +    /// Parses the directives string in the same form as the `RUST_LOG`  | 
 | 613 | +    /// environment variable.  | 
 | 614 | +    ///  | 
 | 615 | +    /// See the module documentation for more details.  | 
 | 616 | +    pub fn parse_filters(&mut self, filters: &str) -> &mut Self {  | 
583 | 617 |         self.filter.parse(filters);  | 
584 | 618 |         self  | 
585 | 619 |     }  | 
@@ -630,7 +664,16 @@ impl Builder {  | 
630 | 664 |     ///  | 
631 | 665 |     /// See the module documentation for more details.  | 
632 | 666 |     pub fn parse_write_style(&mut self, write_style: &str) -> &mut Self {  | 
633 |  | -        self.writer.parse(write_style);  | 
 | 667 | +        self.writer.parse_write_style(write_style);  | 
 | 668 | +        self  | 
 | 669 | +    }  | 
 | 670 | + | 
 | 671 | +    /// Sets whether or not the logger will be used in unit tests.  | 
 | 672 | +    ///   | 
 | 673 | +    /// If `is_test` is `true` then the logger will allow the testing framework to  | 
 | 674 | +    /// capture log records rather than printing them to the terminal directly.  | 
 | 675 | +    pub fn is_test(&mut self, is_test: bool) -> &mut Self {  | 
 | 676 | +        self.writer.is_test(is_test);  | 
634 | 677 |         self  | 
635 | 678 |     }  | 
636 | 679 | 
 
  | 
@@ -1068,6 +1111,23 @@ where  | 
1068 | 1111 |     try_init_from_env(env).expect("env_logger::init_from_env should not be called after logger initialized");  | 
1069 | 1112 | }  | 
1070 | 1113 | 
 
  | 
 | 1114 | +/// Create a new builder with the default environment variables.  | 
 | 1115 | +///   | 
 | 1116 | +/// The builder can be configured before being initialized.  | 
 | 1117 | +pub fn builder() -> Builder {  | 
 | 1118 | +    Builder::from_default_env()  | 
 | 1119 | +}  | 
 | 1120 | + | 
 | 1121 | +/// Create a builder from the given environment variables.  | 
 | 1122 | +///   | 
 | 1123 | +/// The builder can be configured before being initialized.  | 
 | 1124 | +pub fn from_env<'a, E>(env: E) -> Builder  | 
 | 1125 | +where  | 
 | 1126 | +    E: Into<Env<'a>>  | 
 | 1127 | +{  | 
 | 1128 | +    Builder::from_env(env)  | 
 | 1129 | +}  | 
 | 1130 | + | 
1071 | 1131 | #[cfg(test)]  | 
1072 | 1132 | mod tests {  | 
1073 | 1133 |     use super::*;  | 
 | 
0 commit comments