Skip to content

feat(fmt)!: Generalize custom formatter to 'RecordFormat' #361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,18 @@ impl fmt::Debug for Formatter {
}
}

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

Expand Down
6 changes: 4 additions & 2 deletions src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::{borrow::Cow, cell::RefCell, env, io};
#[cfg(feature = "kv")]
use std::io;
use std::{borrow::Cow, cell::RefCell, env};

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

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