hubcio commented on code in PR #2452:
URL: https://github.com/apache/iggy/pull/2452#discussion_r2652348448


##########
core/server/src/log/logger.rs:
##########
@@ -397,26 +444,199 @@ impl Logging {
         Format::default().with_thread_names(true)
     }
 
-    fn _install_log_rotation_handler(&self) {
-        todo!("Implement log rotation handler based on size and retention 
time");
-    }
-
     fn print_build_info() {
         if option_env!("IGGY_CI_BUILD") == Some("true") {
             let hash = option_env!("VERGEN_GIT_SHA").unwrap_or("unknown");
             let built_at = 
option_env!("VERGEN_BUILD_TIMESTAMP").unwrap_or("unknown");
             let rust_version = 
option_env!("VERGEN_RUSTC_SEMVER").unwrap_or("unknown");
             let target = 
option_env!("VERGEN_CARGO_TARGET_TRIPLE").unwrap_or("unknown");
             info!(
-                "Version: {VERSION}, hash: {}, built at: {} using rust 
version: {} for target: {}",
-                hash, built_at, rust_version, target
+                "Version: {VERSION}, hash: {hash}, built at: {built_at} using 
rust version: {rust_version} for target: {target}"
             );
         } else {
             info!(
                 "It seems that you are a developer. Environment variable 
IGGY_CI_BUILD is not set to 'true', skipping build info print."
             )
         }
     }
+
+    fn calculate_max_files(max_total_size_bytes: u64, max_file_size_bytes: 
u64) -> usize {
+        if max_file_size_bytes == 0 {
+            return 10;
+        }
+
+        let max_files = max_total_size_bytes / max_file_size_bytes;
+        max_files.clamp(1, 1000) as usize
+    }
+
+    fn _install_log_rotation_handler(&self, config: &LoggingConfig, logs_path: 
Option<&PathBuf>) {
+        if let Some(logs_path) = logs_path {
+            let path = logs_path.to_path_buf();
+            let max_size = config.max_size.as_bytes_u64();
+            let retention = config.retention.get_duration();
+            let check_interval = config.rotation_check_interval;
+            let should_stop = Arc::clone(&self.rotation_should_stop);
+
+            std::thread::spawn(move || {
+                loop {
+                    if should_stop.load(Ordering::Relaxed) {
+                        break;
+                    }
+
+                    std::thread::sleep(Duration::from_secs(check_interval));
+
+                    if should_stop.load(Ordering::Relaxed) {
+                        break;
+                    }
+
+                    Self::cleanup_log_files(&path, retention, max_size);

Review Comment:
   you pass `max_size` here which is per-file size, but `cleanup_log_files` 
expects total limit



##########
core/server/src/log/logger.rs:
##########
@@ -397,26 +444,199 @@ impl Logging {
         Format::default().with_thread_names(true)
     }
 
-    fn _install_log_rotation_handler(&self) {
-        todo!("Implement log rotation handler based on size and retention 
time");
-    }
-
     fn print_build_info() {
         if option_env!("IGGY_CI_BUILD") == Some("true") {
             let hash = option_env!("VERGEN_GIT_SHA").unwrap_or("unknown");
             let built_at = 
option_env!("VERGEN_BUILD_TIMESTAMP").unwrap_or("unknown");
             let rust_version = 
option_env!("VERGEN_RUSTC_SEMVER").unwrap_or("unknown");
             let target = 
option_env!("VERGEN_CARGO_TARGET_TRIPLE").unwrap_or("unknown");
             info!(
-                "Version: {VERSION}, hash: {}, built at: {} using rust 
version: {} for target: {}",
-                hash, built_at, rust_version, target
+                "Version: {VERSION}, hash: {hash}, built at: {built_at} using 
rust version: {rust_version} for target: {target}"
             );
         } else {
             info!(
                 "It seems that you are a developer. Environment variable 
IGGY_CI_BUILD is not set to 'true', skipping build info print."
             )
         }
     }
+
+    fn calculate_max_files(max_total_size_bytes: u64, max_file_size_bytes: 
u64) -> usize {
+        if max_file_size_bytes == 0 {
+            return 10;
+        }
+
+        let max_files = max_total_size_bytes / max_file_size_bytes;
+        max_files.clamp(1, 1000) as usize
+    }
+
+    fn _install_log_rotation_handler(&self, config: &LoggingConfig, logs_path: 
Option<&PathBuf>) {

Review Comment:
   remove prefix `_`



##########
core/server/src/log/logger.rs:
##########
@@ -397,26 +444,199 @@ impl Logging {
         Format::default().with_thread_names(true)
     }
 
-    fn _install_log_rotation_handler(&self) {
-        todo!("Implement log rotation handler based on size and retention 
time");
-    }
-
     fn print_build_info() {
         if option_env!("IGGY_CI_BUILD") == Some("true") {
             let hash = option_env!("VERGEN_GIT_SHA").unwrap_or("unknown");
             let built_at = 
option_env!("VERGEN_BUILD_TIMESTAMP").unwrap_or("unknown");
             let rust_version = 
option_env!("VERGEN_RUSTC_SEMVER").unwrap_or("unknown");
             let target = 
option_env!("VERGEN_CARGO_TARGET_TRIPLE").unwrap_or("unknown");
             info!(
-                "Version: {VERSION}, hash: {}, built at: {} using rust 
version: {} for target: {}",
-                hash, built_at, rust_version, target
+                "Version: {VERSION}, hash: {hash}, built at: {built_at} using 
rust version: {rust_version} for target: {target}"
             );
         } else {
             info!(
                 "It seems that you are a developer. Environment variable 
IGGY_CI_BUILD is not set to 'true', skipping build info print."
             )
         }
     }
+
+    fn calculate_max_files(max_total_size_bytes: u64, max_file_size_bytes: 
u64) -> usize {
+        if max_file_size_bytes == 0 {
+            return 10;
+        }
+
+        let max_files = max_total_size_bytes / max_file_size_bytes;
+        max_files.clamp(1, 1000) as usize
+    }
+
+    fn _install_log_rotation_handler(&self, config: &LoggingConfig, logs_path: 
Option<&PathBuf>) {
+        if let Some(logs_path) = logs_path {
+            let path = logs_path.to_path_buf();
+            let max_size = config.max_size.as_bytes_u64();
+            let retention = config.retention.get_duration();
+            let check_interval = config.rotation_check_interval;
+            let should_stop = Arc::clone(&self.rotation_should_stop);
+
+            std::thread::spawn(move || {
+                loop {
+                    if should_stop.load(Ordering::Relaxed) {
+                        break;
+                    }
+
+                    std::thread::sleep(Duration::from_secs(check_interval));
+
+                    if should_stop.load(Ordering::Relaxed) {
+                        break;
+                    }
+
+                    Self::cleanup_log_files(&path, retention, max_size);
+                }
+            });
+        }
+    }
+
+    fn read_log_files(
+        logs_path: &PathBuf,
+    ) -> Vec<(fs::DirEntry, std::time::SystemTime, Duration, u64)> {
+        use std::fs;
+        use std::time::UNIX_EPOCH;
+
+        let entries = match fs::read_dir(logs_path) {
+            Ok(entries) => entries,
+            Err(e) => {
+                warn!("Failed to read log directory {logs_path:?}: {e}");
+                return Vec::new();
+            }
+        };
+
+        let mut file_entries = Vec::new();
+
+        for entry in entries.flatten() {
+            let metadata = match entry.metadata() {
+                Ok(metadata) => metadata,
+                Err(e) => {
+                    warn!(
+                        "Failed to get metadata for {entry_path:?}: {e}",
+                        entry_path = entry.path()
+                    );
+                    continue;
+                }
+            };
+
+            if !metadata.is_file() {
+                continue;
+            }
+
+            let modified = match metadata.modified() {
+                Ok(modified) => modified,
+                Err(e) => {
+                    warn!(
+                        "Failed to get modification time for {entry_path:?}: 
{e}",
+                        entry_path = entry.path()
+                    );
+                    continue;
+                }
+            };
+
+            let elapsed = match modified.duration_since(UNIX_EPOCH) {
+                Ok(elapsed) => elapsed,
+                Err(e) => {
+                    warn!(
+                        "Failed to calculate elapsed time for {entry_path:?}: 
{e}",
+                        entry_path = entry.path()
+                    );
+                    continue;
+                }
+            };
+
+            let file_size = metadata.len();
+            file_entries.push((entry, modified, elapsed, file_size));
+        }
+
+        file_entries
+    }
+
+    fn cleanup_log_files(logs_path: &PathBuf, retention: Duration, 
max_size_bytes: u64) {

Review Comment:
   use `IggyDuration`/`IggyByteSize` when you can :)



##########
core/server/src/log/logger.rs:
##########
@@ -397,26 +444,199 @@ impl Logging {
         Format::default().with_thread_names(true)
     }
 
-    fn _install_log_rotation_handler(&self) {
-        todo!("Implement log rotation handler based on size and retention 
time");
-    }
-
     fn print_build_info() {
         if option_env!("IGGY_CI_BUILD") == Some("true") {
             let hash = option_env!("VERGEN_GIT_SHA").unwrap_or("unknown");
             let built_at = 
option_env!("VERGEN_BUILD_TIMESTAMP").unwrap_or("unknown");
             let rust_version = 
option_env!("VERGEN_RUSTC_SEMVER").unwrap_or("unknown");
             let target = 
option_env!("VERGEN_CARGO_TARGET_TRIPLE").unwrap_or("unknown");
             info!(
-                "Version: {VERSION}, hash: {}, built at: {} using rust 
version: {} for target: {}",
-                hash, built_at, rust_version, target
+                "Version: {VERSION}, hash: {hash}, built at: {built_at} using 
rust version: {rust_version} for target: {target}"
             );
         } else {
             info!(
                 "It seems that you are a developer. Environment variable 
IGGY_CI_BUILD is not set to 'true', skipping build info print."
             )
         }
     }
+
+    fn calculate_max_files(max_total_size_bytes: u64, max_file_size_bytes: 
u64) -> usize {
+        if max_file_size_bytes == 0 {
+            return 10;
+        }
+
+        let max_files = max_total_size_bytes / max_file_size_bytes;
+        max_files.clamp(1, 1000) as usize
+    }
+
+    fn _install_log_rotation_handler(&self, config: &LoggingConfig, logs_path: 
Option<&PathBuf>) {
+        if let Some(logs_path) = logs_path {
+            let path = logs_path.to_path_buf();
+            let max_size = config.max_size.as_bytes_u64();
+            let retention = config.retention.get_duration();
+            let check_interval = config.rotation_check_interval;
+            let should_stop = Arc::clone(&self.rotation_should_stop);
+
+            std::thread::spawn(move || {
+                loop {
+                    if should_stop.load(Ordering::Relaxed) {
+                        break;
+                    }
+
+                    std::thread::sleep(Duration::from_secs(check_interval));
+
+                    if should_stop.load(Ordering::Relaxed) {
+                        break;
+                    }
+
+                    Self::cleanup_log_files(&path, retention, max_size);
+                }
+            });
+        }
+    }
+
+    fn read_log_files(

Review Comment:
   this function is somewhat incorrect - it looks for ANY file, perhaps you 
have to add something like `const IGGY_LOG_FILE_PREFIX` and filter the files 
based on that



##########
core/server/src/log/logger.rs:
##########
@@ -397,26 +444,199 @@ impl Logging {
         Format::default().with_thread_names(true)
     }
 
-    fn _install_log_rotation_handler(&self) {
-        todo!("Implement log rotation handler based on size and retention 
time");
-    }
-
     fn print_build_info() {
         if option_env!("IGGY_CI_BUILD") == Some("true") {
             let hash = option_env!("VERGEN_GIT_SHA").unwrap_or("unknown");
             let built_at = 
option_env!("VERGEN_BUILD_TIMESTAMP").unwrap_or("unknown");
             let rust_version = 
option_env!("VERGEN_RUSTC_SEMVER").unwrap_or("unknown");
             let target = 
option_env!("VERGEN_CARGO_TARGET_TRIPLE").unwrap_or("unknown");
             info!(
-                "Version: {VERSION}, hash: {}, built at: {} using rust 
version: {} for target: {}",
-                hash, built_at, rust_version, target
+                "Version: {VERSION}, hash: {hash}, built at: {built_at} using 
rust version: {rust_version} for target: {target}"
             );
         } else {
             info!(
                 "It seems that you are a developer. Environment variable 
IGGY_CI_BUILD is not set to 'true', skipping build info print."
             )
         }
     }
+
+    fn calculate_max_files(max_total_size_bytes: u64, max_file_size_bytes: 
u64) -> usize {
+        if max_file_size_bytes == 0 {
+            return 10;
+        }
+
+        let max_files = max_total_size_bytes / max_file_size_bytes;
+        max_files.clamp(1, 1000) as usize
+    }
+
+    fn _install_log_rotation_handler(&self, config: &LoggingConfig, logs_path: 
Option<&PathBuf>) {

Review Comment:
   how are you preventing active log file deletion?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to