fstdout_logger/config/mod.rs
1//! Configuration options for the logger.
2//!
3//! This module provides the [`LoggerConfig`] struct and [`LoggerConfigBuilder`]
4//! for configuring the behavior of the logger.
5
6use log::LevelFilter;
7
8/// Configuration for the logger.
9///
10/// This struct controls the behavior and appearance of logs, including:
11/// - Minimum log level to display
12/// - Whether to show file and line information
13/// - Whether to show dates in stdout logs
14/// - Whether to use colors in stdout output
15///
16/// # Examples
17///
18/// ```
19/// use fstdout_logger::LoggerConfig;
20/// use log::LevelFilter;
21///
22/// // Create with default settings
23/// let default_config = LoggerConfig::default();
24///
25/// // Create using a builder
26/// let custom_config = LoggerConfig::builder()
27/// .level(LevelFilter::Debug)
28/// .show_file_info(false)
29/// .build();
30///
31/// // Create using presets
32/// let prod_config = LoggerConfig::production();
33/// let dev_config = LoggerConfig::development();
34/// ```
35#[derive(Debug, Clone)]
36pub struct LoggerConfig {
37 /// Whether to show file and line information in log messages
38 pub show_file_info: bool,
39
40 /// Whether to show date in stdout logs (always shown in file logs)
41 pub show_date_in_stdout: bool,
42
43 /// Whether to use colors in stdout logs
44 pub use_colors: bool,
45
46 /// Minimum log level to display
47 pub level: LevelFilter,
48}
49
50impl Default for LoggerConfig {
51 /// Creates a default configuration with:
52 /// - `show_file_info`: `true` - Show file/line information
53 /// - `show_date_in_stdout`: `false` - Only show time in stdout
54 /// - `use_colors`: `true` - Use colors in stdout output
55 /// - `level`: `Info` - Only show Info level and above
56 fn default() -> Self {
57 Self {
58 show_file_info: true,
59 show_date_in_stdout: false,
60 use_colors: true,
61 level: LevelFilter::Info,
62 }
63 }
64}
65
66impl LoggerConfig {
67 /// Create a new logger configuration with default settings.
68 ///
69 /// This is equivalent to calling `LoggerConfig::default()`.
70 pub fn new() -> Self {
71 Self::default()
72 }
73
74 /// Create a new configuration builder.
75 ///
76 /// This returns a [`LoggerConfigBuilder`] that can be used to construct
77 /// a custom configuration with a fluent API.
78 ///
79 /// # Example
80 ///
81 /// ```
82 /// use fstdout_logger::LoggerConfig;
83 /// use log::LevelFilter;
84 ///
85 /// let config = LoggerConfig::builder()
86 /// .level(LevelFilter::Debug)
87 /// .show_file_info(false)
88 /// .build();
89 /// ```
90 pub fn builder() -> LoggerConfigBuilder {
91 LoggerConfigBuilder::default()
92 }
93
94 /// Create a new configuration optimized for production use.
95 ///
96 /// Production settings:
97 /// - `show_file_info`: `false` - Hide file/line for cleaner logs
98 /// - `show_date_in_stdout`: `false` - Only show time in stdout
99 /// - `use_colors`: `true` - Keep colors for readability
100 /// - `level`: `Info` - Hide Debug/Trace logs in production
101 pub fn production() -> Self {
102 Self {
103 show_file_info: false,
104 show_date_in_stdout: false,
105 use_colors: true,
106 level: LevelFilter::Info,
107 }
108 }
109
110 /// Create a new configuration optimized for development use.
111 ///
112 /// Development settings:
113 /// - `show_file_info`: `true` - Show file/line for debugging
114 /// - `show_date_in_stdout`: `false` - Only show time in stdout
115 /// - `use_colors`: `true` - Use colors for readability
116 /// - `level`: `Debug` - Show Debug logs (but not Trace)
117 pub fn development() -> Self {
118 Self {
119 show_file_info: true,
120 show_date_in_stdout: false,
121 use_colors: true,
122 level: LevelFilter::Debug,
123 }
124 }
125}
126
127/// Builder for constructing a [`LoggerConfig`] using a fluent API.
128///
129/// This follows the builder pattern to provide a clean way to create
130/// custom logger configurations.
131///
132/// # Example
133///
134/// ```
135/// use fstdout_logger::LoggerConfigBuilder;
136/// use log::LevelFilter;
137///
138/// let config = LoggerConfigBuilder::default()
139/// .level(LevelFilter::Warn)
140/// .show_file_info(true)
141/// .show_date_in_stdout(true)
142/// .use_colors(false)
143/// .build();
144/// ```
145#[derive(Debug, Default)]
146pub struct LoggerConfigBuilder {
147 config: LoggerConfig,
148}
149
150impl LoggerConfigBuilder {
151 /// Set whether to show file and line information in log messages.
152 ///
153 /// When enabled, each log message will include the source file and line
154 /// number where the log was created. This is useful for debugging but
155 /// can make logs more verbose for production use.
156 ///
157 /// Default: `true`
158 pub fn show_file_info(mut self, show: bool) -> Self {
159 self.config.show_file_info = show;
160 self
161 }
162
163 /// Set whether to show date in stdout logs.
164 ///
165 /// When enabled, stdout logs will include the full date (YYYY-MM-DD).
166 /// When disabled, only the time (HH:MM:SS) will be shown.
167 /// Note: Log files always include the full date regardless of this setting.
168 ///
169 /// Default: `false`
170 pub fn show_date_in_stdout(mut self, show: bool) -> Self {
171 self.config.show_date_in_stdout = show;
172 self
173 }
174
175 /// Set whether to use colors in stdout logs.
176 ///
177 /// When enabled, different log levels will be displayed in different colors:
178 /// - ERROR: Red
179 /// - WARN: Yellow
180 /// - INFO: Blue
181 /// - DEBUG: Green
182 /// - TRACE: Default terminal color
183 ///
184 /// Note: Log files never include color codes regardless of this setting.
185 ///
186 /// Default: `true`
187 pub fn use_colors(mut self, use_colors: bool) -> Self {
188 self.config.use_colors = use_colors;
189 self
190 }
191
192 /// Set the minimum log level to display.
193 ///
194 /// This filters log messages based on their level:
195 /// - `Error`: Only errors
196 /// - `Warn`: Errors and warnings
197 /// - `Info`: Errors, warnings, and info
198 /// - `Debug`: Errors, warnings, info, and debug
199 /// - `Trace`: All log levels
200 ///
201 /// Default: `Info`
202 pub fn level(mut self, level: LevelFilter) -> Self {
203 self.config.level = level;
204 self
205 }
206
207 /// Build the final configuration.
208 ///
209 /// This consumes the builder and returns a [`LoggerConfig`].
210 pub fn build(self) -> LoggerConfig {
211 self.config
212 }
213}