Skip to content

Commit 41f1f71

Browse files
committed
feat (core): 优化获取日志规则及方式
1 parent 84dab1a commit 41f1f71

1 file changed

Lines changed: 33 additions & 6 deletions

File tree

src-tauri/src/utils/logger.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub async fn reset_log_directory(_app: AppHandle) -> Result<(), String> {
9595
}
9696

9797
// 获取日志文件列表
98-
#[command]
98+
#[tauri::command]
9999
pub async fn get_log_files(app: AppHandle) -> Result<Vec<String>, String> {
100100
let log_dir = {
101101
let guard = LOG_DIRECTORY.lock().unwrap();
@@ -106,22 +106,49 @@ pub async fn get_log_files(app: AppHandle) -> Result<Vec<String>, String> {
106106
};
107107
info!("获取日志 -> 日志目录为: {}", log_dir.display());
108108

109+
// 获取今天的日期字符串
110+
let today = chrono::Local::now().format("%Y-%m-%d").to_string();
111+
info!("获取日志 -> 查找今天({})的日志文件", today);
112+
109113
let mut log_files = Vec::new();
110114

111115
if let Ok(entries) = std::fs::read_dir(&log_dir) {
112116
for entry in entries.flatten() {
113117
if let Some(filename) = entry.file_name().to_str() {
114-
if filename.ends_with(".log") && filename.starts_with("codeforge-") {
118+
if filename.ends_with(".log")
119+
&& filename.starts_with("codeforge-")
120+
&& filename.contains(&today)
121+
{
115122
log_files.push(filename.to_string());
116-
info!("获取日志 -> 发现日志文件: {}", filename);
123+
info!("获取日志 -> 发现今天的日志文件: {}", filename);
117124
}
118125
}
119126
}
120127
}
121128

122-
info!("获取日志 -> 找到 {} 个日志文件", log_files.len());
123-
log_files.sort();
124-
log_files.reverse(); // 最新的在前面
129+
info!("获取日志 -> 找到 {} 个今天的日志文件", log_files.len());
130+
131+
// 按日志级别排序:error -> warn -> info -> debug -> 普通日志
132+
log_files.sort_by(|a, b| {
133+
let get_priority = |filename: &str| -> u8 {
134+
if filename.contains("-info-") {
135+
1
136+
} else if filename.contains("-warn-") {
137+
2
138+
} else if filename.contains("-error-") {
139+
3
140+
} else if filename.contains("-debug-") {
141+
4
142+
} else {
143+
0
144+
}
145+
};
146+
147+
let priority_a = get_priority(a);
148+
let priority_b = get_priority(b);
149+
150+
priority_a.cmp(&priority_b).then_with(|| a.cmp(b))
151+
});
125152

126153
Ok(log_files)
127154
}

0 commit comments

Comments
 (0)