Skip to content

Commit

Permalink
feat: expose build to build logger without registering in log crate (f…
Browse files Browse the repository at this point in the history
…ixes iorust#2)
  • Loading branch information
alekitto committed Sep 11, 2024
1 parent 995f1ee commit 0d0dccd
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,23 @@ impl Builder {
cfg
}

/// Builds the logger without registering it in the [`log`] crate.
///
/// Unlike [`Builder::init`] and [`Builder::try_init`] this does not register
/// the logger into the [`log`] system, allowing it to be combined with
/// other logging crates.
pub fn build(self) -> impl log::Log {
Logger {
filter: self.filter,
default_writer: self.default_writer,
writers: self
.writers
.into_iter()
.map(|(t, w)| (InnerTarget::from(t), w))
.collect(),
}
}

/// Initialize the logger for [`log`] crate.
///
/// See the [crate level documentation] for more.
Expand All @@ -198,17 +215,11 @@ impl Builder {
/// [`init`]: fn.init.html
/// [crate level documentation]: index.html
pub fn try_init(self) -> Result<(), SetLoggerError> {
let logger = Box::new(Logger {
filter: self.filter,
default_writer: self.default_writer,
writers: self
.writers
.into_iter()
.map(|(t, w)| (InnerTarget::from(t), w))
.collect(),
});
let filter = self.filter;
let logger = Box::new(self.build());

log::set_boxed_logger(logger)?;
log::set_max_level(self.filter);
log::set_max_level(filter);

#[cfg(feature = "log-panic")]
std::panic::set_hook(Box::new(log_panic));
Expand Down

0 comments on commit 0d0dccd

Please sign in to comment.