1+ use chrono:: { Duration , Local } ;
12use log:: { error, info, warn} ;
23use std:: path:: PathBuf ;
34use std:: sync:: Mutex ;
@@ -156,8 +157,6 @@ pub async fn get_log_files(app: AppHandle) -> Result<Vec<String>, String> {
156157// 清除日志
157158#[ command]
158159pub 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
0 commit comments