Skip to main content

genja_core/settings/
logging.rs

1use super::env_defaults::{
2    deserialize_bool_loose, get_default_log_file, get_log_level_default, get_log_to_console_default,
3};
4use serde::{Deserialize, Serialize};
5
6/// Logging settings consumed by applications embedding Genja.
7///
8/// Genja does not install a logging subscriber itself. Environment-backed
9/// defaults are used for `level`, `log_file`, and `to_console`, and the boolean
10/// fields accept the module's loose boolean forms.
11#[derive(Deserialize, Serialize, Clone, Debug)]
12#[serde(default)]
13pub struct LoggingConfig {
14    #[serde(deserialize_with = "deserialize_bool_loose")]
15    enabled: bool,
16    level: String,
17    log_file: String,
18    #[serde(deserialize_with = "deserialize_bool_loose")]
19    to_console: bool,
20    file_size: u64,
21    max_file_count: usize,
22}
23
24impl Default for LoggingConfig {
25    fn default() -> Self {
26        Self {
27            enabled: true,
28            level: get_log_level_default(),
29            log_file: get_default_log_file(),
30            to_console: get_log_to_console_default(),
31            file_size: 1024 * 1024 * 10,
32            max_file_count: 10,
33        }
34    }
35}
36
37impl LoggingConfig {
38    pub fn builder() -> LoggingConfigBuilder {
39        LoggingConfigBuilder::default()
40    }
41
42    pub fn enabled(&self) -> bool {
43        self.enabled
44    }
45
46    pub fn level(&self) -> &str {
47        &self.level
48    }
49
50    pub fn log_file(&self) -> &str {
51        &self.log_file
52    }
53
54    pub fn to_console(&self) -> bool {
55        self.to_console
56    }
57
58    pub fn file_size(&self) -> u64 {
59        self.file_size
60    }
61
62    pub fn max_file_count(&self) -> usize {
63        self.max_file_count
64    }
65}
66
67/// Builder for `LoggingConfig`.
68#[derive(Default)]
69pub struct LoggingConfigBuilder {
70    enabled: Option<bool>,
71    level: Option<String>,
72    log_file: Option<String>,
73    to_console: Option<bool>,
74    file_size: Option<u64>,
75    max_file_count: Option<usize>,
76}
77
78impl LoggingConfigBuilder {
79    pub fn enabled(mut self, enabled: bool) -> Self {
80        self.enabled = Some(enabled);
81        self
82    }
83
84    pub fn level(mut self, level: impl Into<String>) -> Self {
85        self.level = Some(level.into());
86        self
87    }
88
89    pub fn log_file(mut self, log_file: impl Into<String>) -> Self {
90        self.log_file = Some(log_file.into());
91        self
92    }
93
94    pub fn to_console(mut self, to_console: bool) -> Self {
95        self.to_console = Some(to_console);
96        self
97    }
98
99    pub fn file_size(mut self, file_size: u64) -> Self {
100        self.file_size = Some(file_size);
101        self
102    }
103
104    pub fn max_file_count(mut self, max_file_count: usize) -> Self {
105        self.max_file_count = Some(max_file_count);
106        self
107    }
108
109    pub fn build(self) -> LoggingConfig {
110        LoggingConfig {
111            enabled: self.enabled.unwrap_or(true),
112            level: self.level.unwrap_or_else(get_log_level_default),
113            log_file: self.log_file.unwrap_or_else(get_default_log_file),
114            to_console: self.to_console.unwrap_or_else(get_log_to_console_default),
115            file_size: self.file_size.unwrap_or(1024 * 1024 * 10),
116            max_file_count: self.max_file_count.unwrap_or(10),
117        }
118    }
119}