scsys_config/services/
logger.rs

1/*
2    Appellation: logger <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5#[doc(inline)]
6pub use self::tracing::*;
7
8pub(crate) mod tracing;
9
10use crate::LogLevel;
11
12#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
13#[cfg_attr(
14    feature = "serde",
15    derive(serde::Deserialize, serde::Serialize),
16    serde(default, rename_all = "snake_case")
17)]
18pub struct LoggerConfig {
19    pub level: LogLevel,
20    pub name: String,
21    #[cfg_attr(feature = "serde", serde(flatten))]
22    pub tracing: TracingConfig,
23}
24
25impl LoggerConfig {
26    const LOGGER_PREFIX: &str = "app";
27
28    pub fn new() -> Self {
29        Self::from_name(Self::LOGGER_PREFIX)
30    }
31
32    pub fn from_name<T: ToString>(name: T) -> Self {
33        Self {
34            level: LogLevel::default(),
35            name: name.to_string(),
36            tracing: TracingConfig::new(),
37        }
38    }
39
40    pub fn from_level(level: LogLevel) -> Self {
41        Self {
42            level,
43            name: Self::LOGGER_PREFIX.to_string(),
44            tracing: TracingConfig::new(),
45        }
46    }
47    /// returns a copy of the configured [`level`](LogLevel)
48    pub const fn level(&self) -> LogLevel {
49        self.level
50    }
51    /// returns a mutable reference to the configured [`level`](LogLevel)
52    pub const fn level_mut(&mut self) -> &mut LogLevel {
53        &mut self.level
54    }
55    /// returns a reference to the current logger name
56    pub const fn name(&self) -> &String {
57        &self.name
58    }
59    /// returns a mutable reference to the current logger name
60    pub const fn name_mut(&mut self) -> &mut String {
61        &mut self.name
62    }
63    /// returns a copy of the current [`tracing`](TracingConfig) schema
64    pub const fn tracing(&self) -> TracingConfig {
65        self.tracing
66    }
67    /// returns a mutable reference to the current tracing configuration
68    pub const fn tracing_mut(&mut self) -> &mut TracingConfig {
69        &mut self.tracing
70    }
71    /// update the current logger level and return a mutable reference
72    pub fn set_level(&mut self, level: LogLevel) -> &mut Self {
73        self.level = level;
74        self
75    }
76    /// update the current logger name and return a mutable reference
77    pub fn set_name<T: ToString>(&mut self, name: T) -> &mut Self {
78        self.name = name.to_string();
79        self
80    }
81    /// update the current tracing schema and return a mutable reference
82    pub fn set_tracing(&mut self, tracing: TracingConfig) -> &mut Self {
83        self.tracing = tracing;
84        self
85    }
86    /// consumes the current instance to create another with the given logger level
87    pub fn with_level(self, level: LogLevel) -> Self {
88        Self { level, ..self }
89    }
90    /// consumes the current instance to create another with the given logger name
91    pub fn with_name<T: ToString>(self, name: T) -> Self {
92        Self {
93            name: name.to_string(),
94            ..self
95        }
96    }
97    /// consumes the current instance to create another with the given tracing schema
98    pub fn with_tracing(self, tracing: TracingConfig) -> Self {
99        Self { tracing, ..self }
100    }
101
102    #[cfg(feature = "tracing")]
103    pub fn init_tracing(&self) {
104        self.tracing().init_tracing(self.level, Some(&self.name));
105    }
106}
107
108impl Default for LoggerConfig {
109    fn default() -> Self {
110        Self::new()
111    }
112}
113
114/*
115 ************* Implementations *************
116*/
117impl core::fmt::Display for LoggerConfig {
118    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
119        #[cfg(feature = "json")]
120        {
121            f.write_str(&serde_json::to_string(self).unwrap())
122        }
123        #[cfg(not(feature = "json"))]
124        {
125            write!(f, "{{ level: {}, name: {} }}", self.level, self.name)
126        }
127    }
128}
129
130unsafe impl Send for LoggerConfig {}
131
132unsafe impl Sync for LoggerConfig {}