torrust_index/config/v2/
logging.rs

1use std::fmt;
2
3use serde::{Deserialize, Serialize};
4use tracing::level_filters::LevelFilter;
5
6/// Core configuration for the API
7#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
8pub struct Logging {
9    /// Logging level. Possible values are: `Off`, `Error`, `Warn`, `Info`, `Debug`, `Trace`.
10    #[serde(default = "Logging::default_threshold")]
11    pub threshold: Threshold,
12}
13
14impl Default for Logging {
15    fn default() -> Self {
16        Self {
17            threshold: Logging::default_threshold(),
18        }
19    }
20}
21
22impl Logging {
23    fn default_threshold() -> Threshold {
24        Threshold::Info
25    }
26}
27
28#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Clone)]
29#[serde(rename_all = "lowercase")]
30pub enum Threshold {
31    /// A level lower than all log security levels.
32    Off,
33    /// Corresponds to the `Error` log security level.
34    Error,
35    /// Corresponds to the `Warn` log security level.
36    Warn,
37    /// Corresponds to the `Info` log security level.
38    Info,
39    /// Corresponds to the `Debug` log security level.
40    Debug,
41    /// Corresponds to the `Trace` log security level.
42    Trace,
43}
44
45impl Default for Threshold {
46    fn default() -> Self {
47        Self::Info
48    }
49}
50
51impl fmt::Display for Threshold {
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        let display_str = match self {
54            Threshold::Off => "off",
55            Threshold::Error => "error",
56            Threshold::Warn => "warn",
57            Threshold::Info => "info",
58            Threshold::Debug => "debug",
59            Threshold::Trace => "trace",
60        };
61        write!(f, "{display_str}")
62    }
63}
64
65impl From<Threshold> for LevelFilter {
66    fn from(threshold: Threshold) -> Self {
67        match threshold {
68            Threshold::Off => LevelFilter::OFF,
69            Threshold::Error => LevelFilter::ERROR,
70            Threshold::Warn => LevelFilter::WARN,
71            Threshold::Info => LevelFilter::INFO,
72            Threshold::Debug => LevelFilter::DEBUG,
73            Threshold::Trace => LevelFilter::TRACE,
74        }
75    }
76}