feat: add configurable log size and count options in settings

This commit is contained in:
Tunglies
2025-09-22 16:31:38 +08:00
parent 22e1d329cb
commit 620922f82e
5 changed files with 99 additions and 7 deletions

View File

@@ -14,6 +14,12 @@ pub struct IVerge {
/// silent | error | warn | info | debug | trace
pub app_log_level: Option<String>,
/// app log max size in KB
pub app_log_max_size: Option<u64>,
/// app log max count
pub app_log_max_count: Option<usize>,
// i18n
pub language: Option<String>,
@@ -350,6 +356,8 @@ impl IVerge {
pub fn template() -> Self {
Self {
app_log_max_size: Some(128),
app_log_max_count: Some(8),
clash_core: Some("verge-mihomo".into()),
language: Some(Self::get_system_language()),
theme_mode: Some("system".into()),
@@ -426,6 +434,9 @@ impl IVerge {
}
patch!(app_log_level);
patch!(app_log_max_size);
patch!(app_log_max_count);
patch!(language);
patch!(theme_mode);
patch!(tray_event);
@@ -524,6 +535,8 @@ impl IVerge {
#[derive(Debug, Clone, Serialize)]
pub struct IVergeResponse {
pub app_log_level: Option<String>,
pub app_log_max_size: Option<u64>,
pub app_log_max_count: Option<usize>,
pub language: Option<String>,
pub theme_mode: Option<String>,
pub tray_event: Option<String>,
@@ -595,6 +608,8 @@ impl From<IVerge> for IVergeResponse {
let valid_clash_core = verge.get_valid_clash_core();
Self {
app_log_level: verge.app_log_level,
app_log_max_size: verge.app_log_max_size,
app_log_max_count: verge.app_log_max_count,
language: verge.language,
theme_mode: verge.theme_mode,
tray_event: verge.tray_event,

View File

@@ -22,7 +22,16 @@ use tokio::fs::DirEntry;
/// initialize this instance's log file
#[cfg(not(feature = "tauri-dev"))]
pub async fn init_logger() -> Result<()> {
let log_level = Config::verge().await.latest_ref().get_log_level();
// TODO 提供 runtime 级别实时修改
let (log_level, log_max_size, log_max_count) = {
let verge_guard = Config::verge().await;
let verge = verge_guard.latest_ref();
(
verge.get_log_level(),
verge.app_log_max_size.unwrap_or(128),
verge.app_log_max_count.unwrap_or(8),
)
};
let log_dir = dirs::app_logs_dir()?;
let logger = Logger::with(LogSpecification::from(log_level))
@@ -31,14 +40,12 @@ pub async fn init_logger() -> Result<()> {
.format(console_colored_format)
.format_for_files(file_format)
.rotate(
// ? 总是保持单个日志最大 10 MB
Criterion::Size(10 * 1024 * 1024),
Criterion::Size(log_max_size * 1024),
flexi_logger::Naming::TimestampsCustomFormat {
current_infix: Some("latest"),
format: "%Y-%m-%d_%H-%M-%S",
},
// TODO 提供前端设置最大保留文件数量
Cleanup::Never,
Cleanup::KeepLogFiles(log_max_count),
)
.filter(Box::new(NoExternModule));