Skip to content
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

Expose method to build logger without registering in log crate #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
feat: expose build to build logger without registering in log crate (f…
…ixes #2)
  • Loading branch information
alekitto committed Sep 11, 2024
commit 0d0dccd2bdd758e047143a679c66d0811f856bce
31 changes: 21 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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.
@@ -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));