Skip to content

Commit

Permalink
Add {File,Terminal}LoggerDecorator
Browse files Browse the repository at this point in the history
  • Loading branch information
sile committed Jan 27, 2024
1 parent b9af710 commit b96e68a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ pub trait Build {
fn build(&self) -> Result<Logger>;
}

/// This trait allows to build a logger instance with a custom format (i.e., [`Drain`]}.
pub trait BuildWithCustomFormat {
/// [`Decorator`] type generated by the logger builder.
type Decorator: Decorator;

/// Builds a logger with a custom format.
fn build_with_custom_format<F, D>(&self, f: F) -> Result<Logger>
where
F: FnOnce(Self::Decorator) -> Result<D>,
Expand Down
23 changes: 20 additions & 3 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,22 +179,39 @@ impl Build for FileLoggerBuilder {
}
}
impl BuildWithCustomFormat for FileLoggerBuilder {
type Decorator = PlainDecorator<FileAppender>;
type Decorator = FileLoggerDecorator;

fn build_with_custom_format<F, D>(&self, f: F) -> Result<Logger>
where
F: FnOnce(Self::Decorator) -> Result<D>,
D: Drain + Send + 'static,
D::Err: Debug,
{
let decorator = PlainDecorator::new(self.appender.clone());
let decorator = FileLoggerDecorator(PlainDecorator::new(self.appender.clone()));
let drain = track!(f(decorator))?;
Ok(self.common.build_with_drain(drain))
}
}

/// [`slog_term::Decorator`] implementation for [`FileLoggerBuilder`].
pub struct FileLoggerDecorator(PlainDecorator<FileAppender>);

impl slog_term::Decorator for FileLoggerDecorator {
fn with_record<F>(
&self,
record: &slog::Record,
logger_values: &slog::OwnedKVList,
f: F,
) -> io::Result<()>
where
F: FnOnce(&mut dyn slog_term::RecordDecorator) -> io::Result<()>,
{
self.0.with_record(record, logger_values, f)
}
}

#[derive(Debug)]
pub struct FileAppender {
struct FileAppender {
path: PathBuf,
file: Option<BufWriter<File>>,
truncate: bool,
Expand Down
23 changes: 20 additions & 3 deletions src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,37 @@ impl Build for TerminalLoggerBuilder {
}
}
impl BuildWithCustomFormat for TerminalLoggerBuilder {
type Decorator = Decorator;
type Decorator = TerminalLoggerDecorator;

fn build_with_custom_format<F, D>(&self, f: F) -> Result<Logger>
where
F: FnOnce(Self::Decorator) -> Result<D>,
D: Drain + Send + 'static,
D::Err: Debug,
{
let decorator = self.destination.to_decorator();
let decorator = TerminalLoggerDecorator(self.destination.to_decorator());
let drain = track!(f(decorator))?;
Ok(self.common.build_with_drain(drain))
}
}

/// [`slog_term::Decorator`] implementation for [`TerminalLoggerBuilder`].
pub struct TerminalLoggerDecorator(Decorator);

impl slog_term::Decorator for TerminalLoggerDecorator {
fn with_record<F>(
&self,
record: &slog::Record,
logger_values: &slog::OwnedKVList,
f: F,
) -> io::Result<()>
where
F: FnOnce(&mut dyn slog_term::RecordDecorator) -> io::Result<()>,
{
self.0.with_record(record, logger_values, f)
}
}

/// The destination to which log records will be outputted.
///
/// # Examples
Expand Down Expand Up @@ -171,7 +188,7 @@ impl Destination {
}
}

pub enum Decorator {
enum Decorator {
Term(TermDecorator),
PlainStdout(PlainDecorator<io::Stdout>),
PlainStderr(PlainDecorator<io::Stderr>),
Expand Down

0 comments on commit b96e68a

Please sign in to comment.