humanlog

Enum DebugMode

Source
pub enum DebugMode {
    HumanFriendly,
    Debug,
    Full,
}
Expand description

Defines the mode to print the log messages in the HumanLogger.

Note that it applies both a change in what is logged, as well as how it is logged (i.e., the formatting changes too).

Variants§

§

HumanFriendly

No debugging, only warnings (Level::Warn) and errors (Level::Error).

§Examples

use humanlog::{DebugMode, HumanLogger};
use log::{debug, error, info, trace, warn};
 
// Setup the logger to write to the terminal with default settings and the prettiest (and least informative) debug mode
if let Err(err) = HumanLogger::terminal(DebugMode::HumanFriendly).init() {
    eprintln!("WARNING: Failed to initialize logger: {err} (no logging enabled for this session)");
}
 
error!("This is an error!");
warn!("This is a warning!");
info!("This is an info message!");
debug!("This is a debug message!");
trace!("This is a trace message!");

This will show:

ERROR: This is an error!
WARNING: This is a warning!
§

Debug

Debugs Level::Info and Level::Debug in addition to those of DebugMode::HumanFriendly.

§Examples

use humanlog::{DebugMode, HumanLogger};
use log::{debug, error, info, trace, warn};
 
// Setup the logger to write to the terminal with a server-level verbosity and formatting
if let Err(err) = HumanLogger::terminal(DebugMode::Debug).init() {
    eprintln!("WARNING: Failed to initialize logger: {err} (no logging enabled for this session)");
}
 
error!("This is an error!");
warn!("This is a warning!");
info!("This is an info message!");
debug!("This is a debug message!");
trace!("This is a trace message!");

This will show:

[2023-03-03T18:10:13Z ERROR debug] This is an error!
[2023-03-03T18:10:13Z WARNING debug] This is a warning!
[2023-03-03T18:10:13Z INFO debug] This is an info message!
[2023-03-03T18:10:13Z DEBUG debug] This is a debug message!
§

Full

Debugs everything, which is everything DebugLevel::Debug does, plus Level::Trace.

§Examples

use humanlog::{DebugMode, HumanLogger};
use log::{debug, error, info, trace, warn};
 
// Setup the logger to write to the terminal with the most verbose and extensive mode available.
if let Err(err) = HumanLogger::terminal(DebugMode::Full).init() {
    eprintln!("WARNING: Failed to initialize logger: {err} (no logging enabled for this session)");
}
 
error!("This is an error!");
warn!("This is a warning!");
info!("This is an info message!");
debug!("This is a debug message!");
trace!("This is a trace message!");

This will show:

[2023-03-03T18:11:37.853292702+01:00 ERROR examples/full.rs:27 full] This is an error!
[2023-03-03T18:11:37.853450438+01:00 WARNING examples/full.rs:28 full] This is a warning!
[2023-03-03T18:11:37.853482929+01:00 INFO examples/full.rs:29 full] This is an info message!
[2023-03-03T18:11:37.853495693+01:00 DEBUG examples/full.rs:30 full] This is a debug message!
[2023-03-03T18:11:37.853507184+01:00 TRACE examples/full.rs:31 full] This is a trace message!

Implementations§

Source§

impl DebugMode

Source

pub fn from_flags(trace: bool, debug: bool) -> Self

Converts two flags (i.e., boolean values) to a suitable DebugMode.

Assumes that trace outplays debug, i.e., if trace is true, then debug is ignored.

§Arguments
  • trace: If true, will return DebugMode::Full.
  • debug: If trace is false and debug is true, will return DebugMode::Debug.
§Returns

A new DebugMode that matches the given boolean values, or else DebugMode::HumanFriendly.

§Examples
// We use [clap](https://docs.rs/clap/latest/clap/) to parse command-line arguments
// Enable the `derive` feature
use clap::Parser;
use humanlog::{DebugMode, HumanLogger};
use log::{debug, info};
 
/// Defines the command-line arguments for this executable.
#[derive(Parser)]
struct Arguments {
    /// Will change to `DebugMode::Debug`
    #[clap(long, global=true)]
    debug: bool,
    /// Will change to `DebugMode::Full`
    #[clap(long, global=true)]
    trace: bool,
}
 
fn main() {
    // Parse the arguments
    let args = Arguments::parse();
 
    // Enable the correct debugging mode based on the values
    if let Err(err) = HumanLogger::terminal(DebugMode::from_flags(args.debug, args.trace)).init() {
        eprintln!("WARNING: Failed to setup logger: {err} (no logging enabled for this session)");
    }
    info!("Successfully setup HumanLogger!");
    debug!("Time to crime...");
}
Source

pub fn from_num(num: u32) -> Self

Converts a numerical value to a suitable DebugMode.

§Arguments
  • num: The numerical value to parse from.
§Returns

A new DebugMode matching the value. Specifically:

  • if num == 0, then DebugMode::HumanFriendly is returned.
  • if num == 1, then DebugMode::Debug is returned.
  • if num >= 2, then DebugMode::Full is returned.
§Examples
// We use [clap](https://docs.rs/clap/latest/clap/) to parse command-line arguments
// Enable the `derive` feature
use clap::Parser;
use humanlog::{DebugMode, HumanLogger};
use log::{debug, info};
 
/// Defines the command-line arguments for this executable.
#[derive(Parser)]
struct Arguments {
    /// Defines the level to parse.
    #[clap(short, long, default_value="0")]
    verbosity : u32,
}
 
fn main() {
    // Parse the arguments
    let args = Arguments::parse();
 
    // Enable the correct debugging mode based on the values
    if let Err(err) = HumanLogger::terminal(DebugMode::from_num(args.verbosity)).init() {
        eprintln!("WARNING: Failed to setup logger: {err} (no logging enabled for this session)");
    }
    info!("Successfully setup HumanLogger!");
    debug!("Time to crime...");
}

Trait Implementations§

Source§

impl Clone for DebugMode

Source§

fn clone(&self) -> DebugMode

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DebugMode

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Hash for DebugMode

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for DebugMode

Source§

fn eq(&self, other: &DebugMode) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for DebugMode

Source§

impl Eq for DebugMode

Source§

impl StructuralPartialEq for DebugMode

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.