mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-12 16:16:55 +00:00
fix(obs): harden cleaner file matching (#4486)
Co-authored-by: heihutu <heihutu@gmail.com>
This commit is contained in:
@@ -315,7 +315,8 @@ fn init_file_logging_internal(
|
||||
is_production: bool,
|
||||
) -> Result<OtelGuard, TelemetryError> {
|
||||
let service_name = config.service_name.as_deref().unwrap_or(APP_NAME);
|
||||
let log_filename = config.log_filename.as_deref().unwrap_or(service_name);
|
||||
let resolved_log_filename = resolve_log_cleanup_file_pattern(config, service_name);
|
||||
let log_filename = resolved_log_filename.as_str();
|
||||
let keep_files = config.log_keep_files.unwrap_or(DEFAULT_LOG_KEEP_FILES);
|
||||
|
||||
// ── 1. Ensure the log directory exists ───────────────────────────────────
|
||||
@@ -523,7 +524,7 @@ pub fn spawn_cleanup_task(
|
||||
let log_dir = std::path::PathBuf::from(log_directory);
|
||||
// Use suffix matching for log files like `2026-03-01-06-21.rustfs.log`
|
||||
// where `rustfs.log` is the stable suffix generated by the rolling appender.
|
||||
let file_pattern = config.log_filename.as_deref().unwrap_or(log_filename).to_string();
|
||||
let file_pattern = resolve_log_cleanup_file_pattern(config, log_filename);
|
||||
let active_filename = file_pattern.clone();
|
||||
|
||||
// Determine match mode from config, defaulting to the repository-wide
|
||||
@@ -648,6 +649,13 @@ pub fn spawn_cleanup_task(
|
||||
})
|
||||
}
|
||||
|
||||
fn resolve_log_cleanup_file_pattern(config: &OtelConfig, fallback_log_filename: &str) -> String {
|
||||
match config.log_filename.as_deref() {
|
||||
Some(candidate) if !candidate.trim().is_empty() => candidate.to_string(),
|
||||
_ => fallback_log_filename.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -900,4 +908,14 @@ mod tests {
|
||||
|
||||
assert_eq!(resolve_log_cleanup_interval_seconds(&config), DEFAULT_OBS_LOG_CLEANUP_INTERVAL_SECONDS);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_cleanup_pattern_falls_back_to_runtime_log_filename() {
|
||||
let config = OtelConfig {
|
||||
log_filename: Some(" ".to_string()),
|
||||
..OtelConfig::default()
|
||||
};
|
||||
|
||||
assert_eq!(resolve_log_cleanup_file_pattern(&config, "rustfs.log"), "rustfs.log");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user