From 752da7c25e643f532095d06cc123cc9c599e6a66 Mon Sep 17 00:00:00 2001 From: Heorhii Azarov Date: Fri, 20 Dec 2024 13:14:33 +0200 Subject: [PATCH] Add logs rotation --- Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + node-lib/Cargo.toml | 1 + node-lib/src/runner.rs | 12 ++++++++++-- 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a01a47db28..9cbea26955 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 31bb03d5e8..76307f276a 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 5b455f1b51..3c32847be1 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 47160133c8..5e4e9578a9 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();