Skip to main content

lognorm/
severity.rs

1/// Common severity bands, ordered from least to most severe.
2#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
3pub enum Severity {
4    /// Detailed diagnostic information.
5    Trace,
6    /// Debugging information.
7    Debug,
8    /// Normal operational information.
9    Info,
10    /// A condition that may need attention.
11    Warn,
12    /// An operation failed.
13    Error,
14    /// A fatal condition.
15    Fatal,
16}
17
18impl Severity {
19    /// Canonical uppercase spelling of the severity band.
20    pub const fn as_str(self) -> &'static str {
21        match self {
22            Self::Trace => "TRACE",
23            Self::Debug => "DEBUG",
24            Self::Info => "INFO",
25            Self::Warn => "WARN",
26            Self::Error => "ERROR",
27            Self::Fatal => "FATAL",
28        }
29    }
30}
31
32pub(crate) fn name(value: &str) -> Option<Severity> {
33    let value = value.trim();
34    [
35        (Severity::Trace, &["trace", "trc"][..]),
36        (Severity::Debug, &["debug", "dbg"]),
37        (
38            Severity::Info,
39            &["info", "information", "informational", "notice"],
40        ),
41        (Severity::Warn, &["warn", "warning", "wrn"]),
42        (Severity::Error, &["error", "err", "exception"]),
43        (
44            Severity::Fatal,
45            &["fatal", "critical", "crit", "panic", "emergency"],
46        ),
47    ]
48    .into_iter()
49    .find_map(|(severity, aliases)| {
50        aliases
51            .iter()
52            .any(|alias| value.eq_ignore_ascii_case(alias))
53            .then_some(severity)
54    })
55}
56
57pub(crate) fn pino(value: i64) -> Option<Severity> {
58    match value {
59        10 => Some(Severity::Trace),
60        20 => Some(Severity::Debug),
61        30 => Some(Severity::Info),
62        40 => Some(Severity::Warn),
63        50 => Some(Severity::Error),
64        60 => Some(Severity::Fatal),
65        _ => None,
66    }
67}
68
69pub(crate) fn python(value: i64) -> Option<Severity> {
70    match value {
71        10 => Some(Severity::Debug),
72        20 => Some(Severity::Info),
73        30 => Some(Severity::Warn),
74        40 => Some(Severity::Error),
75        50 => Some(Severity::Fatal),
76        _ => None,
77    }
78}
79
80pub(crate) fn otel(value: i64) -> Option<Severity> {
81    match value {
82        1..=4 => Some(Severity::Trace),
83        5..=8 => Some(Severity::Debug),
84        9..=12 => Some(Severity::Info),
85        13..=16 => Some(Severity::Warn),
86        17..=20 => Some(Severity::Error),
87        21..=24 => Some(Severity::Fatal),
88        _ => None,
89    }
90}