Skip to content

Commit

Permalink
feat: Add additional log levels
Browse files Browse the repository at this point in the history
This adds `OffLevel`, `DebugLevel`, and `TraceLevel` to the list of log
levels that can be set as the default log level for the logger.

Fixes: #122
  • Loading branch information
joshka committed Nov 16, 2024
1 parent a7e305e commit 6e5fc6a
Showing 1 changed file with 129 additions and 1 deletion.
130 changes: 129 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
//! - `-vvv` show debug
//! - `-vvvv` show trace
//!
//! You can also customize the default logging level:
//! By default, the log level is set to Error. To customize this to a different level, pass a type
//! implementing the [`LogLevel`] trait to [`Verbosity`]:
//!
//! ```rust,no_run
//! # use clap::Parser;
//! use clap_verbosity_flag::{Verbosity, InfoLevel};
Expand Down Expand Up @@ -222,6 +224,36 @@ impl LogLevel for InfoLevel {
}
}

/// Default to [`log::Level::Debug`]
#[derive(Copy, Clone, Debug, Default)]
pub struct DebugLevel;

impl LogLevel for DebugLevel {
fn default() -> Option<Level> {
Some(Level::Debug)
}
}

/// Default to [`log::Level::Trace`]
#[derive(Copy, Clone, Debug, Default)]
pub struct TraceLevel;

impl LogLevel for TraceLevel {
fn default() -> Option<Level> {
Some(Level::Trace)
}
}

/// Default to no logging (i.e. `None` or [`log::LevelFilter::Off`])
#[derive(Copy, Clone, Debug, Default)]
pub struct OffLevel;

impl LogLevel for OffLevel {
fn default() -> Option<Level> {
None
}
}

#[cfg(test)]
mod test {
use super::*;
Expand All @@ -238,6 +270,38 @@ mod test {
Cli::command().debug_assert();
}

#[test]
fn verbosity_off_level() {
let tests = [
// verbose, quiet, expected_level, expected_filter
(0, 0, None, LevelFilter::Off),
(1, 0, Some(Level::Error), LevelFilter::Error),
(2, 0, Some(Level::Warn), LevelFilter::Warn),
(3, 0, Some(Level::Info), LevelFilter::Info),
(4, 0, Some(Level::Debug), LevelFilter::Debug),
(5, 0, Some(Level::Trace), LevelFilter::Trace),
(6, 0, Some(Level::Trace), LevelFilter::Trace),
(255, 0, Some(Level::Trace), LevelFilter::Trace),
(0, 1, None, LevelFilter::Off),
(0, 255, None, LevelFilter::Off),
(255, 255, None, LevelFilter::Off),
];

for (verbose, quiet, expected_level, expected_filter) in tests.iter() {
let v = Verbosity::<OffLevel>::new(*verbose, *quiet);
assert_eq!(
v.log_level(),
*expected_level,
"verbose = {verbose}, quiet = {quiet}"
);
assert_eq!(
v.log_level_filter(),
*expected_filter,
"verbose = {verbose}, quiet = {quiet}"
);
}
}

#[test]
fn verbosity_error_level() {
let tests = [
Expand Down Expand Up @@ -333,4 +397,68 @@ mod test {
);
}
}

#[test]
fn verbosity_debug_level() {
let tests = [
// verbose, quiet, expected_level, expected_filter
(0, 0, Some(Level::Debug), LevelFilter::Debug),
(1, 0, Some(Level::Trace), LevelFilter::Trace),
(2, 0, Some(Level::Trace), LevelFilter::Trace),
(255, 0, Some(Level::Trace), LevelFilter::Trace),
(0, 1, Some(Level::Info), LevelFilter::Info),
(0, 2, Some(Level::Warn), LevelFilter::Warn),
(0, 3, Some(Level::Error), LevelFilter::Error),
(0, 4, None, LevelFilter::Off),
(0, 5, None, LevelFilter::Off),
(0, 255, None, LevelFilter::Off),
(255, 255, Some(Level::Debug), LevelFilter::Debug),
];

for (verbose, quiet, expected_level, expected_filter) in tests.iter() {
let v = Verbosity::<DebugLevel>::new(*verbose, *quiet);
assert_eq!(
v.log_level(),
*expected_level,
"verbose = {verbose}, quiet = {quiet}"
);
assert_eq!(
v.log_level_filter(),
*expected_filter,
"verbose = {verbose}, quiet = {quiet}"
);
}
}

#[test]
fn verbosity_trace_level() {
let tests = [
// verbose, quiet, expected_level, expected_filter
(0, 0, Some(Level::Trace), LevelFilter::Trace),
(1, 0, Some(Level::Trace), LevelFilter::Trace),
(255, 0, Some(Level::Trace), LevelFilter::Trace),
(0, 1, Some(Level::Debug), LevelFilter::Debug),
(0, 2, Some(Level::Info), LevelFilter::Info),
(0, 3, Some(Level::Warn), LevelFilter::Warn),
(0, 4, Some(Level::Error), LevelFilter::Error),
(0, 5, None, LevelFilter::Off),
(0, 6, None, LevelFilter::Off),
(0, 255, None, LevelFilter::Off),
(255, 255, Some(Level::Trace), LevelFilter::Trace),
];

for (verbose, quiet, expected_level, expected_filter) in tests.iter() {
let v = Verbosity::<TraceLevel>::new(*verbose, *quiet);
assert_eq!(
v.log_level(),
*expected_level,
"verbose = {verbose}, quiet = {quiet}"
);
assert_eq!(
v.log_level_filter(),
*expected_filter,
"verbose = {verbose}, quiet = {quiet}"
);
}
}
}

0 comments on commit 6e5fc6a

Please sign in to comment.