Skip to content

Commit 595ff06

Browse files
committed
refactor: remove fmt::Builder, and retool Builder::format into build_with_format_fn
1 parent b8ecedc commit 595ff06

File tree

6 files changed

+82
-110
lines changed

6 files changed

+82
-110
lines changed

examples/custom_format.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn main() {
2929
.write_style("MY_LOG_STYLE");
3030

3131
Builder::from_env(env)
32-
.format(|buf, record| {
32+
.build_with_format_fn(|buf, record| {
3333
// We are reusing `anstyle` but there are `anstyle-*` crates to adapt it to your
3434
// preferred styling crate.
3535
let warn_style = buf.default_level_style(log::Level::Warn);
@@ -41,7 +41,8 @@ fn main() {
4141
record.args()
4242
)
4343
})
44-
.init();
44+
.try_init()
45+
.unwrap();
4546
}
4647

4748
init_logger();

examples/syslog_friendly_format.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::io::Write;
33
fn main() {
44
match std::env::var("RUST_LOG_STYLE") {
55
Ok(s) if s == "SYSTEMD" => env_logger::builder()
6-
.format(|buf, record| {
6+
.build_with_format_fn(|buf, record| {
77
writeln!(
88
buf,
99
"<{}>{}: {}",
@@ -18,7 +18,8 @@ fn main() {
1818
record.args()
1919
)
2020
})
21-
.init(),
21+
.try_init()
22+
.unwrap(),
2223
_ => env_logger::init(),
2324
};
2425
}

src/fmt/humantime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl Formatter {
1515
///
1616
/// let mut builder = env_logger::Builder::new();
1717
///
18-
/// builder.format(|buf, record| {
18+
/// builder.build_with_format_fn(|buf, record| {
1919
/// let ts = buf.timestamp();
2020
///
2121
/// writeln!(buf, "{}: {}: {}", ts, record.level(), record.args())

src/fmt/mod.rs

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//!
88
//! # Formatting log records
99
//!
10-
//! The format used to print log records can be customised using the [`Builder::format`]
10+
//! The format used to print log records can be customised using the [`Builder::build_with_format_fn`]
1111
//! method.
1212
//!
1313
//! Terminal styling is done through ANSI escape codes and will be adapted to the capabilities of
@@ -25,7 +25,7 @@
2525
//!
2626
//! let mut builder = env_logger::Builder::new();
2727
//!
28-
//! builder.format(|buf, record| {
28+
//! builder.build_with_format_fn(|buf, record| {
2929
//! writeln!(buf, "{}: {}",
3030
//! record.level(),
3131
//! record.args())
@@ -61,7 +61,7 @@ use std::cell::RefCell;
6161
use std::fmt::Display;
6262
use std::io::prelude::Write;
6363
use std::rc::Rc;
64-
use std::{fmt, io, mem};
64+
use std::{fmt, io};
6565

6666
#[cfg(feature = "color")]
6767
use log::Level;
@@ -125,7 +125,7 @@ impl Default for TimestampPrecision {
125125
///
126126
/// let mut builder = env_logger::Builder::new();
127127
///
128-
/// builder.format(|buf, record| writeln!(buf, "{}: {}", record.level(), record.args()));
128+
/// builder.build_with_format_fn(|buf, record| writeln!(buf, "{}: {}", record.level(), record.args()));
129129
/// ```
130130
///
131131
/// [`Write`]: std::io::Write
@@ -215,38 +215,6 @@ where
215215

216216
pub(crate) type FormatFn = Box<dyn RecordFormat + Sync + Send>;
217217

218-
#[derive(Default)]
219-
pub(crate) struct Builder {
220-
pub(crate) default_format: ConfigurableFormat,
221-
pub(crate) custom_format: Option<FormatFn>,
222-
built: bool,
223-
}
224-
225-
impl Builder {
226-
/// Convert the format into a callable function.
227-
///
228-
/// If the `custom_format` is `Some`, then any `default_format` switches are ignored.
229-
/// If the `custom_format` is `None`, then a default format is returned.
230-
/// Any `default_format` switches set to `false` won't be written by the format.
231-
pub(crate) fn build(&mut self) -> FormatFn {
232-
assert!(!self.built, "attempt to re-use consumed builder");
233-
234-
let built = mem::replace(
235-
self,
236-
Builder {
237-
built: true,
238-
..Default::default()
239-
},
240-
);
241-
242-
if let Some(fmt) = built.custom_format {
243-
fmt
244-
} else {
245-
Box::new(built.default_format)
246-
}
247-
}
248-
}
249-
250218
#[cfg(feature = "color")]
251219
type SubtleStyle = StyledValue<&'static str>;
252220
#[cfg(not(feature = "color"))]

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,10 @@
235235
//! use std::io::Write;
236236
//!
237237
//! env_logger::builder()
238-
//! .format(|buf, record| {
238+
//! .build_with_format_fn(|buf, record| {
239239
//! writeln!(buf, "{}: {}", record.level(), record.args())
240240
//! })
241-
//! .init();
241+
//! .try_init().unwrap();
242242
//! ```
243243
//!
244244
//! See the [`fmt`] module for more details about custom formats.

src/logger.rs

Lines changed: 69 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ pub const DEFAULT_WRITE_STYLE_ENV: &str = "RUST_LOG_STYLE";
2727
/// let mut builder = Builder::from_default_env();
2828
///
2929
/// builder
30-
/// .format(|buf, record| writeln!(buf, "{} - {}", record.level(), record.args()))
3130
/// .filter(None, LevelFilter::Info)
32-
/// .init();
31+
/// .build_with_format_fn(|buf, record| writeln!(buf, "{} - {}", record.level(), record.args()))
32+
/// .try_init().unwrap();
3333
///
3434
/// error!("error message");
3535
/// info!("info message");
@@ -38,7 +38,7 @@ pub const DEFAULT_WRITE_STYLE_ENV: &str = "RUST_LOG_STYLE";
3838
pub 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,68 +211,23 @@ impl Builder {
211211
self.parse_env(Env::default())
212212
}
213213

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-
259214
/// Whether or not to write the level in the default format.
260215
pub fn format_level(&mut self, write: bool) -> &mut Self {
261-
self.format.default_format.level(write);
216+
self.format.level(write);
262217
self
263218
}
264219

265220
/// Whether or not to write the source file path in the default format.
266221
pub fn format_file(&mut self, write: bool) -> &mut Self {
267-
self.format.default_format.file(write);
222+
self.format.file(write);
268223
self
269224
}
270225

271226
/// Whether or not to write the source line number path in the default format.
272227
///
273228
/// Only has effect if `format_file` is also enabled
274229
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);
276231
self
277232
}
278233

@@ -287,26 +242,26 @@ impl Builder {
287242

288243
/// Whether or not to write the module path in the default format.
289244
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);
291246
self
292247
}
293248

294249
/// Whether or not to write the target in the default format.
295250
pub fn format_target(&mut self, write: bool) -> &mut Self {
296-
self.format.default_format.target(write);
251+
self.format.target(write);
297252
self
298253
}
299254

300255
/// Configures the amount of spaces to use to indent multiline log records.
301256
/// A value of `None` disables any kind of indentation.
302257
pub fn format_indent(&mut self, indent: Option<usize>) -> &mut Self {
303-
self.format.default_format.indent(indent);
258+
self.format.indent(indent);
304259
self
305260
}
306261

307262
/// Configures if timestamp should be included and in what precision.
308263
pub fn format_timestamp(&mut self, timestamp: Option<fmt::TimestampPrecision>) -> &mut Self {
309-
self.format.default_format.timestamp(timestamp);
264+
self.format.timestamp(timestamp);
310265
self
311266
}
312267

@@ -332,7 +287,7 @@ impl Builder {
332287

333288
/// Configures the end of line suffix.
334289
pub fn format_suffix(&mut self, suffix: &'static str) -> &mut Self {
335-
self.format.default_format.suffix(suffix);
290+
self.format.suffix(suffix);
336291
self
337292
}
338293

@@ -351,7 +306,7 @@ impl Builder {
351306
where
352307
F: Fn(&mut Formatter, &dyn log::kv::Source) -> io::Result<()> + Sync + Send + 'static,
353308
{
354-
self.format.default_format.key_values(format);
309+
self.format.key_values(format);
355310
self
356311
}
357312

@@ -497,15 +452,7 @@ impl Builder {
497452
/// library has already initialized a global logger.
498453
pub fn try_init(&mut self) -> Result<(), SetLoggerError> {
499454
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()
509456
}
510457

511458
/// Initializes the global logger with the built env logger.
@@ -533,7 +480,51 @@ impl Builder {
533480
Logger {
534481
writer: self.writer.build(),
535482
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),
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

646648
impl Log for Logger {

0 commit comments

Comments
 (0)