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
@@ -641,6 +604,40 @@ impl Logger {
641
604
pub fn matches ( & self , record : & Record < ' _ > ) -> bool {
642
605
self . filter . matches ( record)
643
606
}
607
+
608
+ /// Sets the format function for formatting the log output.
609
+ ///
610
+ /// This function is called on each record logged and should format the
611
+ /// log record and output it to the given [`Formatter`].
612
+ ///
613
+ /// The format function is expected to output the string directly to the
614
+ /// `Formatter` so that implementations can use the [`std::fmt`] macros
615
+ /// to format and output without intermediate heap allocations. The default
616
+ /// `env_logger` formatter takes advantage of this.
617
+ ///
618
+ /// When the `color` feature is enabled, styling via ANSI escape codes is supported and the
619
+ /// output will automatically respect [`Builder::write_style`].
620
+ ///
621
+ /// # Examples
622
+ ///
623
+ /// Use a custom format to write only the log message:
624
+ ///
625
+ /// ```
626
+ /// use std::io::Write;
627
+ /// use env_logger::Builder;
628
+ ///
629
+ /// let mut builder = Builder::new();
630
+ ///
631
+ /// builder.format(|buf, record| writeln!(buf, "{}", record.args()));
632
+ /// ```
633
+ ///
634
+ /// [`Formatter`]: fmt/struct.Formatter.html
635
+ /// [`String`]: https://doc.rust-lang.org/stable/std/string/struct.String.html
636
+ /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html
637
+ pub fn with_format < F > ( & mut self , format : FormatFn ) -> & mut Self {
638
+ self . format = format;
639
+ self
640
+ }
644
641
}
645
642
646
643
impl Log for Logger {
0 commit comments