Skip to content

Commit

Permalink
feat: enable size-limited file logs by default (paradigmxyz#4192)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
onbjerg and mattsse authored Sep 18, 2023
1 parent aeb37aa commit 6658920
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 22 additions & 10 deletions bin/reth/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,6 @@ pub enum Commands<Ext: RethCliExt = ()> {
#[derive(Debug, Args)]
#[command(next_help_heading = "Logging")]
pub struct Logs {
/// The flag to enable persistent logs.
#[arg(long = "log.persistent", global = true, conflicts_with = "journald")]
persistent: bool,

/// The path to put log files in.
#[arg(
long = "log.directory",
Expand All @@ -172,6 +168,15 @@ pub struct Logs {
)]
log_directory: PlatformPath<LogsDir>,

/// The maximum size (in MB) of log files.
#[arg(long = "log.max-size", value_name = "SIZE", global = true, default_value_t = 200)]
log_max_size: u64,

/// The maximum amount of log files that will be stored. If set to 0, background file logging
/// is disabled.
#[arg(long = "log.max-files", value_name = "COUNT", global = true, default_value_t = 5)]
log_max_files: usize,

/// Log events to journald.
#[arg(long = "log.journald", global = true, conflicts_with = "log_directory")]
journald: bool,
Expand All @@ -191,6 +196,9 @@ pub struct Logs {
color: ColorMode,
}

/// Constant to convert megabytes to bytes
const MB_TO_BYTES: u64 = 1024 * 1024;

impl Logs {
/// Builds a tracing layer from the current log options.
pub fn layer<S>(&self) -> eyre::Result<Option<(BoxedLayer<S>, Option<FileWorkerGuard>)>>
Expand All @@ -202,8 +210,14 @@ impl Logs {

if self.journald {
Ok(Some((reth_tracing::journald(filter).expect("Could not connect to journald"), None)))
} else if self.persistent {
let (layer, guard) = reth_tracing::file(filter, &self.log_directory, "reth.log");
} else if self.log_max_files > 0 {
let (layer, guard) = reth_tracing::file(
filter,
&self.log_directory,
"reth.log",
self.log_max_size * MB_TO_BYTES,
self.log_max_files,
);
Ok(Some((layer, Some(guard))))
} else {
Ok(None)
Expand Down Expand Up @@ -305,14 +319,12 @@ mod tests {
/// name
#[test]
fn parse_logs_path() {
let mut reth = Cli::<()>::try_parse_from(["reth", "node", "--log.persistent"]).unwrap();
let mut reth = Cli::<()>::try_parse_from(["reth", "node"]).unwrap();
reth.logs.log_directory = reth.logs.log_directory.join(reth.chain.chain.to_string());
let log_dir = reth.logs.log_directory;
assert!(log_dir.as_ref().ends_with("reth/logs/mainnet"), "{:?}", log_dir);

let mut reth =
Cli::<()>::try_parse_from(["reth", "node", "--chain", "sepolia", "--log.persistent"])
.unwrap();
let mut reth = Cli::<()>::try_parse_from(["reth", "node", "--chain", "sepolia"]).unwrap();
reth.logs.log_directory = reth.logs.log_directory.join(reth.chain.chain.to_string());
let log_dir = reth.logs.log_directory;
assert!(log_dir.as_ref().ends_with("reth/logs/sepolia"), "{:?}", log_dir);
Expand Down
1 change: 1 addition & 0 deletions crates/tracing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ tracing.workspace = true
tracing-subscriber = { version = "0.3", default-features = false, features = ["env-filter", "fmt"] }
tracing-appender.workspace = true
tracing-journald = "0.3"
rolling-file = "0.2.0"
20 changes: 18 additions & 2 deletions crates/tracing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
//! - [`journald()`]
//!
//! As well as a simple way to initialize a subscriber: [`init`].
use rolling_file::{RollingConditionBasic, RollingFileAppender};
use std::path::Path;
use tracing::Subscriber;
use tracing_subscriber::{
Expand Down Expand Up @@ -74,13 +75,28 @@ pub fn file<S>(
filter: EnvFilter,
dir: impl AsRef<Path>,
file_name: impl AsRef<Path>,
max_size_bytes: u64,
max_files: usize,
) -> (BoxedLayer<S>, tracing_appender::non_blocking::WorkerGuard)
where
S: Subscriber,
for<'a> S: LookupSpan<'a>,
{
let (writer, guard) =
tracing_appender::non_blocking(tracing_appender::rolling::never(dir, file_name));
// Create log dir if it doesn't exist (RFA doesn't do this for us)
let log_dir = dir.as_ref();
if !log_dir.exists() {
std::fs::create_dir_all(log_dir).expect("Could not create log directory");
}

// Create layer
let (writer, guard) = tracing_appender::non_blocking(
RollingFileAppender::new(
log_dir.join(file_name.as_ref()),
RollingConditionBasic::new().max_size(max_size_bytes),
max_files,
)
.expect("Could not initialize file logging"),
);
let layer = tracing_subscriber::fmt::layer()
.with_ansi(false)
.with_writer(writer)
Expand Down

0 comments on commit 6658920

Please sign in to comment.