humanlog

Struct HumanLogger

Source
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

Source

pub fn new( writers: impl IntoIterator<Item = LogWriter>, debug: DebugMode, ) -> Self

Constructor for the HumanLogger that will log to the given set of Writers.

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)");
}
Source

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 LogWriters.

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 which Levels 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)");
}
Source

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)");
}

Trait Implementations§

Source§

impl Log for HumanLogger

Source§

fn enabled(&self, metadata: &Metadata<'_>) -> bool

Determines if a log message with the specified metadata would be logged. Read more
Source§

fn log(&self, record: &Record<'_>)

Logs the Record. Read more
Source§

fn flush(&self)

Flushes any buffered records. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.