Skip to content
This repository has been archived by the owner on Oct 21, 2023. It is now read-only.

Commit

Permalink
log config
Browse files Browse the repository at this point in the history
  • Loading branch information
LaoLittle committed Jan 14, 2023
1 parent a0a62be commit 7f4d965
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 8 deletions.
6 changes: 6 additions & 0 deletions default_config/log.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 日志输出最大等级
# 等级由低至高分别为: Error, Warn, Info, Debug, Tracing
max_level = 'Info'
# 日志时间的输出格式
# 详见'https://time-rs.github.io/book/api/format-description.html'
time_format = '[year]-[month]-[day] [hour]:[minute]:[second]'
39 changes: 39 additions & 0 deletions src/config/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use serde::{Deserialize, Serialize};

pub static DEFAULT_CONFIG: &[u8] = include_bytes!("../../default_config/log.toml");

#[derive(Serialize, Deserialize, Debug)]
pub struct LogConfig {
pub max_level: Level,
pub time_format: String,
}

impl Default for LogConfig {
fn default() -> Self {
Self {
max_level: Level::Info,
time_format: "[year]-[month]-[day] [hour]:[minute]:[second]".into(),
}
}
}

#[derive(Serialize, Deserialize, Debug)]
pub enum Level {
Trace,
Debug,
Info,
Warn,
Error,
}

impl Level {
pub fn as_tracing_level(&self) -> tracing::Level {
match self {
Level::Trace => tracing::Level::TRACE,
Level::Debug => tracing::Level::DEBUG,
Level::Info => tracing::Level::INFO,
Level::Warn => tracing::Level::WARN,
Level::Error => tracing::Level::ERROR,
}
}
}
57 changes: 57 additions & 0 deletions src/config/service.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::config::service_config_dir_path;
use serde::{Deserialize, Serialize};
use std::fs;
use std::marker::PhantomData;
use std::path::PathBuf;
use tracing::error;

pub struct ServiceConfig<T> {
path: PathBuf,
service_name: &'static str,
default_config: &'static [u8],
_mark: PhantomData<T>,
}

impl<T> ServiceConfig<T>
where
for<'a> T: Serialize + Deserialize<'a>,
T: Default,
{
pub fn new(name: &'static str, default: &'static [u8]) -> Self {
Self {
path: service_config_dir_path().join(format!("{name}.toml")),
default_config: default,
service_name: name,
_mark: PhantomData,
}
}

pub fn read(&self) -> T {
if self.path.is_file() {
match fs::read(&self.path) {
Ok(file) => toml::from_slice(&file).unwrap_or_else(|e| {
error!("{e}");
let mut path = self.path.clone();
path.pop();
let mut name = self.service_name.to_owned();
name.push_str(".toml.bak");
path.push(name);
let _ = fs::copy(&self.path, path);
self.write_default()
}),
Err(e) => {
error!("{e}");
self.write_default()
}
}
} else {
self.write_default()
}
}

fn write_default(&self) -> T {
let default = T::default();
let _ = fs::write(&self.path, self.default_config);
default
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(once_cell)]
#![feature(string_leak)]

use dashmap::DashMap;
use ricq::msg::elem::Text;
Expand Down
28 changes: 20 additions & 8 deletions src/service/log.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::config::log::LogConfig;
use crate::config::service::ServiceConfig;
use crate::terminal::buffer::{INPUT_BUFFER, TERMINAL_CLOSED};
use crate::terminal::PROMPT;
use std::io;
Expand All @@ -11,16 +13,23 @@ use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;

pub fn init_logger() -> [WorkerGuard; 3] {
let config = ServiceConfig::<LogConfig>::new("log", crate::config::log::DEFAULT_CONFIG).read();

let local_offset = time::UtcOffset::current_local_offset();

let time_format =
time::format_description::parse("[year]-[month]-[day] [hour]:[minute]:[second]").unwrap();
let mut errors = Vec::with_capacity(2);
let time_format = time::format_description::parse(config.time_format.leak())
.or_else(|e| {
errors.push(format!("日志时间格式错误: {e}, 将使用默认时间格式"));
time::format_description::parse("[year]-[month]-[day] [hour]:[minute]:[second]")
})
.unwrap();

let (s, s_guard) = tracing_appender::non_blocking(LogStdoutWriter);

let stdout_layer = tracing_subscriber::fmt::layer()
.with_target(false)
.with_writer(s.with_max_level(Level::DEBUG));
.with_writer(s.with_max_level(config.max_level.as_tracing_level()));

let file_writer = tracing_appender::rolling::daily("log", "atri_bot.log");
let (f, f_guard) = tracing_appender::non_blocking(file_writer);
Expand All @@ -38,9 +47,12 @@ pub fn init_logger() -> [WorkerGuard; 3] {
.with_ansi(false)
.with_writer(f_err.with_max_level(Level::ERROR));

let (offset, err) = match local_offset {
Ok(ofs) => (ofs, None),
Err(e) => (time::UtcOffset::from_hms(8, 0, 0).unwrap(), Some(e)),
let offset = match local_offset {
Ok(ofs) => ofs,
Err(e) => {
errors.push(format!("初始化日志时间错误: {e}, 将使用默认时区UTC+8"));
time::UtcOffset::from_hms(8, 0, 0).unwrap()
}
};

let timer = OffsetTime::new(offset, time_format);
Expand All @@ -56,8 +68,8 @@ pub fn init_logger() -> [WorkerGuard; 3] {
.with(file_error_layer)
.init();

if let Some(e) = err {
warn!("初始化日志时间错误: {}, 使用默认时区UTC+8", e);
for error in errors {
warn!("{error}");
}

[s_guard, f_guard, f_err_guard]
Expand Down

0 comments on commit 7f4d965

Please sign in to comment.