Skip to content

Commit 9315474

Browse files
committed
feat(fmt)!: Generalize custom formatter to 'RecordFormat'
This breaks type inference for basic custom formatters
1 parent 6459880 commit 9315474

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/fmt/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,18 @@ impl fmt::Debug for Formatter {
200200
}
201201
}
202202

203-
pub(crate) trait RecordFormat {
203+
/// Formats a log [`Record`] to be outputted
204+
///
205+
/// This is called on each record logged and should format the
206+
/// log record and output it to the given [`Formatter`].
207+
///
208+
/// This is expected to output the content directly to the
209+
/// [`Formatter`] so that implementations can use the [`std::fmt`] macros
210+
/// to format and output without intermediate heap allocations.
211+
///
212+
/// When the `color` feature is enabled, styling via ANSI escape codes is supported and the
213+
/// output will automatically respect [`Builder::write_style`].
214+
pub trait RecordFormat {
204215
fn format(&self, formatter: &mut Formatter, record: &Record<'_>) -> io::Result<()>;
205216
}
206217

src/logger.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use std::{borrow::Cow, cell::RefCell, env, io};
1+
#[cfg(feature = "kv")]
2+
use std::io;
3+
use std::{borrow::Cow, cell::RefCell, env};
24

35
use log::{LevelFilter, Log, Metadata, Record, SetLoggerError};
46

@@ -242,7 +244,7 @@ impl Builder {
242244
/// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html
243245
pub fn format<F>(&mut self, format: F) -> &mut Self
244246
where
245-
F: Fn(&mut Formatter, &Record<'_>) -> io::Result<()> + Sync + Send + 'static,
247+
F: fmt::RecordFormat + Sync + Send + 'static,
246248
{
247249
self.format.custom_format = Some(Box::new(format));
248250
self

0 commit comments

Comments
 (0)