use flexi_logger::{Cleanup, Criterion, FileSpec, Logger, Naming};
use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::fmt::{Debug, Display};
static LOG_CONFIGS: OnceLock<HashMap<&'static str, String>> = OnceLock::new();
fn get_base(ltype: &str) -> String {
ltype.to_string()
}
#[derive(Debug, Clone)]
pub struct RuLog {}
impl ruentity::BaseEntitySingle for RuLog {}
impl RuLog {
pub fn new() -> RuLog {
get_log_info();
RuLog {}
}
}
impl RuLog {
pub fn info(&self, msg: &str) {
println!("{}", msg);
}
}
use crate::rubase::{BaseEntity, ruentity};
use crate::rulog;
use flexi_logger::FlexiLoggerError::LevelFilter;
use flexi_logger::LoggerHandle;
use log::Level;
use crate::rubase::rutils::{json_utils, struct2jsonpretty};
use serde::Serialize;
use serde_json::Value;
use std::sync::OnceLock;
static LOGGERS: OnceLock<HashMap<String, LoggerHandle>> = OnceLock::new();
fn init_loggers() -> &'static HashMap<String, LoggerHandle> {
LOGGERS.get_or_init(|| {
let mut map = HashMap::new();
for ltype in &["db", "info", "error"] {
let handle = create_logger(ltype);
map.insert(ltype.to_string(), handle);
}
map
})
}
fn create_logger(ltype: &str) -> LoggerHandle {
let log_level = match ltype {
"db" => "info", "info" => "info",
"error" => "error", _ => "info",
};
Logger::try_with_str(log_level)
.unwrap()
.log_to_file(
FileSpec::default()
.directory("./logs")
.basename(ltype) .suppress_timestamp()
.suffix(".log"),
)
.append()
.format(|w, now, record| {
write!(
w,
"[{}] [{}] {}:{} - {}",
now.format("%Y-%m-%d %H:%M:%S%.3f"),
record.level(),
record.module_path().unwrap_or("unknown"),
record.line().unwrap_or(0),
record.args()
)
})
.rotate(
Criterion::Size(50_000_000), Naming::Numbers, Cleanup::KeepLogFiles(5), )
.start()
.unwrap()
}
pub fn get_log(ltype: &str) -> Option<&'static LoggerHandle> {
let loggers = init_loggers();
loggers.get(ltype)
}
pub fn get_base1(ltype: &str) -> String {
ltype.to_string()
}
static CONFIG: OnceLock<String> = OnceLock::new();
fn get_log_db() -> &'static String {
get_log_config("db")
}
fn get_log_info() -> &'static String {
get_log_config("info")
}
fn get_log_error() -> &'static String {
get_log_config("error")
}
fn get_basename(ltype: &str) -> String {
"app".to_string()
}
fn get_log_config(ltype: &str) -> &'static String {
CONFIG.get_or_init(|| {
let _r = Logger::try_with_str(ltype)
.unwrap()
.log_to_file(
FileSpec::default()
.directory("./logs") .basename(get_basename(ltype)) .suppress_timestamp()
.suffix(".log"), )
.append()
.format(|w, now, record| {
write!(
w,
"[{}] [{}] {}:{} - {}",
now.format("%Y-%m-%d %H:%M:%S%.3f"),
record.level(),
record.module_path().unwrap_or("unknown"),
record.line().unwrap_or(0),
record.args()
)
})
.rotate(
Criterion::Size(50_000_000), Naming::Numbers, Cleanup::KeepLogFiles(5), )
.start();
return String::from("init log");
})
}
pub fn info(msg0: impl Debug) -> () {
get_log_info();
let msg = format!("{:#?}", msg0);
println!("{}", msg);
log::info!("{}", msg)
}
pub fn error(msg: impl Debug) {
get_log_error();
let msg = format!("{:#?}", msg);
println!("{}", msg);
log::error!("{}", msg)
}
pub fn db(msg: impl Debug) {
get_log_db();
let msg = format!("{:#?}", msg);
println!("{}", msg);
log::debug!("{}", msg)
}
pub fn debug(msg: impl Debug) {
get_log_info();
let msg = format!("{:#?}", msg);
println!("{}", msg);
log::debug!("{}", msg)
}
#[allow(unused)]
macro_rules! info {
($($arg:tt)*) => {
println!("{}", format!($($arg)*));
log::info!("{}", format!($($arg)*));
};
}
pub fn info2(msg1: impl Debug, msg2: impl Debug) -> () {
rulog::find_bean_ru_log();
let mut msg = format!("{:#?} {:#?}", msg1, msg2);
println!("{}", msg);
log::info!("{}", msg);
}
pub fn error2(msg1: &str, msg2: impl Debug) -> () {
get_log_error();
let msg = format!("{} {:#?}", msg1, msg2);
println!("{}", msg);
log::error!("{}", msg);
}
pub fn debug2(msg1: &str, msg2: impl Debug) -> () {
rulog::find_bean_ru_log();
let msg = format!("{} {:#?}", msg1, msg2);
println!("{}", msg);
log::debug!("{}", msg);
}
pub fn db2(msg1: &str, msg2: impl Debug) -> () {
get_log_db();
let msg = format!("{} {:#?}", msg1, msg2);
println!("{}", msg);
log::error!("{}", msg);
}