extern crate chrono; extern crate colored; use chrono::prelude::*;
use colored::*;
use std::env;
pub struct TerminalLogger {
pkg: String,
name: String,
}
impl TerminalLogger {
fn new(name: &str, log_name: Option<&str>) -> TerminalLogger {
let pkg = log_name.unwrap_or(env!("CARGO_PKG_NAME"));
return TerminalLogger {
pkg: pkg.to_string(),
name: name.to_string(),
};
}
pub fn info(&self, cont: &str) {
println!(
"{} [{}:{}] {}: {}",
get_current_time(),
self.pkg.green().bold(),
self.name.blue(),
"INFO".blue(),
cont.to_string()
);
}
pub fn error(&self, cont: &str) {
println!(
"{} [{}:{}] {}: {}",
get_current_time(),
self.pkg.green().bold(),
self.name.blue(),
"ERROR".red(),
cont.to_string()
);
}
pub fn fatal(&self, cont: &str) {
println!(
"{} [{}:{}] {}: {}",
get_current_time(),
self.pkg.green().bold(),
self.name.blue(),
"FATAL".red(),
cont.to_string()
);
}
pub fn critical(&self, cont: &str) {
println!(
"{} [{}:{}] {}: {}",
get_current_time(),
self.pkg.green().bold(),
self.name.blue(),
"CRITICAL".red(),
cont.to_string()
);
}
pub fn debug(&self, cont: &str) {
println!(
"{} [{}:{}] {}: {}",
get_current_time(),
self.pkg.green().bold(),
self.name.blue(),
"DEBUG".magenta(),
cont.to_string()
);
}
pub fn warn(&self, cont: &str) {
println!(
"{} [{}:{}] {}: {}",
get_current_time(),
self.pkg.green().bold(),
self.name.blue(),
"WARN".yellow(),
cont.to_string()
);
}
}
pub fn get(name: &str) -> TerminalLogger {
let terminal_logger = TerminalLogger::new(name, None);
return terminal_logger;
}
pub fn get_current_time() -> String {
let local_time = Local::now();
return local_time.format("%Y-%m-%d %H:%M:%S").to_string();
}