@@ -27,9 +27,9 @@ pub const DEFAULT_WRITE_STYLE_ENV: &str = "RUST_LOG_STYLE";
27
27
/// let mut builder = Builder::from_default_env();
28
28
///
29
29
/// builder
30
- /// .format(|buf, record| writeln!(buf, "{} - {}", record.level(), record.args()))
31
30
/// .filter(None, LevelFilter::Info)
32
- /// .init();
31
+ /// .build_with_format_fn(|buf, record| writeln!(buf, "{} - {}", record.level(), record.args()))
32
+ /// .try_init().unwrap();
33
33
///
34
34
/// error!("error message");
35
35
/// info!("info message");
@@ -38,7 +38,7 @@ pub const DEFAULT_WRITE_STYLE_ENV: &str = "RUST_LOG_STYLE";
38
38
pub struct Builder {
39
39
filter : env_filter:: Builder ,
40
40
writer : writer:: Builder ,
41
- format : fmt:: Builder ,
41
+ format : fmt:: ConfigurableFormat ,
42
42
built : bool ,
43
43
}
44
44
@@ -211,68 +211,23 @@ impl Builder {
211
211
self . parse_env ( Env :: default ( ) )
212
212
}
213
213
214
- /// Sets the format function for formatting the log output.
215
- ///
216
- /// This function is called on each record logged and should format the
217
- /// log record and output it to the given [`Formatter`].
218
- ///
219
- /// The format function is expected to output the string directly to the
220
- /// `Formatter` so that implementations can use the [`std::fmt`] macros
221
- /// to format and output without intermediate heap allocations. The default
222
- /// `env_logger` formatter takes advantage of this.
223
- ///
224
- /// When the `color` feature is enabled, styling via ANSI escape codes is supported and the
225
- /// output will automatically respect [`Builder::write_style`].
226
- ///
227
- /// # Examples
228
- ///
229
- /// Use a custom format to write only the log message:
230
- ///
231
- /// ```
232
- /// use std::io::Write;
233
- /// use env_logger::Builder;
234
- ///
235
- /// let mut builder = Builder::new();
236
- ///
237
- /// builder.format(|buf, record| writeln!(buf, "{}", record.args()));
238
- /// ```
239
- ///
240
- /// [`Formatter`]: fmt/struct.Formatter.html
241
- /// [`String`]: https://doc.rust-lang.org/stable/std/string/struct.String.html
242
- /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html
243
- pub fn format < F > ( & mut self , format : F ) -> & mut Self
244
- where
245
- F : Fn ( & mut Formatter , & Record < ' _ > ) -> io:: Result < ( ) > + Sync + Send + ' static ,
246
- {
247
- self . format . custom_format = Some ( Box :: new ( format) ) ;
248
- self
249
- }
250
-
251
- /// Use the default format.
252
- ///
253
- /// This method will clear any custom format set on the builder.
254
- pub fn default_format ( & mut self ) -> & mut Self {
255
- self . format = Default :: default ( ) ;
256
- self
257
- }
258
-
259
214
/// Whether or not to write the level in the default format.
260
215
pub fn format_level ( & mut self , write : bool ) -> & mut Self {
261
- self . format . default_format . level ( write) ;
216
+ self . format . level ( write) ;
262
217
self
263
218
}
264
219
265
220
/// Whether or not to write the source file path in the default format.
266
221
pub fn format_file ( & mut self , write : bool ) -> & mut Self {
267
- self . format . default_format . file ( write) ;
222
+ self . format . file ( write) ;
268
223
self
269
224
}
270
225
271
226
/// Whether or not to write the source line number path in the default format.
272
227
///
273
228
/// Only has effect if `format_file` is also enabled
274
229
pub fn format_line_number ( & mut self , write : bool ) -> & mut Self {
275
- self . format . default_format . line_number ( write) ;
230
+ self . format . line_number ( write) ;
276
231
self
277
232
}
278
233
@@ -287,26 +242,26 @@ impl Builder {
287
242
288
243
/// Whether or not to write the module path in the default format.
289
244
pub fn format_module_path ( & mut self , write : bool ) -> & mut Self {
290
- self . format . default_format . module_path ( write) ;
245
+ self . format . module_path ( write) ;
291
246
self
292
247
}
293
248
294
249
/// Whether or not to write the target in the default format.
295
250
pub fn format_target ( & mut self , write : bool ) -> & mut Self {
296
- self . format . default_format . target ( write) ;
251
+ self . format . target ( write) ;
297
252
self
298
253
}
299
254
300
255
/// Configures the amount of spaces to use to indent multiline log records.
301
256
/// A value of `None` disables any kind of indentation.
302
257
pub fn format_indent ( & mut self , indent : Option < usize > ) -> & mut Self {
303
- self . format . default_format . indent ( indent) ;
258
+ self . format . indent ( indent) ;
304
259
self
305
260
}
306
261
307
262
/// Configures if timestamp should be included and in what precision.
308
263
pub fn format_timestamp ( & mut self , timestamp : Option < fmt:: TimestampPrecision > ) -> & mut Self {
309
- self . format . default_format . timestamp ( timestamp) ;
264
+ self . format . timestamp ( timestamp) ;
310
265
self
311
266
}
312
267
@@ -332,7 +287,7 @@ impl Builder {
332
287
333
288
/// Configures the end of line suffix.
334
289
pub fn format_suffix ( & mut self , suffix : & ' static str ) -> & mut Self {
335
- self . format . default_format . suffix ( suffix) ;
290
+ self . format . suffix ( suffix) ;
336
291
self
337
292
}
338
293
@@ -351,7 +306,7 @@ impl Builder {
351
306
where
352
307
F : Fn ( & mut Formatter , & dyn log:: kv:: Source ) -> io:: Result < ( ) > + Sync + Send + ' static ,
353
308
{
354
- self . format . default_format . key_values ( format) ;
309
+ self . format . key_values ( format) ;
355
310
self
356
311
}
357
312
@@ -497,15 +452,7 @@ impl Builder {
497
452
/// library has already initialized a global logger.
498
453
pub fn try_init ( & mut self ) -> Result < ( ) , SetLoggerError > {
499
454
let logger = self . build ( ) ;
500
-
501
- let max_level = logger. filter ( ) ;
502
- let r = log:: set_boxed_logger ( Box :: new ( logger) ) ;
503
-
504
- if r. is_ok ( ) {
505
- log:: set_max_level ( max_level) ;
506
- }
507
-
508
- r
455
+ logger. try_init ( )
509
456
}
510
457
511
458
/// Initializes the global logger with the built env logger.
@@ -533,7 +480,51 @@ impl Builder {
533
480
Logger {
534
481
writer : self . writer . build ( ) ,
535
482
filter : self . filter . build ( ) ,
536
- format : self . format . build ( ) ,
483
+ format : Box :: new ( std:: mem:: take ( & mut self . format ) ) ,
484
+ }
485
+ }
486
+
487
+ /// Sets the format function for formatting the log output,
488
+ /// and builds the Logger.
489
+ ///
490
+ /// This function is called on each record logged and should format the
491
+ /// log record and output it to the given [`Formatter`].
492
+ ///
493
+ /// The format function is expected to output the string directly to the
494
+ /// `Formatter` so that implementations can use the [`std::fmt`] macros
495
+ /// to format and output without intermediate heap allocations. The default
496
+ /// `env_logger` formatter takes advantage of this.
497
+ ///
498
+ /// When the `color` feature is enabled, styling via ANSI escape codes is supported and the
499
+ /// output will automatically respect [`Builder::write_style`].
500
+ ///
501
+ /// # Examples
502
+ ///
503
+ /// Use a custom format to write only the log message:
504
+ ///
505
+ /// ```
506
+ /// use std::io::Write;
507
+ /// use env_logger::Builder;
508
+ ///
509
+ /// let mut builder = Builder::new();
510
+ ///
511
+ /// builder.build_with_format_fn(|buf, record| writeln!(buf, "{}", record.args()));
512
+ /// ```
513
+ ///
514
+ /// [`Formatter`]: fmt/struct.Formatter.html
515
+ /// [`String`]: https://doc.rust-lang.org/stable/std/string/struct.String.html
516
+ /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html
517
+ pub fn build_with_format_fn < F > ( & mut self , format : F ) -> Logger
518
+ where
519
+ F : Fn ( & mut Formatter , & Record < ' _ > ) -> io:: Result < ( ) > + Sync + Send + ' static ,
520
+ {
521
+ assert ! ( !self . built, "attempt to re-use consumed builder" ) ;
522
+ self . built = true ;
523
+
524
+ Logger {
525
+ writer : self . writer . build ( ) ,
526
+ filter : self . filter . build ( ) ,
527
+ format : Box :: new ( format) ,
537
528
}
538
529
}
539
530
}
@@ -641,6 +632,17 @@ impl Logger {
641
632
pub fn matches ( & self , record : & Record < ' _ > ) -> bool {
642
633
self . filter . matches ( record)
643
634
}
635
+
636
+ pub fn try_init ( self ) -> Result < ( ) , SetLoggerError > {
637
+ let max_level = self . filter ( ) ;
638
+ let r = log:: set_boxed_logger ( Box :: new ( self ) ) ;
639
+
640
+ if r. is_ok ( ) {
641
+ log:: set_max_level ( max_level) ;
642
+ }
643
+
644
+ r
645
+ }
644
646
}
645
647
646
648
impl Log for Logger {
0 commit comments