Initialize Twilight project, set up basic framework, initialize DB module
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
use colored::Colorize;
|
||||
use log::{Level, LevelFilter};
|
||||
use time::{OffsetDateTime, macros::format_description};
|
||||
|
||||
pub struct Logger {
|
||||
level: Level,
|
||||
}
|
||||
|
||||
impl log::Log for Logger {
|
||||
fn enabled(&self, metadata: &log::Metadata) -> bool {
|
||||
metadata.level() <= self.level
|
||||
}
|
||||
|
||||
fn log(&self, record: &log::Record) {
|
||||
if self.enabled(record.metadata()) {
|
||||
println!(
|
||||
"{} {} {}",
|
||||
get_formatted_timestamp(),
|
||||
get_formatted_level(record.level()),
|
||||
record.args(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&self) {}
|
||||
}
|
||||
|
||||
static LOGGER: Logger = Logger { level: Level::Info };
|
||||
|
||||
pub fn init() {
|
||||
log::set_logger(&LOGGER)
|
||||
.map(|()| log::set_max_level(LevelFilter::Trace))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn get_formatted_timestamp() -> String {
|
||||
let now = OffsetDateTime::now_local().unwrap_or_else(|_| OffsetDateTime::now_utc());
|
||||
|
||||
let format = format_description!(
|
||||
"[day]/[month]/[year] at [hour]:[minute]:[second].[subsecond digits:2]"
|
||||
);
|
||||
|
||||
now.format(&format).unwrap().dimmed().to_string()
|
||||
}
|
||||
|
||||
fn get_formatted_level(level: Level) -> String {
|
||||
let string = format!("[{level}]");
|
||||
let string = format!("{string:<7}");
|
||||
|
||||
match level {
|
||||
Level::Trace => string.dimmed().to_string(),
|
||||
Level::Debug => string.white().to_string(),
|
||||
Level::Info => string.blue().to_string(),
|
||||
Level::Warn => string.yellow().to_string(),
|
||||
Level::Error => string.red().bold().to_string(),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user