1
- use std:: { borrow:: Cow , cell:: RefCell , env, io } ;
1
+ use std:: { borrow:: Cow , cell:: RefCell , env} ;
2
2
3
3
use log:: { LevelFilter , Log , Metadata , Record , SetLoggerError } ;
4
4
@@ -211,43 +211,6 @@ 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
214
/// Use the default format.
252
215
///
253
216
/// This method will clear any custom format set on the builder.
@@ -258,21 +221,21 @@ impl Builder {
258
221
259
222
/// Whether or not to write the level in the default format.
260
223
pub fn format_level ( & mut self , write : bool ) -> & mut Self {
261
- self . format . default_format . level ( write) ;
224
+ self . format . format . level ( write) ;
262
225
self
263
226
}
264
227
265
228
/// Whether or not to write the source file path in the default format.
266
229
pub fn format_file ( & mut self , write : bool ) -> & mut Self {
267
- self . format . default_format . file ( write) ;
230
+ self . format . format . file ( write) ;
268
231
self
269
232
}
270
233
271
234
/// Whether or not to write the source line number path in the default format.
272
235
///
273
236
/// Only has effect if `format_file` is also enabled
274
237
pub fn format_line_number ( & mut self , write : bool ) -> & mut Self {
275
- self . format . default_format . line_number ( write) ;
238
+ self . format . format . line_number ( write) ;
276
239
self
277
240
}
278
241
@@ -287,26 +250,26 @@ impl Builder {
287
250
288
251
/// Whether or not to write the module path in the default format.
289
252
pub fn format_module_path ( & mut self , write : bool ) -> & mut Self {
290
- self . format . default_format . module_path ( write) ;
253
+ self . format . format . module_path ( write) ;
291
254
self
292
255
}
293
256
294
257
/// Whether or not to write the target in the default format.
295
258
pub fn format_target ( & mut self , write : bool ) -> & mut Self {
296
- self . format . default_format . target ( write) ;
259
+ self . format . format . target ( write) ;
297
260
self
298
261
}
299
262
300
263
/// Configures the amount of spaces to use to indent multiline log records.
301
264
/// A value of `None` disables any kind of indentation.
302
265
pub fn format_indent ( & mut self , indent : Option < usize > ) -> & mut Self {
303
- self . format . default_format . indent ( indent) ;
266
+ self . format . format . indent ( indent) ;
304
267
self
305
268
}
306
269
307
270
/// Configures if timestamp should be included and in what precision.
308
271
pub fn format_timestamp ( & mut self , timestamp : Option < fmt:: TimestampPrecision > ) -> & mut Self {
309
- self . format . default_format . timestamp ( timestamp) ;
272
+ self . format . format . timestamp ( timestamp) ;
310
273
self
311
274
}
312
275
@@ -332,7 +295,7 @@ impl Builder {
332
295
333
296
/// Configures the end of line suffix.
334
297
pub fn format_suffix ( & mut self , suffix : & ' static str ) -> & mut Self {
335
- self . format . default_format . suffix ( suffix) ;
298
+ self . format . format . suffix ( suffix) ;
336
299
self
337
300
}
338
301
@@ -349,9 +312,9 @@ impl Builder {
349
312
#[ cfg( feature = "kv" ) ]
350
313
pub fn format_key_values < F > ( & mut self , format : F ) -> & mut Self
351
314
where
352
- F : Fn ( & mut Formatter , & dyn log:: kv:: Source ) -> io:: Result < ( ) > + Sync + Send + ' static ,
315
+ F : Fn ( & mut Formatter , & dyn log:: kv:: Source ) -> std :: io:: Result < ( ) > + Sync + Send + ' static ,
353
316
{
354
- self . format . default_format . key_values ( format) ;
317
+ self . format . format . key_values ( format) ;
355
318
self
356
319
}
357
320
@@ -497,15 +460,7 @@ impl Builder {
497
460
/// library has already initialized a global logger.
498
461
pub fn try_init ( & mut self ) -> Result < ( ) , SetLoggerError > {
499
462
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
463
+ logger. try_init ( )
509
464
}
510
465
511
466
/// Initializes the global logger with the built env logger.
@@ -641,6 +596,51 @@ impl Logger {
641
596
pub fn matches ( & self , record : & Record < ' _ > ) -> bool {
642
597
self . filter . matches ( record)
643
598
}
599
+
600
+ /// Sets the format function for formatting the log output.
601
+ ///
602
+ /// This function is called on each record logged and should format the
603
+ /// log record and output it to the given [`Formatter`].
604
+ ///
605
+ /// The format function is expected to output the string directly to the
606
+ /// `Formatter` so that implementations can use the [`std::fmt`] macros
607
+ /// to format and output without intermediate heap allocations. The default
608
+ /// `env_logger` formatter takes advantage of this.
609
+ ///
610
+ /// When the `color` feature is enabled, styling via ANSI escape codes is supported and the
611
+ /// output will automatically respect [`Builder::write_style`].
612
+ ///
613
+ /// # Examples
614
+ ///
615
+ /// Use a custom format to write only the log message:
616
+ ///
617
+ /// ```
618
+ /// use std::io::Write;
619
+ /// use env_logger::Builder;
620
+ ///
621
+ /// let mut builder = Builder::new();
622
+ ///
623
+ /// builder.format(|buf, record| writeln!(buf, "{}", record.args()));
624
+ /// ```
625
+ ///
626
+ /// [`Formatter`]: fmt/struct.Formatter.html
627
+ /// [`String`]: https://doc.rust-lang.org/stable/std/string/struct.String.html
628
+ /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html
629
+ pub fn with_format ( mut self , format : FormatFn ) -> Self {
630
+ self . format = format;
631
+ self
632
+ }
633
+
634
+ pub fn try_init ( self ) -> Result < ( ) , SetLoggerError > {
635
+ let max_level = self . filter ( ) ;
636
+ let r = log:: set_boxed_logger ( Box :: new ( self ) ) ;
637
+
638
+ if r. is_ok ( ) {
639
+ log:: set_max_level ( max_level) ;
640
+ }
641
+
642
+ r
643
+ }
644
644
}
645
645
646
646
impl Log for Logger {
0 commit comments