pub struct HumanLogger { /* private fields */ }
Expand description
Defines a logger that has a pretty, user-friendly mode, and a comprehensive, dev-friendly debug mode.
Implementations§
Source§impl HumanLogger
impl HumanLogger
Sourcepub fn new(
writers: impl IntoIterator<Item = LogWriter>,
debug: DebugMode,
) -> Self
pub fn new( writers: impl IntoIterator<Item = LogWriter>, debug: DebugMode, ) -> Self
Constructor for the HumanLogger that will log to the given set of Write
rs.
Don’t forget to also install the Logger at some point using HumanLogger::init()
.
§Arguments
writers
: A list of writers to write to. You can configure for each of them if they should add ANSI colours to their output or not, and which log levels need to be written to them.debug
: Whether to enable debug mode or not.
§Returns
A new HumanLogger instance that can then be installed in the log
-crate.
§Examples
use humanlog::{DebugMode, HumanLogger, LogWriter};
// Will emulate the default behaviour of writing `Level::Error` and `Level::Warn` to stderr, the rest to stdout.
if let Err(err) = HumanLogger::new(vec![ LogWriter::stdout(), LogWriter::stderr() ], DebugMode::Debug).init() {
eprintln!("WARNING: Failed to initialize logger: {err} (no logging enabled for this session)");
}
Sourcepub fn terminal(mode: DebugMode) -> Self
pub fn terminal(mode: DebugMode) -> Self
Default constructor for the HumanLogger that prepares it for logging to the terminal.
Logs to both stdout and stderr (errors and warnings to the latter, the rest to the first), and uses automatic colour selection.
For more customization options, use HumanLogger::new()
with a list of LogWriter
s.
Don’t forget to also install the Logger at some point using HumanLogger::init()
.
§Arguments
mode
: The mode of debugging to use for this session. Decides both whichLevel
s to apply, and how to format the resulting messages.
§Returns
A new HumanLogger that will log to stdout and stderr.
§Examples
use humanlog::{DebugMode, HumanLogger};
// Will emulate the default behaviour of writing `Level::Error` and `Level::Warn` to stderr, the rest to stdout.
if let Err(err) = HumanLogger::terminal(DebugMode::Debug).init() {
eprintln!("WARNING: Failed to initialize logger: {err} (no logging enabled for this session)");
}
Sourcepub fn init(self) -> Result<(), SetLoggerError>
pub fn init(self) -> Result<(), SetLoggerError>
Initializes this logger as the log
-crate’s logger.
§Errors
Tihs function may error if we failed to setup the logger. This can happen if there already was one or any other reason that log
crashes.
§Examples
use humanlog::{DebugMode, HumanLogger};
// Let's create a logger
let logger: HumanLogger = HumanLogger::terminal(DebugMode::HumanFriendly);
// Enable it
if let Err(err) = logger.init() {
eprintln!("WARNING: Failed to initialize logger: {err} (no logging enabled for this session)");
}