Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: store logs in different folders based on the chain #3948

Merged
merged 6 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading