Skip to content

Commit 3617e61

Browse files
authored
feat: refactor log formats (#281)
1 parent fff4e8a commit 3617e61

File tree

27 files changed

+297
-223
lines changed

27 files changed

+297
-223
lines changed

Cargo.lock

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

bin/pbs.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use cb_common::{
2-
config::load_pbs_config,
3-
utils::{initialize_pbs_tracing_log, wait_for_signal},
2+
config::{load_pbs_config, LogsSettings, PBS_MODULE_NAME},
3+
utils::{initialize_tracing_log, wait_for_signal},
44
};
55
use cb_pbs::{DefaultBuilderApi, PbsService, PbsState};
66
use clap::Parser;
@@ -15,7 +15,7 @@ async fn main() -> Result<()> {
1515
if std::env::var_os("RUST_BACKTRACE").is_none() {
1616
std::env::set_var("RUST_BACKTRACE", "1");
1717
}
18-
let _guard = initialize_pbs_tracing_log();
18+
let _guard = initialize_tracing_log(PBS_MODULE_NAME, LogsSettings::from_env_config()?);
1919

2020
let _args = cb_cli::PbsArgs::parse();
2121

@@ -29,6 +29,7 @@ async fn main() -> Result<()> {
2929
maybe_err = server => {
3030
if let Err(err) = maybe_err {
3131
error!(%err, "PBS service unexpectedly stopped");
32+
eprintln!("PBS service unexpectedly stopped: {}", err);
3233
}
3334
},
3435
_ = wait_for_signal() => {

bin/signer.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use cb_common::{
2-
config::{StartSignerConfig, SIGNER_MODULE_NAME},
2+
config::{LogsSettings, StartSignerConfig, SIGNER_MODULE_NAME},
33
utils::{initialize_tracing_log, wait_for_signal},
44
};
55
use cb_signer::service::SigningService;
@@ -15,7 +15,7 @@ async fn main() -> Result<()> {
1515
if std::env::var_os("RUST_BACKTRACE").is_none() {
1616
std::env::set_var("RUST_BACKTRACE", "1");
1717
}
18-
let _guard = initialize_tracing_log(SIGNER_MODULE_NAME);
18+
let _guard = initialize_tracing_log(SIGNER_MODULE_NAME, LogsSettings::from_env_config()?);
1919

2020
let _args = cb_cli::SignerArgs::parse();
2121

@@ -26,6 +26,7 @@ async fn main() -> Result<()> {
2626
maybe_err = server => {
2727
if let Err(err) = maybe_err {
2828
error!(%err, "signing server unexpectedly stopped");
29+
eprintln!("signing server unexpectedly stopped: {}", err);
2930
}
3031
},
3132
_ = wait_for_signal() => {

bin/src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@ pub mod prelude {
77
},
88
config::{
99
load_builder_module_config, load_commit_module_config, load_pbs_config,
10-
load_pbs_custom_config, LogsSettings, StartCommitModuleConfig,
10+
load_pbs_custom_config, LogsSettings, StartCommitModuleConfig, PBS_MODULE_NAME,
1111
},
1212
pbs::{BuilderEvent, BuilderEventClient, OnBuilderApiEvent},
1313
signer::{BlsPublicKey, BlsSignature, EcdsaPublicKey, EcdsaSignature},
1414
types::Chain,
15-
utils::{
16-
initialize_pbs_tracing_log, initialize_tracing_log, utcnow_ms, utcnow_ns, utcnow_sec,
17-
utcnow_us,
18-
},
15+
utils::{initialize_tracing_log, utcnow_ms, utcnow_ns, utcnow_sec, utcnow_us},
1916
};
2017
pub use cb_metrics::provider::MetricsProvider;
2118
pub use cb_pbs::{

config.example.toml

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,37 @@ host = "127.0.0.1"
251251
# OPTIONAL, DEFAULT: 10000
252252
start_port = 10000
253253

254-
# Configuration for how logs should be collected and stored
255-
# OPTIONAL, info to stdout if missing
256-
[logs]
254+
# Configuration stdout logs
255+
# OPTIONAL, DEFAULT: enabled
256+
[logs.stdout]
257+
# Whether to enable stdout logging
258+
# OPTIONAL, DEFAULT: true
259+
enabled = true
260+
# Log level. Supported values: trace, debug, info, warn, error
261+
# OPTIONAL, DEFAULT: info
262+
level = "info"
263+
# Log in JSON format
264+
# OPTIONAL, DEFAULT: false
265+
use_json = false
266+
# Whether to enable ANSI color codes
267+
# OPTIONAL, DEFAULT: true
268+
color = true
269+
270+
# Configuration file logs
271+
# OPTIONAL, DEFAULT: disabled
272+
[logs.file]
273+
# Whether to enable file logging
274+
# OPTIONAL, DEFAULT: false
275+
enabled = true
276+
# Log level. Supported values: trace, debug, info, warn, error
277+
# OPTIONAL, DEFAULT: info
278+
level = "debug"
279+
# Log in JSON format
280+
# OPTIONAL, DEFAULT: true
281+
use_json = true
257282
# Path to the log directory
258283
# OPTIONAL, DEFAULT: /var/logs/commit-boost
259-
log_dir_path = "./logs"
260-
# Log level. Supported values: trace, debug, info, warn, error
261-
# OPTIONAL, DEFAULT: debug to file, info to stdout
262-
log_level = "debug"
284+
dir_path = "./logs"
263285
# Maximum number of log files to keep
264286
# OPTIONAL
265-
max_log_files = 30
287+
max_files = 30

crates/cli/src/docker_init.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub async fn handle_docker_init(config_path: PathBuf, output_dir: PathBuf) -> Re
4646

4747
let chain_spec_path = CommitBoostConfig::chain_spec_file(&config_path);
4848

49-
let log_to_file = cb_config.logs.is_some();
49+
let log_to_file = cb_config.logs.file.enabled;
5050
let mut metrics_port = cb_config.metrics.as_ref().map(|m| m.start_port).unwrap_or_default();
5151

5252
let mut services = IndexMap::new();
@@ -584,8 +584,8 @@ pub async fn handle_docker_init(config_path: PathBuf, output_dir: PathBuf) -> Re
584584
println!()
585585
}
586586
// if file logging is enabled, warn about permissions
587-
if let Some(logs_config) = cb_config.logs {
588-
let log_dir = logs_config.log_dir_path;
587+
if cb_config.logs.file.enabled {
588+
let log_dir = cb_config.logs.file.dir_path;
589589
println!(
590590
"Warning: file logging is enabled, you may need to update permissions for the logs directory. e.g. with:\n\t`sudo chown -R 10001:10001 {}`",
591591
log_dir.display()
@@ -654,9 +654,9 @@ fn get_env_uval(k: &str, v: u64) -> (String, Option<SingleValue>) {
654654
// (k.into(), Some(SingleValue::Bool(v)))
655655
// }
656656

657-
fn get_log_volume(maybe_config: &Option<LogsSettings>, module_id: &str) -> Option<Volumes> {
658-
maybe_config.as_ref().map(|config| {
659-
let p = config.log_dir_path.join(module_id.to_lowercase());
657+
fn get_log_volume(config: &LogsSettings, module_id: &str) -> Option<Volumes> {
658+
config.file.enabled.then_some({
659+
let p = config.file.dir_path.join(module_id.to_lowercase());
660660
Volumes::Simple(format!(
661661
"{}:{}",
662662
p.to_str().expect("could not convert pathbuf to str"),

crates/common/src/config/log.rs

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,21 @@ use eyre::Result;
44
use serde::{Deserialize, Serialize};
55

66
use super::{load_optional_env_var, CommitBoostConfig, LOGS_DIR_DEFAULT, LOGS_DIR_ENV};
7+
use crate::utils::default_bool;
78

8-
#[derive(Clone, Debug, Deserialize, Serialize)]
9+
#[derive(Clone, Default, Debug, Deserialize, Serialize)]
910
pub struct LogsSettings {
10-
#[serde(default = "default_log_dir_path")]
11-
pub log_dir_path: PathBuf,
12-
#[serde(default = "default_log_level")]
13-
pub log_level: String,
14-
#[serde(default)]
15-
pub max_log_files: Option<usize>,
16-
}
17-
18-
impl Default for LogsSettings {
19-
fn default() -> Self {
20-
LogsSettings {
21-
log_dir_path: default_log_dir_path(),
22-
log_level: default_log_level(),
23-
max_log_files: None,
24-
}
25-
}
11+
pub stdout: StdoutLogSettings,
12+
pub file: FileLogSettings,
2613
}
2714

2815
impl LogsSettings {
29-
pub fn from_env_config() -> Result<Option<Self>> {
16+
pub fn from_env_config() -> Result<Self> {
3017
let mut config = CommitBoostConfig::from_env_path()?;
3118

3219
// Override log dir path if env var is set
33-
if let Some(log_config) = config.logs.as_mut() {
34-
if let Some(log_dir) = load_optional_env_var(LOGS_DIR_ENV) {
35-
log_config.log_dir_path = log_dir.into();
36-
}
20+
if let Some(log_dir) = load_optional_env_var(LOGS_DIR_ENV) {
21+
config.logs.file.dir_path = log_dir.into();
3722
}
3823

3924
Ok(config.logs)
@@ -44,6 +29,50 @@ fn default_log_dir_path() -> PathBuf {
4429
LOGS_DIR_DEFAULT.into()
4530
}
4631

47-
pub fn default_log_level() -> String {
32+
fn default_level() -> String {
4833
"info".into()
4934
}
35+
36+
#[derive(Clone, Debug, Deserialize, Serialize)]
37+
pub struct StdoutLogSettings {
38+
#[serde(default = "default_bool::<true>")]
39+
pub enabled: bool,
40+
#[serde(default = "default_level")]
41+
pub level: String,
42+
#[serde(default = "default_bool::<false>")]
43+
pub use_json: bool,
44+
#[serde(default = "default_bool::<true>")]
45+
pub color: bool,
46+
}
47+
48+
impl Default for StdoutLogSettings {
49+
fn default() -> Self {
50+
Self { enabled: true, level: "info".into(), use_json: false, color: true }
51+
}
52+
}
53+
54+
#[derive(Clone, Debug, Deserialize, Serialize)]
55+
pub struct FileLogSettings {
56+
#[serde(default = "default_bool::<false>")]
57+
pub enabled: bool,
58+
#[serde(default = "default_level")]
59+
pub level: String,
60+
#[serde(default = "default_bool::<true>")]
61+
pub use_json: bool,
62+
#[serde(default = "default_log_dir_path")]
63+
pub dir_path: PathBuf,
64+
#[serde(default)]
65+
pub max_files: Option<usize>,
66+
}
67+
68+
impl Default for FileLogSettings {
69+
fn default() -> Self {
70+
Self {
71+
enabled: false,
72+
level: "info".into(),
73+
use_json: true,
74+
dir_path: default_log_dir_path(),
75+
max_files: None,
76+
}
77+
}
78+
}

crates/common/src/config/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ pub struct CommitBoostConfig {
3333
pub modules: Option<Vec<StaticModuleConfig>>,
3434
pub signer: Option<SignerConfig>,
3535
pub metrics: Option<MetricsConfig>,
36-
pub logs: Option<LogsSettings>,
36+
#[serde(default)]
37+
pub logs: LogsSettings,
3738
}
3839

3940
impl CommitBoostConfig {
@@ -117,5 +118,6 @@ struct HelperConfig {
117118
modules: Option<Vec<StaticModuleConfig>>,
118119
signer: Option<SignerConfig>,
119120
metrics: Option<MetricsConfig>,
120-
logs: Option<LogsSettings>,
121+
#[serde(default)]
122+
logs: LogsSettings,
121123
}

crates/common/src/pbs/types/beacon_block.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ impl SignedBlindedBeaconBlock {
2525
}
2626
}
2727

28+
pub fn block_number(&self) -> u64 {
29+
match &self.message {
30+
BlindedBeaconBlock::Electra(b) => b.body.execution_payload_header.block_number,
31+
BlindedBeaconBlock::Deneb(b) => b.body.execution_payload_header.block_number,
32+
}
33+
}
34+
35+
pub fn parent_hash(&self) -> B256 {
36+
match &self.message {
37+
BlindedBeaconBlock::Electra(b) => b.body.execution_payload_header.parent_hash,
38+
BlindedBeaconBlock::Deneb(b) => b.body.execution_payload_header.parent_hash,
39+
}
40+
}
41+
2842
pub fn slot(&self) -> u64 {
2943
match &self.message {
3044
BlindedBeaconBlock::Electra(b) => b.slot,

0 commit comments

Comments
 (0)