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

Make loggers::logging::try_log public #143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub use self::loggers::{TermLogger, TerminalMode};
pub use termcolor::{Color, ColorChoice};

pub use log::{Level, LevelFilter};
pub use loggers::logging::try_log;

use log::Log;
#[cfg(test)]
Expand Down
36 changes: 36 additions & 0 deletions src/loggers/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,42 @@ pub fn termcolor_to_ansiterm(color: &Color) -> Option<ansi_term::Color> {
}
}

/// Allows for custom loggers to be created, making formatting easier.
///
/// Takes a `Config` and a `Record` and writes the log to the specified `Write`r.
/// Fails if the `Write`r fails to write.
///
/// # Example
/// ```rs
/// impl log::Log for ... {
/// fn enabled(&self, metadata: &log::Metadata) -> bool {
/// metadata.level() <= self.level
/// }
/// fn log(&self, record: &log::Record) {
/// if !self.enabled(record.metadata()) {
/// return;
/// }
///
/// let mut s = Vec::new();
/// let _ = simplelog::try_log(&self.config, record, &mut std::io::Cursor::new(&mut s));
/// let s = std::str::from_utf8(&s).expect("utf8").trim();
///
/// ...
/// }
/// fn flush(&self) {}
/// }
/// impl simplelog::SharedLogger for ... {
/// fn level(&self) -> simplelog::LevelFilter {
/// self.level
/// }
/// fn config(&self) -> Option<&simplelog::Config> {
/// Some(&self.config)
/// }
/// fn as_log(self: Box<Self>) -> Box<dyn log::Log> {
/// Box::new(*self)
/// }
/// }
/// ```
#[inline(always)]
pub fn try_log<W>(config: &Config, record: &Record<'_>, write: &mut W) -> Result<(), Error>
where
Expand Down