Skip to content

Commit 822b3de

Browse files
committed
CLI tests for logging flags (#3609)
## Issue Addressed Adding CLI tests for logging flags: log-color and disable-log-timestamp Which issue # does this PR address? #3588 ## Proposed Changes Add CLI tests for logging flags as described in #3588 Please list or describe the changes introduced by this PR. Added logger_config to client::Config as suggested. Implemented Default for LoggerConfig based on what was being done elsewhere in the repo. Created 2 tests for each flag addressed. ## Additional Info Please provide any additional information. For example, future considerations or information useful for reviewers.
1 parent 422263f commit 822b3de

File tree

10 files changed

+85
-31
lines changed

10 files changed

+85
-31
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

beacon_node/client/src/config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use directory::DEFAULT_ROOT_DIR;
2+
use environment::LoggerConfig;
23
use network::NetworkConfig;
34
use sensitive_url::SensitiveUrl;
45
use serde_derive::{Deserialize, Serialize};
56
use std::fs;
67
use std::path::PathBuf;
78
use types::{Graffiti, PublicKeyBytes};
8-
99
/// Default directory name for the freezer database under the top-level data dir.
1010
const DEFAULT_FREEZER_DB_DIR: &str = "freezer_db";
1111

@@ -72,6 +72,7 @@ pub struct Config {
7272
pub http_metrics: http_metrics::Config,
7373
pub monitoring_api: Option<monitoring_api::Config>,
7474
pub slasher: Option<slasher::Config>,
75+
pub logger_config: LoggerConfig,
7576
}
7677

7778
impl Default for Config {
@@ -96,6 +97,7 @@ impl Default for Config {
9697
slasher: None,
9798
validator_monitor_auto: false,
9899
validator_monitor_pubkeys: vec![],
100+
logger_config: LoggerConfig::default(),
99101
}
100102
}
101103
}

lcli/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -781,8 +781,8 @@ fn run<T: EthSpec>(
781781
.map_err(|e| format!("should start tokio runtime: {:?}", e))?
782782
.initialize_logger(LoggerConfig {
783783
path: None,
784-
debug_level: "trace",
785-
logfile_debug_level: "trace",
784+
debug_level: String::from("trace"),
785+
logfile_debug_level: String::from("trace"),
786786
log_format: None,
787787
log_color: false,
788788
disable_log_timestamp: false,

lighthouse/environment/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ slog-async = "2.5.0"
1818
futures = "0.3.7"
1919
slog-json = "2.3.0"
2020
exit-future = "0.2.0"
21+
serde = "1.0.116"
22+
serde_derive = "1.0.116"
2123

2224
[target.'cfg(not(target_family = "unix"))'.dependencies]
2325
ctrlc = { version = "3.1.6", features = ["termination"] }

lighthouse/environment/src/lib.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use eth2_network_config::Eth2NetworkConfig;
1212
use futures::channel::mpsc::{channel, Receiver, Sender};
1313
use futures::{future, StreamExt};
1414

15+
use serde_derive::{Deserialize, Serialize};
1516
use slog::{error, info, o, warn, Drain, Duplicate, Level, Logger};
1617
use sloggers::{file::FileLoggerBuilder, types::Format, types::Severity, Build};
1718
use std::fs::create_dir_all;
@@ -43,17 +44,33 @@ const MAXIMUM_SHUTDOWN_TIME: u64 = 15;
4344
/// - `path` == None,
4445
/// - `max_log_size` == 0,
4546
/// - `max_log_number` == 0,
46-
pub struct LoggerConfig<'a> {
47+
#[derive(Debug, Clone, Serialize, Deserialize)]
48+
pub struct LoggerConfig {
4749
pub path: Option<PathBuf>,
48-
pub debug_level: &'a str,
49-
pub logfile_debug_level: &'a str,
50-
pub log_format: Option<&'a str>,
50+
pub debug_level: String,
51+
pub logfile_debug_level: String,
52+
pub log_format: Option<String>,
5153
pub log_color: bool,
5254
pub disable_log_timestamp: bool,
5355
pub max_log_size: u64,
5456
pub max_log_number: usize,
5557
pub compression: bool,
5658
}
59+
impl Default for LoggerConfig {
60+
fn default() -> Self {
61+
LoggerConfig {
62+
path: None,
63+
debug_level: String::from("info"),
64+
logfile_debug_level: String::from("debug"),
65+
log_format: None,
66+
log_color: false,
67+
disable_log_timestamp: false,
68+
max_log_size: 200,
69+
max_log_number: 5,
70+
compression: false,
71+
}
72+
}
73+
}
5774

5875
/// Builds an `Environment`.
5976
pub struct EnvironmentBuilder<E: EthSpec> {
@@ -135,7 +152,7 @@ impl<E: EthSpec> EnvironmentBuilder<E> {
135152
/// Note that background file logging will spawn a new thread.
136153
pub fn initialize_logger(mut self, config: LoggerConfig) -> Result<Self, String> {
137154
// Setting up the initial logger format and build it.
138-
let stdout_drain = if let Some(format) = config.log_format {
155+
let stdout_drain = if let Some(ref format) = config.log_format {
139156
match format.to_uppercase().as_str() {
140157
"JSON" => {
141158
let stdout_drain = slog_json::Json::default(std::io::stdout()).fuse();
@@ -168,7 +185,7 @@ impl<E: EthSpec> EnvironmentBuilder<E> {
168185
.build()
169186
};
170187

171-
let stdout_drain = match config.debug_level {
188+
let stdout_drain = match config.debug_level.as_str() {
172189
"info" => stdout_drain.filter_level(Level::Info),
173190
"debug" => stdout_drain.filter_level(Level::Debug),
174191
"trace" => stdout_drain.filter_level(Level::Trace),
@@ -220,7 +237,7 @@ impl<E: EthSpec> EnvironmentBuilder<E> {
220237
}
221238
}
222239

223-
let logfile_level = match config.logfile_debug_level {
240+
let logfile_level = match config.logfile_debug_level.as_str() {
224241
"info" => Severity::Info,
225242
"debug" => Severity::Debug,
226243
"trace" => Severity::Trace,
@@ -233,7 +250,7 @@ impl<E: EthSpec> EnvironmentBuilder<E> {
233250
let file_logger = FileLoggerBuilder::new(&path)
234251
.level(logfile_level)
235252
.channel_size(LOG_CHANNEL_SIZE)
236-
.format(match config.log_format {
253+
.format(match config.log_format.as_deref() {
237254
Some("JSON") => Format::Json,
238255
_ => Format::default(),
239256
})

lighthouse/src/main.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -438,17 +438,17 @@ fn run<E: EthSpec>(
438438

439439
let logger_config = LoggerConfig {
440440
path: log_path,
441-
debug_level,
442-
logfile_debug_level,
443-
log_format,
441+
debug_level: String::from(debug_level),
442+
logfile_debug_level: String::from(logfile_debug_level),
443+
log_format: log_format.map(String::from),
444444
log_color,
445445
disable_log_timestamp,
446446
max_log_size: logfile_max_size * 1_024 * 1_024,
447447
max_log_number: logfile_max_number,
448448
compression: logfile_compress,
449449
};
450450

451-
let builder = environment_builder.initialize_logger(logger_config)?;
451+
let builder = environment_builder.initialize_logger(logger_config.clone())?;
452452

453453
let mut environment = builder
454454
.multi_threaded_tokio_runtime()?
@@ -528,7 +528,8 @@ fn run<E: EthSpec>(
528528
let context = environment.core_context();
529529
let log = context.log().clone();
530530
let executor = context.executor.clone();
531-
let config = beacon_node::get_config::<E>(matches, &context)?;
531+
let mut config = beacon_node::get_config::<E>(matches, &context)?;
532+
config.logger_config = logger_config;
532533
let shutdown_flag = matches.is_present("immediate-shutdown");
533534
// Dump configs if `dump-config` or `dump-chain-config` flags are set
534535
clap_utils::check_dump_configs::<_, E>(matches, &config, &context.eth2_config.spec)?;

lighthouse/tests/beacon_node.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,3 +1475,39 @@ fn monitoring_endpoint() {
14751475
assert_eq!(api_conf.update_period_secs, Some(30));
14761476
});
14771477
}
1478+
1479+
// Tests for Logger flags.
1480+
#[test]
1481+
fn default_log_color_flag() {
1482+
CommandLineTest::new()
1483+
.run_with_zero_port()
1484+
.with_config(|config| {
1485+
assert!(!config.logger_config.log_color);
1486+
});
1487+
}
1488+
#[test]
1489+
fn enabled_log_color_flag() {
1490+
CommandLineTest::new()
1491+
.flag("log-color", None)
1492+
.run_with_zero_port()
1493+
.with_config(|config| {
1494+
assert!(config.logger_config.log_color);
1495+
});
1496+
}
1497+
#[test]
1498+
fn default_disable_log_timestamp_flag() {
1499+
CommandLineTest::new()
1500+
.run_with_zero_port()
1501+
.with_config(|config| {
1502+
assert!(!config.logger_config.disable_log_timestamp);
1503+
});
1504+
}
1505+
#[test]
1506+
fn enabled_disable_log_timestamp_flag() {
1507+
CommandLineTest::new()
1508+
.flag("disable-log-timestamp", None)
1509+
.run_with_zero_port()
1510+
.with_config(|config| {
1511+
assert!(config.logger_config.disable_log_timestamp);
1512+
});
1513+
}

testing/simulator/src/eth1_sim.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,12 @@ pub fn run_eth1_sim(matches: &ArgMatches) -> Result<(), String> {
5656
})
5757
.collect::<Vec<_>>();
5858

59-
let log_level = "debug";
60-
let log_format = None;
61-
6259
let mut env = EnvironmentBuilder::minimal()
6360
.initialize_logger(LoggerConfig {
6461
path: None,
65-
debug_level: log_level,
66-
logfile_debug_level: "debug",
67-
log_format,
62+
debug_level: String::from("debug"),
63+
logfile_debug_level: String::from("debug"),
64+
log_format: None,
6865
log_color: false,
6966
disable_log_timestamp: false,
7067
max_log_size: 0,

testing/simulator/src/no_eth1_sim.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,12 @@ pub fn run_no_eth1_sim(matches: &ArgMatches) -> Result<(), String> {
4141
})
4242
.collect::<Vec<_>>();
4343

44-
let log_level = "debug";
45-
let log_format = None;
46-
4744
let mut env = EnvironmentBuilder::mainnet()
4845
.initialize_logger(LoggerConfig {
4946
path: None,
50-
debug_level: log_level,
51-
logfile_debug_level: "debug",
52-
log_format,
47+
debug_level: String::from("debug"),
48+
logfile_debug_level: String::from("debug"),
49+
log_format: None,
5350
log_color: false,
5451
disable_log_timestamp: false,
5552
max_log_size: 0,

testing/simulator/src/sync_sim.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ fn syncing_sim(
4848
let mut env = EnvironmentBuilder::minimal()
4949
.initialize_logger(LoggerConfig {
5050
path: None,
51-
debug_level: log_level,
52-
logfile_debug_level: "debug",
53-
log_format,
51+
debug_level: String::from(log_level),
52+
logfile_debug_level: String::from("debug"),
53+
log_format: log_format.map(String::from),
5454
log_color: false,
5555
disable_log_timestamp: false,
5656
max_log_size: 0,

0 commit comments

Comments
 (0)