@@ -38,7 +38,7 @@ pub const DEFAULT_WRITE_STYLE_ENV: &str = "RUST_LOG_STYLE";
3838pub struct Builder {
3939 filter : env_filter:: Builder ,
4040 writer : writer:: Builder ,
41- format : fmt:: Builder ,
41+ format : fmt:: ConfigurableFormat ,
4242 built : bool ,
4343}
4444
@@ -211,7 +211,8 @@ impl Builder {
211211 self . parse_env ( Env :: default ( ) )
212212 }
213213
214- /// Sets the format function for formatting the log output.
214+ /// Sets the format function for formatting the log output,
215+ /// and builds the Logger.
215216 ///
216217 /// This function is called on each record logged and should format the
217218 /// log record and output it to the given [`Formatter`].
@@ -234,45 +235,43 @@ impl Builder {
234235 ///
235236 /// let mut builder = Builder::new();
236237 ///
237- /// builder.format (|buf, record| writeln!(buf, "{}", record.args()));
238+ /// builder.build_with_format_fn (|buf, record| writeln!(buf, "{}", record.args()));
238239 /// ```
239240 ///
240241 /// [`Formatter`]: fmt/struct.Formatter.html
241242 /// [`String`]: https://doc.rust-lang.org/stable/std/string/struct.String.html
242243 /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html
243- pub fn format < F > ( & mut self , format : F ) -> & mut Self
244+ pub fn build_with_format_fn < F > ( & mut self , format : F ) -> Logger
244245 where
245246 F : Fn ( & mut Formatter , & Record < ' _ > ) -> io:: Result < ( ) > + Sync + Send + ' static ,
246247 {
247- self . format . custom_format = Some ( Box :: new ( format) ) ;
248- self
249- }
248+ assert ! ( !self . built, "attempt to re-use consumed builder" ) ;
249+ self . built = true ;
250250
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
251+ Logger {
252+ writer : self . writer . build ( ) ,
253+ filter : self . filter . build ( ) ,
254+ format : Box :: new ( format) ,
255+ }
257256 }
258257
259258 /// Whether or not to write the level in the default format.
260259 pub fn format_level ( & mut self , write : bool ) -> & mut Self {
261- self . format . default_format . level ( write) ;
260+ self . format . level ( write) ;
262261 self
263262 }
264263
265264 /// Whether or not to write the source file path in the default format.
266265 pub fn format_file ( & mut self , write : bool ) -> & mut Self {
267- self . format . default_format . file ( write) ;
266+ self . format . file ( write) ;
268267 self
269268 }
270269
271270 /// Whether or not to write the source line number path in the default format.
272271 ///
273272 /// Only has effect if `format_file` is also enabled
274273 pub fn format_line_number ( & mut self , write : bool ) -> & mut Self {
275- self . format . default_format . line_number ( write) ;
274+ self . format . line_number ( write) ;
276275 self
277276 }
278277
@@ -287,26 +286,26 @@ impl Builder {
287286
288287 /// Whether or not to write the module path in the default format.
289288 pub fn format_module_path ( & mut self , write : bool ) -> & mut Self {
290- self . format . default_format . module_path ( write) ;
289+ self . format . module_path ( write) ;
291290 self
292291 }
293292
294293 /// Whether or not to write the target in the default format.
295294 pub fn format_target ( & mut self , write : bool ) -> & mut Self {
296- self . format . default_format . target ( write) ;
295+ self . format . target ( write) ;
297296 self
298297 }
299298
300299 /// Configures the amount of spaces to use to indent multiline log records.
301300 /// A value of `None` disables any kind of indentation.
302301 pub fn format_indent ( & mut self , indent : Option < usize > ) -> & mut Self {
303- self . format . default_format . indent ( indent) ;
302+ self . format . indent ( indent) ;
304303 self
305304 }
306305
307306 /// Configures if timestamp should be included and in what precision.
308307 pub fn format_timestamp ( & mut self , timestamp : Option < fmt:: TimestampPrecision > ) -> & mut Self {
309- self . format . default_format . timestamp ( timestamp) ;
308+ self . format . timestamp ( timestamp) ;
310309 self
311310 }
312311
@@ -332,7 +331,7 @@ impl Builder {
332331
333332 /// Configures the end of line suffix.
334333 pub fn format_suffix ( & mut self , suffix : & ' static str ) -> & mut Self {
335- self . format . default_format . suffix ( suffix) ;
334+ self . format . suffix ( suffix) ;
336335 self
337336 }
338337
@@ -351,7 +350,7 @@ impl Builder {
351350 where
352351 F : Fn ( & mut Formatter , & dyn log:: kv:: Source ) -> io:: Result < ( ) > + Sync + Send + ' static ,
353352 {
354- self . format . default_format . key_values ( format) ;
353+ self . format . key_values ( format) ;
355354 self
356355 }
357356
@@ -497,15 +496,7 @@ impl Builder {
497496 /// library has already initialized a global logger.
498497 pub fn try_init ( & mut self ) -> Result < ( ) , SetLoggerError > {
499498 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
499+ logger. try_init ( )
509500 }
510501
511502 /// Initializes the global logger with the built env logger.
@@ -533,7 +524,7 @@ impl Builder {
533524 Logger {
534525 writer : self . writer . build ( ) ,
535526 filter : self . filter . build ( ) ,
536- format : self . format . build ( ) ,
527+ format : Box :: new ( std :: mem :: take ( & mut self . format ) ) ,
537528 }
538529 }
539530}
@@ -641,6 +632,17 @@ impl Logger {
641632 pub fn matches ( & self , record : & Record < ' _ > ) -> bool {
642633 self . filter . matches ( record)
643634 }
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+ }
644646}
645647
646648impl Log for Logger {
0 commit comments