Skip to content

Commit 2c8b0a5

Browse files
committed
fix (core): 修复删除日志未适配多 level 格式
1 parent f34de54 commit 2c8b0a5

4 files changed

Lines changed: 57 additions & 9 deletions

File tree

src-tauri/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ chrono = { version = "0.4", features = ["serde"] }
2727
log = "0.4"
2828
fern = "0.7.1"
2929
dirs = "6.0.0"
30+
regex = "1.11.1"

src-tauri/src/utils/logger.rs

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use chrono::{Duration, Local};
12
use log::{error, info, warn};
23
use std::path::PathBuf;
34
use std::sync::Mutex;
@@ -156,8 +157,6 @@ pub async fn get_log_files(app: AppHandle) -> Result<Vec<String>, String> {
156157
// 清除日志
157158
#[command]
158159
pub async fn clear_logs(app: AppHandle, keep_days: u32) -> Result<u32, String> {
159-
use chrono::{Duration, Local};
160-
161160
let log_dir = {
162161
let guard = LOG_DIRECTORY.lock().unwrap();
163162
match &*guard {
@@ -176,13 +175,54 @@ pub async fn clear_logs(app: AppHandle, keep_days: u32) -> Result<u32, String> {
176175
if let Some(filename) = entry.file_name().to_str() {
177176
if filename.ends_with(".log") && filename.starts_with("codeforge-") {
178177
scanned_count += 1;
179-
// 从文件名提取日期 codeforge-2024-08-09.log
180-
if let Some(date_str) = filename
181-
.strip_prefix("codeforge-")
182-
.and_then(|s| s.strip_suffix(".log"))
183-
{
178+
179+
// 提取日期的函数
180+
let extract_date = |filename: &str| -> Option<String> {
181+
// 支持的日志文件格式:
182+
// 1. codeforge-2025-08-12.log
183+
// 2. codeforge-info-2025-08-12.log
184+
// 3. codeforge-warn-2025-08-12.log
185+
// 4. codeforge-error-2025-08-12.log
186+
// 5. codeforge-debug-2025-08-12.log
187+
188+
if let Some(without_prefix) = filename.strip_prefix("codeforge-") {
189+
if let Some(without_suffix) = without_prefix.strip_suffix(".log") {
190+
// 使用正则表达式匹配日期部分
191+
// 匹配模式:可选的级别前缀 + 日期
192+
let date_patterns = [
193+
// 直接日期格式:codeforge-2025-08-12.log
194+
r"^(\d{4}-\d{2}-\d{2})$",
195+
// 带级别前缀:codeforge-info-2025-08-12.log
196+
r"^(?:info|warn|error|debug)-(\d{4}-\d{2}-\d{2})$",
197+
];
198+
199+
for pattern in &date_patterns {
200+
if let Ok(re) = regex::Regex::new(pattern) {
201+
if let Some(captures) = re.captures(without_suffix) {
202+
if let Some(date_match) = captures.get(1) {
203+
return Some(date_match.as_str().to_string());
204+
}
205+
}
206+
}
207+
}
208+
209+
// 如果正则匹配失败,尝试简单的字符串处理
210+
// 检查是否以日期结尾(最后10个字符应该是日期格式)
211+
if without_suffix.len() >= 10 {
212+
let potential_date =
213+
&without_suffix[without_suffix.len() - 10..];
214+
if potential_date.matches('-').count() == 2 {
215+
return Some(potential_date.to_string());
216+
}
217+
}
218+
}
219+
}
220+
None
221+
};
222+
223+
if let Some(date_str) = extract_date(filename) {
184224
if let Ok(file_date) =
185-
chrono::NaiveDate::parse_from_str(date_str, "%Y-%m-%d")
225+
chrono::NaiveDate::parse_from_str(&date_str, "%Y-%m-%d")
186226
{
187227
if file_date < cutoff_date.date_naive() {
188228
if let Err(e) = std::fs::remove_file(entry.path()) {
@@ -192,14 +232,19 @@ pub async fn clear_logs(app: AppHandle, keep_days: u32) -> Result<u32, String> {
192232
deleted_count += 1;
193233
}
194234
} else {
195-
info!("清理日志 -> 日志文件文件未到期,将被保留: {}", filename);
235+
info!("清理日志 -> 日志文件未到期,将被保留: {}", filename);
196236
}
237+
} else {
238+
warn!("清理日志 -> 无法解析日期格式 {}: {}", filename, date_str);
197239
}
240+
} else {
241+
warn!("清理日志 -> 无法从文件名提取日期: {}", filename);
198242
}
199243
}
200244
}
201245
}
202246
}
247+
203248
info!(
204249
"清理日志 -> 扫描 {} 个日志文件,删除 {} 个",
205250
scanned_count, deleted_count

src/components/setting/General.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ const logFiles = ref<string[]>([])
122122
const keepDays = ref(30)
123123
124124
const keepDaysOptions = [
125+
{ label: '保留 1 天', value: 1 },
125126
{ label: '保留 7 天', value: 7 },
126127
{ label: '保留 14 天', value: 14 },
127128
{ label: '保留 30 天', value: 30 },

0 commit comments

Comments
 (0)