Skip to content

Commit ec31808

Browse files
committed
refactor: move format/with_format from builder to formatter
1 parent b8ecedc commit ec31808

File tree

2 files changed

+48
-56
lines changed

2 files changed

+48
-56
lines changed

src/fmt/mod.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ impl fmt::Debug for Formatter {
200200
}
201201
}
202202

203-
pub(crate) trait RecordFormat {
203+
pub trait RecordFormat {
204204
fn format(&self, formatter: &mut Formatter, record: &Record<'_>) -> io::Result<()>;
205205
}
206206

@@ -217,8 +217,7 @@ pub(crate) type FormatFn = Box<dyn RecordFormat + Sync + Send>;
217217

218218
#[derive(Default)]
219219
pub(crate) struct Builder {
220-
pub(crate) default_format: ConfigurableFormat,
221-
pub(crate) custom_format: Option<FormatFn>,
220+
pub(crate) format: ConfigurableFormat,
222221
built: bool,
223222
}
224223

@@ -239,11 +238,7 @@ impl Builder {
239238
},
240239
);
241240

242-
if let Some(fmt) = built.custom_format {
243-
fmt
244-
} else {
245-
Box::new(built.default_format)
246-
}
241+
Box::new(built.format)
247242
}
248243
}
249244

src/logger.rs

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{borrow::Cow, cell::RefCell, env, io};
1+
use std::{borrow::Cow, cell::RefCell, env};
22

33
use log::{LevelFilter, Log, Metadata, Record, SetLoggerError};
44

@@ -211,43 +211,6 @@ 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-
251214
/// Use the default format.
252215
///
253216
/// This method will clear any custom format set on the builder.
@@ -258,21 +221,21 @@ impl Builder {
258221

259222
/// Whether or not to write the level in the default format.
260223
pub fn format_level(&mut self, write: bool) -> &mut Self {
261-
self.format.default_format.level(write);
224+
self.format.format.level(write);
262225
self
263226
}
264227

265228
/// Whether or not to write the source file path in the default format.
266229
pub fn format_file(&mut self, write: bool) -> &mut Self {
267-
self.format.default_format.file(write);
230+
self.format.format.file(write);
268231
self
269232
}
270233

271234
/// Whether or not to write the source line number path in the default format.
272235
///
273236
/// Only has effect if `format_file` is also enabled
274237
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);
276239
self
277240
}
278241

@@ -287,26 +250,26 @@ impl Builder {
287250

288251
/// Whether or not to write the module path in the default format.
289252
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);
291254
self
292255
}
293256

294257
/// Whether or not to write the target in the default format.
295258
pub fn format_target(&mut self, write: bool) -> &mut Self {
296-
self.format.default_format.target(write);
259+
self.format.format.target(write);
297260
self
298261
}
299262

300263
/// Configures the amount of spaces to use to indent multiline log records.
301264
/// A value of `None` disables any kind of indentation.
302265
pub fn format_indent(&mut self, indent: Option<usize>) -> &mut Self {
303-
self.format.default_format.indent(indent);
266+
self.format.format.indent(indent);
304267
self
305268
}
306269

307270
/// Configures if timestamp should be included and in what precision.
308271
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);
310273
self
311274
}
312275

@@ -332,7 +295,7 @@ impl Builder {
332295

333296
/// Configures the end of line suffix.
334297
pub fn format_suffix(&mut self, suffix: &'static str) -> &mut Self {
335-
self.format.default_format.suffix(suffix);
298+
self.format.format.suffix(suffix);
336299
self
337300
}
338301

@@ -349,9 +312,9 @@ impl Builder {
349312
#[cfg(feature = "kv")]
350313
pub fn format_key_values<F>(&mut self, format: F) -> &mut Self
351314
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,
353316
{
354-
self.format.default_format.key_values(format);
317+
self.format.format.key_values(format);
355318
self
356319
}
357320

@@ -641,6 +604,40 @@ impl Logger {
641604
pub fn matches(&self, record: &Record<'_>) -> bool {
642605
self.filter.matches(record)
643606
}
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+
}
644641
}
645642

646643
impl Log for Logger {

0 commit comments

Comments
 (0)