Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@nebkor
Copy link
Contributor

@nebkor nebkor commented Jul 31, 2025

This is an alternative to exposing usize constructors for these enums, that still allows fluent manipulation of the level, a la,

// increase logging verbosity
std::log::set_max_level(std::log::max_level().up());

src/lib.rs Outdated
Comment on lines 592 to 594
/// assert_eq!(Level::Debug, level.up());
/// assert_eq!(Level::Trace, level.up().up());
/// assert_eq!(Level::Trace, level.up().up().up()); // max level
Copy link

@tgross35 tgross35 Aug 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe these should be fn add(&self, n: u8) -> Self / subtract? To allow jumping multiple levels without repeated calls.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'd be fine with me, but I've gotten the impression that exposing any kind of numerically-oriented API is unwanted, aside from being able to cast as <integer>. I just hate having to do an unsafe transmute if I want to change the level.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point. For what it's worth, something like this would be much better than actually transmute:

fn log_level(a: u32) -> Level {
    match a {
        1 => Level::Error,
        2 => Level::Warn,
        3 => Level::Info,
        4 => Level::Debug,
        5 => Level::Trace,
        _ => panic!("unknown log level"),
    }
}

...but I get the reasoning to want something builtin.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funny enough, that's basically what I had in my own code before just going, fuck it, I'll transmute :)

But that's so much boilerplate for what should be something trivial!

@KodrAus
Copy link
Contributor

KodrAus commented Aug 31, 2025

Thanks for the PR @nebkor! Functionality wise this looks good to me. Naming wise, I think we should aim for something a bit more descriptive than up and down, since it's not obvious whether Info.up() should be Debug or Warn. What do you think of increment_severity() and decrement_severity(). Definitely more verbose than up or down, but they're easier to reason about.

/// assert_eq!(Level::Trace, level.increment_severity().increment_severity());
/// assert_eq!(Level::Trace, level.increment_severity().increment_severity().increment_severity()); // max level
/// ```
pub fn increment_severity(&self) -> Self {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KodrAus love the suggestion!

Copy link
Contributor

@KodrAus KodrAus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @nebkor!

This looks good to me.

@KodrAus
Copy link
Contributor

KodrAus commented Sep 1, 2025

The examples point this out already, but just leaving a note here for anyone who finds their way back to this PR; the semantics of increment and decrement here are the same as syslog's. To increment the severity means to reduce it. To decrement the severity means to increase it. I think this is still the correct behavior because it means we don't reverse the order of iteration, or comparison, when incrementing and decrementing.

@KodrAus KodrAus merged commit 60829b1 into rust-lang:master Sep 1, 2025
13 checks passed
@tisonkun
Copy link
Contributor

tisonkun commented Sep 3, 2025

the same as syslog's

@KodrAus could you provide a ref here? I don't find how syslog defines this rule.

Also, perhaps we can change the PR title to align with the final accepted method names.

@nebkor nebkor changed the title Add up() and down() methods for Level and LevelFilter Add increment_severity() and decrement_severity() methods for Level and LevelFilter Sep 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants