Skip to content

Commit

Permalink
core: add as_str() for Level (#1413)
Browse files Browse the repository at this point in the history
## Motivation

Get the string representation of the `Level` is quite a common usecase.
Without this method, I normally need to implement it by myself, which
is fairly noisy.

```rust
#[inline]
fn level_to_str(level: &tracing::Level) -> &'static str {
    match *level {
        tracing::Level::TRACE => "TRACE",
        tracing::Level::DEBUG => "DEBUG",
        tracing::Level::INFO => "INFO",
        tracing::Level::WARN => "WARN",
        tracing::Level::ERROR => "ERROR"
    }
}
```

## Solution

Add an `as_str()` method for `Level`. Similar to [log::Level::as_str()][1].

[1] https://docs.rs/log/0.4.14/log/enum.Level.html#method.as_str
  • Loading branch information
Folyd authored May 26, 2021
1 parent 1f8a895 commit f2abc8d
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tracing-core/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,19 @@ impl Level {
///
/// Designates very low priority, often extremely verbose, information.
pub const TRACE: Level = Level(LevelInner::Trace);

/// Returns the string representation of the `Level`.
///
/// This returns the same string as the `fmt::Display` implementation.
pub fn as_str(&self) -> &'static str {
match *self {
Level::TRACE => "TRACE",
Level::DEBUG => "DEBUG",
Level::INFO => "INFO",
Level::WARN => "WARN",
Level::ERROR => "ERROR",
}
}
}

impl fmt::Display for Level {
Expand Down

0 comments on commit f2abc8d

Please sign in to comment.