scsys_config/services/
logger.rs1#[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 pub const fn level(&self) -> LogLevel {
49 self.level
50 }
51 pub const fn level_mut(&mut self) -> &mut LogLevel {
53 &mut self.level
54 }
55 pub const fn name(&self) -> &String {
57 &self.name
58 }
59 pub const fn name_mut(&mut self) -> &mut String {
61 &mut self.name
62 }
63 pub const fn tracing(&self) -> TracingConfig {
65 self.tracing
66 }
67 pub const fn tracing_mut(&mut self) -> &mut TracingConfig {
69 &mut self.tracing
70 }
71 pub fn set_level(&mut self, level: LogLevel) -> &mut Self {
73 self.level = level;
74 self
75 }
76 pub fn set_name<T: ToString>(&mut self, name: T) -> &mut Self {
78 self.name = name.to_string();
79 self
80 }
81 pub fn set_tracing(&mut self, tracing: TracingConfig) -> &mut Self {
83 self.tracing = tracing;
84 self
85 }
86 pub fn with_level(self, level: LogLevel) -> Self {
88 Self { level, ..self }
89 }
90 pub fn with_name<T: ToString>(self, name: T) -> Self {
92 Self {
93 name: name.to_string(),
94 ..self
95 }
96 }
97 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
114impl 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 {}