Skip to main content

rustbasic_core/
logger.rs

1use std::fs::OpenOptions;
2use std::io::Write;
3use crate::chrono::Local;
4use crate::colored::Colorize;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum Level {
8    Error,
9    Warn,
10    Info,
11    Debug,
12    Trace,
13}
14
15impl std::fmt::Display for Level {
16    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17        let s = match self {
18            Level::Error => "ERROR",
19            Level::Warn => "WARN",
20            Level::Info => "INFO",
21            Level::Debug => "DEBUG",
22            Level::Trace => "TRACE",
23        };
24        write!(f, "{}", s)
25    }
26}
27
28pub struct LoggerGuard;
29
30/// Initialize the logger. Displays the banner and returns a dummy guard to keep API compatibility.
31pub fn init() -> LoggerGuard {
32    print_banner();
33    LoggerGuard
34}
35
36/// Core logging function that handles console output with colors and appends to a daily rolling log file.
37pub fn log(level: Level, msg: &str) {
38    let timestamp = Local::now().format("%Y-%m-%d %H:%M").to_string();
39
40    let level_str = format!("[{}]", level);
41    let level_colored = match level {
42        Level::Error => level_str.red().bold(),
43        Level::Warn => level_str.yellow().bold(),
44        Level::Info => level_str.green().bold(),
45        Level::Debug => level_str.blue().bold(),
46        Level::Trace => level_str.magenta().bold(),
47    };
48
49    let console_line = format!("{} [{}] {}", level_colored, timestamp.to_string().dimmed(), msg);
50    println!("{}", console_line);
51
52    // Ensure logs directory exists
53    let _ = std::fs::create_dir_all("storage/logs");
54
55    // Write to daily file (rolling)
56    let date_str = Local::now().format("%Y-%m-%d").to_string();
57    let log_file_path = format!("storage/logs/rustbasic.log.{}", date_str);
58
59    if let Ok(mut file) = OpenOptions::new()
60        .create(true)
61        .write(true)
62        .append(true)
63        .open(&log_file_path)
64    {
65        let file_line = format!("{} {} {}\n", timestamp, level_str, msg);
66        let _ = file.write_all(file_line.as_bytes());
67    }
68}
69
70fn print_banner() {
71    // println!();
72    // println!("    \x1b[38;5;208m██████╗ ██╗   ██╗███████╗████████╗\x1b[38;5;245m██████╗  █████╗ ███████╗██╗ ██████╗\x1b[0m");
73    // println!("    \x1b[38;5;208m██╔══██╗██║   ██║██╔════╝╚══██╔══╝\x1b[38;5;245m██╔══██╗██╔══██╗██╔════╝██║██╔════╝\x1b[0m");
74    // println!("    \x1b[38;5;208m██████╔╝██║   ██║███████╗   ██║   \x1b[38;5;245m██████╔╝███████║███████╗██║██║     \x1b[0m");
75    // println!("    \x1b[38;5;208m██╔══██╗██║   ██║╚════██║   ██║   \x1b[38;5;245m██╔══██╗██╔══██║╚════██║██║██║     \x1b[0m");
76    // println!("    \x1b[38;5;208m██║  ██║╚██████╔╝███████║   ██║   \x1b[38;5;245m██████╔╝██║  ██║███████║██║╚██████╗\x1b[0m");
77    // println!("    \x1b[38;5;208m╚═╝  ╚═╝ ╚═════╝ ╚══════╝   ╚═╝   \x1b[38;5;245m╚═════╝ ╚═╝  ╚═╝╚══════╝╚═╝ ╚═════╝\x1b[0m");
78    // println!();
79    // println!("    >> \x1b[1;38;5;208mRust\x1b[0m\x1b[1;38;5;245mBasic\x1b[0m Full-stack Framework - Version 2026 <<");
80    println!();
81}
82