strut_tracing/config/verbosity.rs
1use strut_factory::Deserialize as StrutDeserialize;
2use tracing_core::LevelFilter as TracingLevelFilter;
3
4/// A thin abstraction around the `tracing` crate’s
5/// [`LevelFilter`](TracingLevelFilter), introduced to provide deserialization.
6///
7/// A verbosity level is “higher” if it is more verbose. In this sense,
8/// [`Trace`](Verbosity::Trace) is higher (more verbose) than
9/// [`Error`](Verbosity::Error).
10///
11/// Conversely, a verbosity level is “lower” if it is less verbose. In this
12/// sense, [`Warn`](Verbosity::Warn) is lower than [`Info`](Verbosity::Info).
13#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, StrutDeserialize)]
14#[strut(eq_fn = strut_deserialize::Slug::eq_as_slugs)]
15pub enum Verbosity {
16 /// Log **nothing**.
17 #[strut(alias = "no")]
18 Off,
19
20 /// Log at level [`ERROR`](tracing_core::metadata::Level::ERROR) only.
21 #[strut(alias = "err")]
22 Error,
23
24 /// Log at level [`WARN`](tracing_core::metadata::Level::WARN) and lower.
25 #[strut(alias = "warning")]
26 Warn,
27
28 /// Log at level [`INFO`](tracing_core::metadata::Level::INFO) and lower.
29 Info,
30
31 /// Log at level [`DEBUG`](tracing_core::metadata::Level::DEBUG) and lower.
32 Debug,
33
34 /// Log **everything**.
35 Trace,
36}
37
38impl Default for Verbosity {
39 /// Defines a reasonable default [`Verbosity`].
40 fn default() -> Self {
41 Self::Info
42 }
43}
44
45impl Verbosity {
46 /// Translates this [`Verbosity`] level to the `tracing` crate’s
47 /// [`LevelFilter`](TracingLevel).
48 pub fn to_tracing_level_filter(&self) -> TracingLevelFilter {
49 match self {
50 Self::Off => TracingLevelFilter::OFF,
51 Self::Error => TracingLevelFilter::ERROR,
52 Self::Warn => TracingLevelFilter::WARN,
53 Self::Info => TracingLevelFilter::INFO,
54 Self::Debug => TracingLevelFilter::DEBUG,
55 Self::Trace => TracingLevelFilter::TRACE,
56 }
57 }
58}
59
60impl From<Verbosity> for TracingLevelFilter {
61 fn from(value: Verbosity) -> Self {
62 value.to_tracing_level_filter()
63 }
64}
65
66impl From<&Verbosity> for TracingLevelFilter {
67 fn from(value: &Verbosity) -> Self {
68 value.to_tracing_level_filter()
69 }
70}