pub use log::{LevelFilter, trace, debug, info, warn, error}; use chrono::Local; use std::fs::OpenOptions; use std::io::Write; use std::path; use crate::fs_util::{dir_exists, mkdir as create_dir}; /// Must be done before any log macros can work /// /// data/logs/moonmud-MM-DD-YYYY.log pub fn init_logging(level: LevelFilter) -> bool { let f = String::from("data/logs"); if !dir_exists(&f) { if !create_dir(&f) { return false; } } let mut pth = path::PathBuf::new(); pth.push(f); let log_filename = format!("moonmud-{0}.log", Local::now().format("%m-%d-%Y")); pth.push(log_filename); let target = Box::new( OpenOptions::new() .write(true) .append(true) .create(true) .open(&pth) .expect(format!("Can't open log file (write/append/create): {}", &pth.as_path().to_string_lossy()).as_str()), ); env_logger::Builder::new() .target(env_logger::Target::Pipe(target)) .format(|buf, record| { writeln!( buf, "[{} {:5} {}:{}] {}", Local::now().format("%Y-%m-%d %H:%M:%S%.3f"), record.level(), record.file().unwrap_or("unknown"), record.line().unwrap_or(0), record.args() ) }) .filter(None, level) .init(); return true; }