Files
LingTransmit/src/bin/logger/mod.rs
2025-02-23 22:15:44 +08:00

36 lines
1.1 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 版权所有 (c) ling 保留所有权利。
// 除非另行说明否则仅允许在LingTransmit中使用此文件中的代码。
//
// 由 ling 创建于 2025/2/23.
#![allow(non_snake_case)]
use chrono::Local;
use colored::{Color, Colorize};
use fern::Dispatch;
use log::{Level, LevelFilter};
pub fn init_log() {
let console_dispatch = Dispatch::new()
.format(|out, message, record| {
let (title, color) = match record.level() {
Level::Error => ("Error", Color::Red),
Level::Warn => ("Warning", Color::Yellow),
Level::Info => ("Info", Color::Green),
Level::Debug => ("Debug", Color::BrightWhite),
Level::Trace => ("Trace", Color::White),
};
out.finish(format_args!(
"{}",
format!("[{} {}]\t{}", get_time(), title, message).color(color)
))
})
.chain(std::io::stdout())
.level(LevelFilter::Trace)
.apply();
}
fn get_time() -> String {
let now = Local::now();
now.format("%Y-%m-%d %H:%M:%S").to_string()
}