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
impl DebugMode
Sourcepub fn from_flags(trace: bool, debug: bool) -> Self
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 returnDebugMode::Full
.debug
: Iftrace
is false anddebug
is true, will returnDebugMode::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...");
}
Sourcepub fn from_num(num: u32) -> Self
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
, thenDebugMode::HumanFriendly
is returned. - if
num == 1
, thenDebugMode::Debug
is returned. - if
num >= 2
, thenDebugMode::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...");
}