Skip to content

Commit f2abc8d

Browse files
authored
core: add as_str() for Level (#1413)
## 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
1 parent 1f8a895 commit f2abc8d

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

tracing-core/src/metadata.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,19 @@ impl Level {
283283
///
284284
/// Designates very low priority, often extremely verbose, information.
285285
pub const TRACE: Level = Level(LevelInner::Trace);
286+
287+
/// Returns the string representation of the `Level`.
288+
///
289+
/// This returns the same string as the `fmt::Display` implementation.
290+
pub fn as_str(&self) -> &'static str {
291+
match *self {
292+
Level::TRACE => "TRACE",
293+
Level::DEBUG => "DEBUG",
294+
Level::INFO => "INFO",
295+
Level::WARN => "WARN",
296+
Level::ERROR => "ERROR",
297+
}
298+
}
286299
}
287300

288301
impl fmt::Display for Level {

0 commit comments

Comments
 (0)