humanlog

Struct LogWriter

Source
pub struct LogWriter { /* private fields */ }
Expand description

Wrapper around a Write-capable type that filters the types of messages that are written to it.

This can be used to customize the output source of the HumanLogger.

§Examples

A logger that writes to stdout only, instead of mixed stdout/stderr (like usual).

use humanlog::{ColourChoice, DebugMode, HumanLogger, LogWriter};
use log::{debug, info, error, Level};
 
let logger: LogWriter = LogWriter::new(std::io::stdout(), ColourChoice::Auto, vec![ Level::Error, Level::Warn, Level::Info, Level::Debug, Level::Trace ], "stdout");
if let Err(err) = HumanLogger::new(vec![ logger ], DebugMode::Debug).init() {
    eprintln!("WARNING: Failed to initialize logger: {err} (no logging enabled for this session)");
}

A logger that writes to a file instead of stdout/stderr.

use std::fs::File;
use humanlog::{ColourChoice, DebugMode, HumanLogger, LogWriter};
use log::{debug, info, error, Level};
 
// Open a file
match File::create("output.log") {
    Ok(handle) => {
        // Use that to create a writer that receives everything
        let logger: LogWriter = LogWriter::new(handle, ColourChoice::No, vec![ Level::Error, Level::Warn, Level::Info, Level::Debug, Level::Trace ], "file");
        if let Err(err) = HumanLogger::new(vec![ logger ], DebugMode::Debug).init() {
            eprintln!("WARNING: Failed to initialize logger: {err} (no logging enabled for this session)");
        }
    },

    Err(err) => {
        eprintln!("WARNING: Failed to initialize logger: Failed to create file 'output.log': {err} (no logging enabled for this session)");
    },
}

A logger that writes to a file in addition to the standard stdout/stderr logging.

use std::fs::File;
use humanlog::{ColourChoice, DebugMode, HumanLogger, LogWriter};
use log::{debug, info, error, Level};
 
// Open a file
match File::create("output.log") {
    Ok(handle) => {
        // Create three LogWriters: one per output stream
        let stdout_logger: LogWriter = LogWriter::stdout();
        let stderr_logger: LogWriter = LogWriter::stderr();
        // Note the repeated levels; the logger will simply log to all LogWriters that want that particular level
        let file_logger: LogWriter = LogWriter::new(handle, ColourChoice::No, vec![ Level::Error, Level::Warn, Level::Info, Level::Debug, Level::Trace ], "file");
 
        // Finally, we put it all in one logger
        if let Err(err) = HumanLogger::new(vec![ stdout_logger, stderr_logger, file_logger ], DebugMode::Debug).init() {
            eprintln!("WARNING: Failed to initialize logger: {err} (no logging enabled for this session)");
        }
    },
 
    Err(err) => {
        eprintln!("WARNING: Failed to initialize logger: Failed to create file 'output.log': {err} (no logging enabled for this session)");
    },
}

Implementations§

Source§

impl LogWriter

Source

pub fn stdout() -> Self

Default constructor for the LogWriter that initializes it for stdout.

By default, will use automatic colour selection and only logs trace, debug and info-messages.

§Returns

A new LogWriter instance that can be used to log to stdout.

§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 stderr() -> Self

Default constructor for the LogWriter that initializes it for stderr.

By default, will use automatic colour selection and only logs warning and error message.

§Returns

A new LogWriter instance that can be used to log to stderr.

§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 new( writer: impl 'static + Send + Sync + Write, colour: ColourChoice, filter: impl Into<Vec<Level>>, label: impl Into<String>, ) -> Self

Constructor for the LogWriter that wraps it around the given Writer.

§Arguments
  • writer: The handle or other object that implement Write and we will write to.
  • colour_choice: Whether to enable ANSI colours for this file or not.
  • filter: The list of Levels that are only allowed to be written to this writer.
  • label: Some description of the writer for debugging purposes.
§Returns

A new LogWriter instance that wraps around the given writer.

§Examples
use humanlog::{ColourChoice, DebugMode, HumanLogger, LogWriter};
use log::Level;
 
// Will only ever write to stdout, regardless of the log type
let logger: LogWriter = LogWriter::new(std::io::stdout(), ColourChoice::Auto, vec![ Level::Error, Level::Warn, Level::Info, Level::Debug, Level::Trace ], "stdout");
if let Err(err) = HumanLogger::new(vec![ logger ], DebugMode::Debug).init() {
    eprintln!("WARNING: Failed to initialize logger: {err} (no logging enabled for this session)");
}

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.