diff --git a/Cargo.lock b/Cargo.lock index 8fb8c5492..da07bf425 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1631,6 +1631,8 @@ dependencies = [ "exit-future", "futures", "logging", + "serde", + "serde_derive", "slog", "slog-async", "slog-json", diff --git a/beacon_node/client/src/config.rs b/beacon_node/client/src/config.rs index a5d5b37c7..5e43c1eaa 100644 --- a/beacon_node/client/src/config.rs +++ b/beacon_node/client/src/config.rs @@ -1,11 +1,11 @@ use directory::DEFAULT_ROOT_DIR; +use environment::LoggerConfig; use network::NetworkConfig; use sensitive_url::SensitiveUrl; use serde_derive::{Deserialize, Serialize}; use std::fs; use std::path::PathBuf; use types::{Graffiti, PublicKeyBytes}; - /// Default directory name for the freezer database under the top-level data dir. const DEFAULT_FREEZER_DB_DIR: &str = "freezer_db"; @@ -72,6 +72,7 @@ pub struct Config { pub http_metrics: http_metrics::Config, pub monitoring_api: Option, pub slasher: Option, + pub logger_config: LoggerConfig, } impl Default for Config { @@ -96,6 +97,7 @@ impl Default for Config { slasher: None, validator_monitor_auto: false, validator_monitor_pubkeys: vec![], + logger_config: LoggerConfig::default(), } } } diff --git a/lcli/src/main.rs b/lcli/src/main.rs index 11a23fe0b..8b233d847 100644 --- a/lcli/src/main.rs +++ b/lcli/src/main.rs @@ -781,8 +781,8 @@ fn run( .map_err(|e| format!("should start tokio runtime: {:?}", e))? .initialize_logger(LoggerConfig { path: None, - debug_level: "trace", - logfile_debug_level: "trace", + debug_level: String::from("trace"), + logfile_debug_level: String::from("trace"), log_format: None, log_color: false, disable_log_timestamp: false, diff --git a/lighthouse/environment/Cargo.toml b/lighthouse/environment/Cargo.toml index 7dc31e06b..1ba0bb267 100644 --- a/lighthouse/environment/Cargo.toml +++ b/lighthouse/environment/Cargo.toml @@ -18,6 +18,8 @@ slog-async = "2.5.0" futures = "0.3.7" slog-json = "2.3.0" exit-future = "0.2.0" +serde = "1.0.116" +serde_derive = "1.0.116" [target.'cfg(not(target_family = "unix"))'.dependencies] ctrlc = { version = "3.1.6", features = ["termination"] } diff --git a/lighthouse/environment/src/lib.rs b/lighthouse/environment/src/lib.rs index 46348e63b..49163b96f 100644 --- a/lighthouse/environment/src/lib.rs +++ b/lighthouse/environment/src/lib.rs @@ -12,6 +12,7 @@ use eth2_network_config::Eth2NetworkConfig; use futures::channel::mpsc::{channel, Receiver, Sender}; use futures::{future, StreamExt}; +use serde_derive::{Deserialize, Serialize}; use slog::{error, info, o, warn, Drain, Duplicate, Level, Logger}; use sloggers::{file::FileLoggerBuilder, types::Format, types::Severity, Build}; use std::fs::create_dir_all; @@ -43,17 +44,33 @@ const MAXIMUM_SHUTDOWN_TIME: u64 = 15; /// - `path` == None, /// - `max_log_size` == 0, /// - `max_log_number` == 0, -pub struct LoggerConfig<'a> { +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct LoggerConfig { pub path: Option, - pub debug_level: &'a str, - pub logfile_debug_level: &'a str, - pub log_format: Option<&'a str>, + pub debug_level: String, + pub logfile_debug_level: String, + pub log_format: Option, pub log_color: bool, pub disable_log_timestamp: bool, pub max_log_size: u64, pub max_log_number: usize, pub compression: bool, } +impl Default for LoggerConfig { + fn default() -> Self { + LoggerConfig { + path: None, + debug_level: String::from("info"), + logfile_debug_level: String::from("debug"), + log_format: None, + log_color: false, + disable_log_timestamp: false, + max_log_size: 200, + max_log_number: 5, + compression: false, + } + } +} /// Builds an `Environment`. pub struct EnvironmentBuilder { @@ -135,7 +152,7 @@ impl EnvironmentBuilder { /// Note that background file logging will spawn a new thread. pub fn initialize_logger(mut self, config: LoggerConfig) -> Result { // Setting up the initial logger format and build it. - let stdout_drain = if let Some(format) = config.log_format { + let stdout_drain = if let Some(ref format) = config.log_format { match format.to_uppercase().as_str() { "JSON" => { let stdout_drain = slog_json::Json::default(std::io::stdout()).fuse(); @@ -168,7 +185,7 @@ impl EnvironmentBuilder { .build() }; - let stdout_drain = match config.debug_level { + let stdout_drain = match config.debug_level.as_str() { "info" => stdout_drain.filter_level(Level::Info), "debug" => stdout_drain.filter_level(Level::Debug), "trace" => stdout_drain.filter_level(Level::Trace), @@ -220,7 +237,7 @@ impl EnvironmentBuilder { } } - let logfile_level = match config.logfile_debug_level { + let logfile_level = match config.logfile_debug_level.as_str() { "info" => Severity::Info, "debug" => Severity::Debug, "trace" => Severity::Trace, @@ -233,7 +250,7 @@ impl EnvironmentBuilder { let file_logger = FileLoggerBuilder::new(&path) .level(logfile_level) .channel_size(LOG_CHANNEL_SIZE) - .format(match config.log_format { + .format(match config.log_format.as_deref() { Some("JSON") => Format::Json, _ => Format::default(), }) diff --git a/lighthouse/src/main.rs b/lighthouse/src/main.rs index 341e1a91d..9dc0902e0 100644 --- a/lighthouse/src/main.rs +++ b/lighthouse/src/main.rs @@ -438,9 +438,9 @@ fn run( let logger_config = LoggerConfig { path: log_path, - debug_level, - logfile_debug_level, - log_format, + debug_level: String::from(debug_level), + logfile_debug_level: String::from(logfile_debug_level), + log_format: log_format.map(String::from), log_color, disable_log_timestamp, max_log_size: logfile_max_size * 1_024 * 1_024, @@ -448,7 +448,7 @@ fn run( compression: logfile_compress, }; - let builder = environment_builder.initialize_logger(logger_config)?; + let builder = environment_builder.initialize_logger(logger_config.clone())?; let mut environment = builder .multi_threaded_tokio_runtime()? @@ -528,7 +528,8 @@ fn run( let context = environment.core_context(); let log = context.log().clone(); let executor = context.executor.clone(); - let config = beacon_node::get_config::(matches, &context)?; + let mut config = beacon_node::get_config::(matches, &context)?; + config.logger_config = logger_config; let shutdown_flag = matches.is_present("immediate-shutdown"); // Dump configs if `dump-config` or `dump-chain-config` flags are set clap_utils::check_dump_configs::<_, E>(matches, &config, &context.eth2_config.spec)?; diff --git a/lighthouse/tests/beacon_node.rs b/lighthouse/tests/beacon_node.rs index 288d18c1f..2e76d832c 100644 --- a/lighthouse/tests/beacon_node.rs +++ b/lighthouse/tests/beacon_node.rs @@ -1454,3 +1454,39 @@ fn monitoring_endpoint() { assert_eq!(api_conf.update_period_secs, Some(30)); }); } + +// Tests for Logger flags. +#[test] +fn default_log_color_flag() { + CommandLineTest::new() + .run_with_zero_port() + .with_config(|config| { + assert!(!config.logger_config.log_color); + }); +} +#[test] +fn enabled_log_color_flag() { + CommandLineTest::new() + .flag("log-color", None) + .run_with_zero_port() + .with_config(|config| { + assert!(config.logger_config.log_color); + }); +} +#[test] +fn default_disable_log_timestamp_flag() { + CommandLineTest::new() + .run_with_zero_port() + .with_config(|config| { + assert!(!config.logger_config.disable_log_timestamp); + }); +} +#[test] +fn enabled_disable_log_timestamp_flag() { + CommandLineTest::new() + .flag("disable-log-timestamp", None) + .run_with_zero_port() + .with_config(|config| { + assert!(config.logger_config.disable_log_timestamp); + }); +} diff --git a/testing/simulator/src/eth1_sim.rs b/testing/simulator/src/eth1_sim.rs index 182a66b49..3d59013f2 100644 --- a/testing/simulator/src/eth1_sim.rs +++ b/testing/simulator/src/eth1_sim.rs @@ -56,15 +56,12 @@ pub fn run_eth1_sim(matches: &ArgMatches) -> Result<(), String> { }) .collect::>(); - let log_level = "debug"; - let log_format = None; - let mut env = EnvironmentBuilder::minimal() .initialize_logger(LoggerConfig { path: None, - debug_level: log_level, - logfile_debug_level: "debug", - log_format, + debug_level: String::from("debug"), + logfile_debug_level: String::from("debug"), + log_format: None, log_color: false, disable_log_timestamp: false, max_log_size: 0, diff --git a/testing/simulator/src/no_eth1_sim.rs b/testing/simulator/src/no_eth1_sim.rs index 57e2e01eb..06f9e9a4f 100644 --- a/testing/simulator/src/no_eth1_sim.rs +++ b/testing/simulator/src/no_eth1_sim.rs @@ -41,15 +41,12 @@ pub fn run_no_eth1_sim(matches: &ArgMatches) -> Result<(), String> { }) .collect::>(); - let log_level = "debug"; - let log_format = None; - let mut env = EnvironmentBuilder::mainnet() .initialize_logger(LoggerConfig { path: None, - debug_level: log_level, - logfile_debug_level: "debug", - log_format, + debug_level: String::from("debug"), + logfile_debug_level: String::from("debug"), + log_format: None, log_color: false, disable_log_timestamp: false, max_log_size: 0, diff --git a/testing/simulator/src/sync_sim.rs b/testing/simulator/src/sync_sim.rs index af5ba95e0..00e439e4c 100644 --- a/testing/simulator/src/sync_sim.rs +++ b/testing/simulator/src/sync_sim.rs @@ -48,9 +48,9 @@ fn syncing_sim( let mut env = EnvironmentBuilder::minimal() .initialize_logger(LoggerConfig { path: None, - debug_level: log_level, - logfile_debug_level: "debug", - log_format, + debug_level: String::from(log_level), + logfile_debug_level: String::from("debug"), + log_format: log_format.map(String::from), log_color: false, disable_log_timestamp: false, max_log_size: 0,