Skip to content

Commit

Permalink
feat: store logs in different folders based on the chain (paradigmxyz…
Browse files Browse the repository at this point in the history
…#3948)

Co-authored-by: Bjerg <[email protected]>
Co-authored-by: Oliver Nordbjerg <[email protected]>
  • Loading branch information
3 people authored Jul 31, 2023
1 parent 0b913e2 commit 5823255
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
44 changes: 43 additions & 1 deletion bin/reth/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! CLI definition and entrypoint to executable
use crate::{
args::utils::genesis_value_parser,
chain, config, db, debug_cmd,
dirs::{LogsDir, PlatformPath},
node, p2p,
Expand All @@ -8,11 +9,13 @@ use crate::{
version::{LONG_VERSION, SHORT_VERSION},
};
use clap::{ArgAction, Args, Parser, Subcommand};
use reth_primitives::ChainSpec;
use reth_tracing::{
tracing::{metadata::LevelFilter, Level, Subscriber},
tracing_subscriber::{filter::Directive, registry::LookupSpan, EnvFilter},
BoxedLayer, FileWorkerGuard,
};
use std::sync::Arc;

/// The main reth cli interface.
///
Expand All @@ -24,6 +27,25 @@ pub struct Cli {
#[clap(subcommand)]
command: Commands,

/// The chain this node is running.
///
/// Possible values are either a built-in chain or the path to a chain specification file.
///
/// Built-in chains:
/// - mainnet
/// - goerli
/// - sepolia
#[arg(
long,
value_name = "CHAIN_OR_PATH",
global = true,
verbatim_doc_comment,
default_value = "mainnet",
value_parser = genesis_value_parser,
global = true,
)]
chain: Arc<ChainSpec>,

#[clap(flatten)]
logs: Logs,

Expand All @@ -33,7 +55,10 @@ pub struct Cli {

impl Cli {
/// Execute the configured cli command.
pub fn run(self) -> eyre::Result<()> {
pub fn run(mut self) -> eyre::Result<()> {
// add network name to logs dir
self.logs.log_directory = self.logs.log_directory.join(self.chain.chain.to_string());

let _guard = self.init_tracing()?;

let runner = CliRunner::default();
Expand Down Expand Up @@ -213,4 +238,21 @@ mod tests {
assert_eq!(err.kind(), clap::error::ErrorKind::DisplayHelp);
}
}

/// Tests that the log directory is parsed correctly. It's always tied to the specific chain's
/// name
#[test]
fn parse_logs_path() {
let mut reth = Cli::try_parse_from(["reth", "node", "--log.persistent"]).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();
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);
}
}
5 changes: 5 additions & 0 deletions bin/reth/src/dirs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ impl<D> PlatformPath<D> {
let platform_path = PlatformPath::<D>(path, std::marker::PhantomData);
ChainPath::new(platform_path, chain)
}

/// Map the inner path to a new type `T`.
pub fn map_to<T>(&self) -> PlatformPath<T> {
PlatformPath(self.0.clone(), std::marker::PhantomData)
}
}

/// An Optional wrapper type around [PlatformPath].
Expand Down

0 comments on commit 5823255

Please sign in to comment.