diff --git a/Cargo.lock b/Cargo.lock index a01a47db2..9cbea2695 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2516,6 +2516,16 @@ version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28dea519a9695b9977216879a3ebfddf92f1c08c05d984f8996aecd6ecdc811d" +[[package]] +name = "file-rotate" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a3ed82142801f5b1363f7d463963d114db80f467e860b1cd82228eaebc627a0" +dependencies = [ + "chrono", + "flate2", +] + [[package]] name = "fix-hidden-lifetime-bug" version = "0.2.7" @@ -4851,6 +4861,7 @@ dependencies = [ "common", "crypto", "directories", + "file-rotate", "fs4 0.8.4", "jsonrpsee", "logging", diff --git a/Cargo.toml b/Cargo.toml index 31bb03d5e..76307f276 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -162,6 +162,7 @@ enum-iterator = "2.0" enumflags2 = "0.7" expect-test = "1.3" fallible-iterator = "0.3" +file-rotate = "0.7" fix-hidden-lifetime-bug = "0.2" fixed-hash = "0.8" flate2 = "1.0" diff --git a/node-lib/Cargo.toml b/node-lib/Cargo.toml index 5b455f1b5..3c32847be 100644 --- a/node-lib/Cargo.toml +++ b/node-lib/Cargo.toml @@ -24,6 +24,7 @@ utils-networking = { path = "../utils/networking" } anyhow.workspace = true clap = { workspace = true, features = ["derive"] } +file-rotate.workspace = true jsonrpsee = { workspace = true, features = ["macros"] } tokio = { workspace = true, default-features = false } serde = { workspace = true, features = ["derive"] } diff --git a/node-lib/src/runner.rs b/node-lib/src/runner.rs index 47160133c..5e4e9578a 100644 --- a/node-lib/src/runner.rs +++ b/node-lib/src/runner.rs @@ -21,6 +21,8 @@ use std::{ sync::Arc, }; +use file_rotate::{compression::Compression, suffix::AppendCount, ContentLimit, FileRotate}; + use anyhow::{anyhow, Context, Result}; use blockprod::rpc::BlockProductionRpcServer; use chainstate_launcher::{ChainConfig, StorageBackendConfig}; @@ -264,8 +266,14 @@ pub async fn setup(options: Options) -> Result { // Init logging if run_options.log_to_file.is_some_and(|log_to_file| log_to_file) { - let log_file = std::fs::File::create(data_dir.join(LOG_FILE_NAME)) - .map_err(|e| anyhow!("Cannot create log file in {data_dir:?}: {e}"))?; + let log_file = FileRotate::new( + data_dir.join(format!("logs/{}", LOG_FILE_NAME)), + AppendCount::new(2), // total 3 file + ContentLimit::Bytes(100_000_000), // 100MB each + Compression::None, + #[cfg(unix)] + None, + ); logging::init_logging_to(log_file, false); } else { logging::init_logging();