apollo_logger/
lib.rs

1pub mod foreground_colors;
2pub mod background_colors;
3pub mod font_mode;
4pub mod levels;
5
6use chrono::Utc;
7use crate::foreground_colors::ForegroundColors;
8use crate::background_colors::BackgroundColors;
9use crate::levels::Levels;
10
11pub struct Apollo {
12    pub logging_level: Levels
13}
14
15impl Default for Apollo {
16    fn default() -> Self {
17        Self::new()
18    }
19}
20
21impl Apollo {
22
23    /// Creates a new Apollo instance
24    ///
25    /// # Examples
26    ///
27    /// ```
28    /// use crate::apollo_logger::Apollo;
29    ///
30    /// let l = Apollo::new(); // The default logging level is Debug
31    ///
32    /// l.debug("This message will be printed");
33    /// l.warn("This message will also be printed");
34    /// ```
35    /// If you require a different logging level, please use the following code instead
36    /// ```
37    /// use crate::apollo_logger::Apollo;
38    /// use crate::apollo_logger::levels::Levels;
39    ///
40    /// let l = Apollo { logging_level: Levels::INFO };
41    ///
42    /// l.debug("This message will NOT printed");
43    /// l.warn("This message will be printed");
44    /// ```
45    pub fn new() -> Apollo {
46        Apollo { logging_level: Levels::DEBUG }
47    }
48
49    /// Prints a message to the console with the DEBUG label
50    ///
51    /// # Arguments
52    ///
53    /// * `s`: String to print to the console
54    ///
55    /// # Examples
56    ///
57    /// ```
58    /// use crate::apollo_logger::Apollo;
59    ///
60    /// let l = Apollo::new();
61    ///
62    /// l.warn("This is an debug message");
63    /// ```
64    pub fn debug(&self, s: &str) -> Option<String> {
65
66        // Check if the logging level is high enough
67        if self.logging_level.as_u8() > Levels::DEBUG.as_u8() {
68            return None;
69        }
70
71        // Get current time is [day/month/year hour:minute:second] format
72        let current_time = Utc::now().format("%D %H:%M:%S").to_string();
73
74        // Get colors to print
75        let color_date = ForegroundColors::bright_green();
76        let color_label = ForegroundColors::cyan();
77        let color_text = ForegroundColors::bright_white();
78
79        // Print to console
80        let message = format!("{color_date}[{current_time}]\x1B[0m {color_label}[ DEBUG ]\x1B[0m | {color_text}{s}\x1B[0m");
81        println!("{message}");
82
83        Some(message)
84    }
85
86    /// Prints a message to the console with the INFO label
87    ///
88    /// # Arguments
89    ///
90    /// * `s`: String to print to the console
91    ///
92    /// # Examples
93    ///
94    /// ```
95    /// use crate::apollo_logger::Apollo;
96    ///
97    /// let l = Apollo::new();
98    ///
99    /// l.info("This is an info message");
100    /// ```
101    pub fn info(&self, s: &str) -> Option<String> {
102
103        // Check if the logging level is high enough
104        if self.logging_level.as_u8() > Levels::INFO.as_u8() {
105            return None;
106        }
107
108        // Get current time is [day/month/year hour:minute:second] format
109        let current_time = Utc::now().format("%D %H:%M:%S").to_string();
110
111        // Get colors to print
112        let color_date = ForegroundColors::bright_green();
113        let color_label = ForegroundColors::blue();
114        let color_text = ForegroundColors::bright_white();
115
116        // Print to console
117        let message = format!("{color_date}[{current_time}]\x1B[0m {color_label}[ INFO  ]\x1B[0m | {color_text}{s}\x1B[0m");
118        println!("{message}");
119
120        Some(message)
121    }
122
123    /// Prints a message to the console with the WARN label
124    ///
125    /// # Arguments
126    ///
127    /// * `s`: String to print to the console
128    ///
129    /// # Examples
130    ///
131    /// ```
132    /// use crate::apollo_logger::Apollo;
133    ///
134    /// let l = Apollo::new();
135    ///
136    /// l.warn("This is an warning message");
137    /// ```
138    pub fn warn(&self, s: &str) -> Option<String> {
139
140        // Check if the logging level is high enough
141        if self.logging_level.as_u8() > Levels::WARN.as_u8() {
142            return None;
143        }
144
145        // Get current time is [day/month/year hour:minute:second] format
146        let current_time = Utc::now().format("%D %H:%M:%S").to_string();
147
148        // Get colors to print
149        let color_date = ForegroundColors::bright_green();
150        let color_label = ForegroundColors::yellow();
151        let color_text = ForegroundColors::bright_white();
152
153        // Print to console
154        let message = format!("{color_date}[{current_time}]\x1B[0m {color_label}[ WARN  ]\x1B[0m | {color_text}{s}\x1B[0m");
155        println!("{message}");
156
157        Some(message)
158    }
159
160    /// Prints a message to the console with the ERROR label
161    ///
162    /// # Arguments
163    ///
164    /// * `s`: String to print to the console
165    ///
166    /// # Examples
167    ///
168    /// ```
169    /// use crate::apollo_logger::Apollo;
170    ///
171    /// let l = Apollo::new();
172    ///
173    /// l.error("This is an error message");
174    /// ```
175    pub fn error(&self, s: &str) -> Option<String> {
176
177        // Check if the logging level is high enough
178        if self.logging_level.as_u8() > Levels::ERROR.as_u8() {
179            return None;
180        }
181
182        // Get current time is [day/month/year hour:minute:second] format
183        let current_time = Utc::now().format("%D %H:%M:%S").to_string();
184
185        // Get colors to print
186        let color_date = ForegroundColors::bright_green();
187        let color_label = ForegroundColors::red();
188        let color_text = ForegroundColors::bright_white();
189
190        // Print to console
191        let message = format!("{color_date}[{current_time}]\x1B[0m {color_label}[ ERROR ]\x1B[0m | {color_text}{s}\x1B[0m");
192        eprintln!("{message}");
193
194        Some(message)
195    }
196
197    /// Prints a message to the console with the CRITICAL label
198    ///
199    /// # Arguments
200    ///
201    /// * `s`: String to print to the console
202    ///
203    /// # Examples
204    ///
205    /// ```
206    /// use crate::apollo_logger::Apollo;
207    ///
208    /// let l = Apollo::new();
209    ///
210    /// l.critical("This is an critical message");
211    /// ```
212    pub fn critical(&self, s: &str) -> Option<String> {
213
214        // Check if the logging level is high enough
215        if self.logging_level.as_u8() > Levels::CRITICAL.as_u8() {
216            return None;
217        }
218
219        // Get current time is [day/month/year hour:minute:second] format
220        let current_time = Utc::now().format("%D %H:%M:%S").to_string();
221
222        // Get colors to print
223        let color_date = ForegroundColors::bright_green();
224        let color_label = ForegroundColors::bright_red();
225        let color_text = ForegroundColors::bright_white() + BackgroundColors::bright_red();
226
227        // Print to console
228        let message = format!("{color_date}[{current_time}]\x1B[0m {color_label}[ CRIT  ]\x1B[0m | {color_text}{s}\x1B[0m");
229        eprintln!("{message}");
230
231        Some(message)
232    }
233}
234
235#[cfg(test)]
236mod tests {
237    // Note this useful idiom: importing names from outer (for mod tests) scope.
238    use super::*;
239
240    /// Test if debug will log to console with default logger level
241    #[test]
242    fn test_debug() {
243        let logger = Apollo::new();
244        assert!(logger.debug("This is a test debug message").is_some());
245    }
246
247    /// Test if debug will return None when logging level is too high
248    #[test]
249    fn test_debug_under_level() {
250        let logger = Apollo { logging_level: Levels::INFO };
251        assert!(logger.debug("This is a test debug message").is_none());
252    }
253
254    /// Test if info will log to console with default logger level
255    #[test]
256    fn test_info() {
257        let logger = Apollo::new();
258        assert!(logger.info("This is a test info message").is_some());
259    }
260
261    /// Test if info will return None when logging level is too high
262    #[test]
263    fn test_info_under_level() {
264        let logger = Apollo { logging_level: Levels::WARN };
265        assert!(logger.info("This is a test info message").is_none());
266    }
267
268    /// Test if warn will log to console with default logger level
269    #[test]
270    fn test_warn() {
271        let logger = Apollo::new();
272        assert!(logger.warn("This is a test warning message").is_some());
273    }
274
275    /// Test if warn will return None when logging level is too high
276    #[test]
277    fn test_warn_under_level() {
278        let logger = Apollo { logging_level: Levels::ERROR };
279        assert!(logger.warn("This is a test warning message").is_none());
280    }
281
282    /// Test if error will log to console with default logger level
283    #[test]
284    fn test_error() {
285        let logger = Apollo::new();
286        assert!(logger.error("This is a test error message").is_some());
287    }
288
289    /// Test if error will return None when logging level is too high
290    #[test]
291    fn test_error_under_level() {
292        let logger = Apollo { logging_level: Levels::CRITICAL };
293        assert!(logger.error("This is a test error message").is_none());
294    }
295
296    /// Test if critical will log to console with default logger level
297    #[test]
298    fn test_critical() {
299        let logger = Apollo::new();
300        assert!(logger.critical("This is a test critical message").is_some());
301    }
302
303    /// Test if critical will return None when logging level is too high
304    #[test]
305    fn test_critical_under_level() {
306        let logger = Apollo { logging_level: Levels::NONE };
307        assert!(logger.critical("This is a test critical message").is_none());
308    }
309
310    /// Test if nothing gets logged when logging level is None
311    #[test]
312    fn test_logging_level_none() {
313        let logger = Apollo { logging_level: Levels::NONE };
314        assert!(logger.debug("This is a test debug message").is_none());
315        assert!(logger.info("This is a test info message").is_none());
316        assert!(logger.warn("This is a test warning message").is_none());
317        assert!(logger.error("This is a test error message").is_none());
318        assert!(logger.critical("This is a test critical message").is_none());
319    }
320}