Skip to content

Commit a3bbf33

Browse files
authored
Merge pull request #2013 from input-output-hk/djo/1981/enhance_logs_in_client
Enhance logs in client and client-cli
2 parents 7f3f4e5 + 99bf8c3 commit a3bbf33

23 files changed

+119
-88
lines changed

Cargo.lock

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

mithril-client-cli/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-client-cli"
3-
version = "0.9.16"
3+
version = "0.9.17"
44
description = "A Mithril Client"
55
authors = { workspace = true }
66
edition = { workspace = true }
@@ -44,7 +44,6 @@ slog = { version = "2.7.0", features = [
4444
] }
4545
slog-async = "2.8.0"
4646
slog-bunyan = "2.5.0"
47-
slog-scope = "4.4.0"
4847
slog-term = "2.9.1"
4948
thiserror = "1.0.64"
5049
tokio = { version = "1.40.0", features = ["full"] }

mithril-client-cli/src/commands/cardano_db/download.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anyhow::{anyhow, Context};
22
use chrono::Utc;
33
use clap::Parser;
4-
use slog_scope::{debug, warn};
4+
use slog::{debug, warn, Logger};
55
use std::{
66
collections::HashMap,
77
fs::File,
@@ -55,6 +55,7 @@ impl CardanoDbDownloadCommand {
5555
let params = context.config_parameters()?.add_source(self)?;
5656
let download_dir: &String = &params.require("download_dir")?;
5757
let db_dir = Path::new(download_dir).join("db");
58+
let logger = context.logger();
5859

5960
let progress_output_type = if self.is_json_output_enabled() {
6061
ProgressOutputType::JsonReporter
@@ -65,7 +66,9 @@ impl CardanoDbDownloadCommand {
6566
let client = client_builder(&params)?
6667
.add_feedback_receiver(Arc::new(IndicatifFeedbackReceiver::new(
6768
progress_output_type,
69+
logger.clone(),
6870
)))
71+
.with_logger(logger.clone())
6972
.build()?;
7073

7174
let get_list_of_artifact_ids = || async {
@@ -99,6 +102,7 @@ impl CardanoDbDownloadCommand {
99102
.await?;
100103

101104
Self::download_and_unpack_cardano_db(
105+
logger,
102106
3,
103107
&progress_printer,
104108
&client,
@@ -117,6 +121,7 @@ impl CardanoDbDownloadCommand {
117121
Self::compute_cardano_db_message(4, &progress_printer, &certificate, &db_dir).await?;
118122

119123
Self::verify_cardano_db_signature(
124+
logger,
120125
5,
121126
&progress_printer,
122127
&certificate,
@@ -181,6 +186,7 @@ impl CardanoDbDownloadCommand {
181186
}
182187

183188
async fn download_and_unpack_cardano_db(
189+
logger: &Logger,
184190
step_number: u16,
185191
progress_printer: &ProgressPrinter,
186192
client: &Client,
@@ -196,14 +202,17 @@ impl CardanoDbDownloadCommand {
196202
// The cardano db download does not fail if the statistic call fails.
197203
// It would be nice to implement tests to verify the behavior of `add_statistics`
198204
if let Err(e) = client.snapshot().add_statistics(cardano_db).await {
199-
warn!("Could not increment cardano db download statistics: {e:?}");
205+
warn!(
206+
logger, "Could not increment cardano db download statistics";
207+
"error" => ?e
208+
);
200209
}
201210

202211
// Append 'clean' file to speedup node bootstrap
203212
if let Err(error) = File::create(db_dir.join("clean")) {
204213
warn!(
205-
"Could not create clean shutdown marker file in directory {}: {error}",
206-
db_dir.display()
214+
logger, "Could not create clean shutdown marker file in directory '{}'", db_dir.display();
215+
"error" => error.to_string()
207216
);
208217
};
209218

@@ -233,6 +242,7 @@ impl CardanoDbDownloadCommand {
233242
}
234243

235244
async fn verify_cardano_db_signature(
245+
logger: &Logger,
236246
step_number: u16,
237247
progress_printer: &ProgressPrinter,
238248
certificate: &MithrilCertificate,
@@ -242,10 +252,16 @@ impl CardanoDbDownloadCommand {
242252
) -> MithrilResult<()> {
243253
progress_printer.report_step(step_number, "Verifying the cardano db signature…")?;
244254
if !certificate.match_message(message) {
245-
debug!("Digest verification failed, removing unpacked files & directory.");
255+
debug!(
256+
logger,
257+
"Digest verification failed, removing unpacked files & directory."
258+
);
246259

247260
if let Err(error) = std::fs::remove_dir_all(db_dir) {
248-
warn!("Error while removing unpacked files & directory: {error}.");
261+
warn!(
262+
logger, "Error while removing unpacked files & directory";
263+
"error" => error.to_string()
264+
);
249265
}
250266

251267
return Err(anyhow!(
@@ -389,6 +405,7 @@ mod tests {
389405
);
390406

391407
let result = CardanoDbDownloadCommand::verify_cardano_db_signature(
408+
&Logger::root(slog::Discard, slog::o!()),
392409
1,
393410
&progress_printer,
394411
&certificate,

mithril-client-cli/src/commands/cardano_db/list.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ impl CardanoDbListCommand {
2323
/// Main command execution
2424
pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
2525
let params = context.config_parameters()?;
26-
let client = client_builder_with_fallback_genesis_key(&params)?.build()?;
26+
let client = client_builder_with_fallback_genesis_key(&params)?
27+
.with_logger(context.logger().clone())
28+
.build()?;
2729
let items = client.snapshot().list().await?;
2830

2931
if self.is_json_output_enabled() {

mithril-client-cli/src/commands/cardano_db/show.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ impl CardanoDbShowCommand {
3030
/// Cardano DB Show command
3131
pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
3232
let params = context.config_parameters()?;
33-
let client = client_builder_with_fallback_genesis_key(&params)?.build()?;
33+
let client = client_builder_with_fallback_genesis_key(&params)?
34+
.with_logger(context.logger().clone())
35+
.build()?;
3436

3537
let get_list_of_artifact_ids = || async {
3638
let cardano_dbs = client.snapshot().list().await.with_context(|| {

mithril-client-cli/src/commands/cardano_stake_distribution/download.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ impl CardanoStakeDistributionDownloadCommand {
4949
let params = context.config_parameters()?.add_source(self)?;
5050
let download_dir = params.get_or("download_dir", ".");
5151
let download_dir = Path::new(&download_dir);
52+
let logger = context.logger();
5253

5354
let progress_output_type = if self.is_json_output_enabled() {
5455
ProgressOutputType::JsonReporter
@@ -59,7 +60,9 @@ impl CardanoStakeDistributionDownloadCommand {
5960
let client = client_builder(&params)?
6061
.add_feedback_receiver(Arc::new(IndicatifFeedbackReceiver::new(
6162
progress_output_type,
63+
logger.clone(),
6264
)))
65+
.with_logger(logger.clone())
6366
.build()?;
6467

6568
progress_printer.report_step(

mithril-client-cli/src/commands/cardano_stake_distribution/list.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ impl CardanoStakeDistributionListCommand {
2323
/// Main command execution
2424
pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
2525
let params = context.config_parameters()?;
26-
let client = client_builder_with_fallback_genesis_key(&params)?.build()?;
26+
let client = client_builder_with_fallback_genesis_key(&params)?
27+
.with_logger(context.logger().clone())
28+
.build()?;
2729
let lines = client.cardano_stake_distribution().list().await?;
2830

2931
if self.is_json_output_enabled() {

mithril-client-cli/src/commands/cardano_transaction/certify.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anyhow::{anyhow, Context};
22
use clap::Parser;
33
use cli_table::{print_stdout, Cell, Table};
4-
use slog_scope::debug;
4+
use slog::debug;
55
use std::{collections::HashMap, sync::Arc};
66

77
use mithril_client::{
@@ -40,6 +40,7 @@ impl CardanoTransactionsCertifyCommand {
4040
/// Cardano transaction certify command
4141
pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
4242
let params = context.config_parameters()?.add_source(self)?;
43+
let logger = context.logger();
4344

4445
let progress_output_type = if self.is_json_output_enabled() {
4546
ProgressOutputType::JsonReporter
@@ -50,7 +51,9 @@ impl CardanoTransactionsCertifyCommand {
5051
let client = client_builder(&params)?
5152
.add_feedback_receiver(Arc::new(IndicatifFeedbackReceiver::new(
5253
progress_output_type,
54+
logger.clone(),
5355
)))
56+
.with_logger(logger.clone())
5457
.build()?;
5558

5659
progress_printer.report_step(1, "Fetching a proof for the given transactions…")?;
@@ -64,10 +67,7 @@ impl CardanoTransactionsCertifyCommand {
6467
self.transactions_hashes
6568
)
6669
})?;
67-
debug!(
68-
"Got Proof from aggregator, proof: {:?}",
69-
cardano_transaction_proof
70-
);
70+
debug!(logger, "Got Proof from aggregator"; "proof" => ?cardano_transaction_proof);
7171

7272
let verified_transactions =
7373
Self::verify_proof_validity(2, &progress_printer, &cardano_transaction_proof)?;

mithril-client-cli/src/commands/cardano_transaction/snapshot_list.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ impl CardanoTransactionSnapshotListCommand {
2222
/// Main command execution
2323
pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
2424
let params = context.config_parameters()?;
25-
let client = client_builder_with_fallback_genesis_key(&params)?.build()?;
25+
let client = client_builder_with_fallback_genesis_key(&params)?
26+
.with_logger(context.logger().clone())
27+
.build()?;
2628
let lines = client.cardano_transaction().list_snapshots().await?;
2729

2830
if self.is_json_output_enabled() {

mithril-client-cli/src/commands/cardano_transaction/snapshot_show.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ impl CardanoTransactionsSnapshotShowCommand {
3131
/// Cardano transaction snapshot Show command
3232
pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
3333
let params = context.config_parameters()?;
34-
let client = client_builder_with_fallback_genesis_key(&params)?.build()?;
34+
let client = client_builder_with_fallback_genesis_key(&params)?
35+
.with_logger(context.logger().clone())
36+
.build()?;
3537

3638
let get_list_of_artifact_ids = || async {
3739
let transactions_sets = client.cardano_transaction().list_snapshots().await.with_context(|| {

mithril-client-cli/src/commands/mithril_stake_distribution/download.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ impl MithrilStakeDistributionDownloadCommand {
5050
let params = context.config_parameters()?.add_source(self)?;
5151
let download_dir = params.get_or("download_dir", ".");
5252
let download_dir = Path::new(&download_dir);
53+
let logger = context.logger();
5354

5455
let progress_output_type = if self.is_json_output_enabled() {
5556
ProgressOutputType::JsonReporter
@@ -60,7 +61,9 @@ impl MithrilStakeDistributionDownloadCommand {
6061
let client = client_builder(&params)?
6162
.add_feedback_receiver(Arc::new(IndicatifFeedbackReceiver::new(
6263
progress_output_type,
64+
logger.clone(),
6365
)))
66+
.with_logger(logger.clone())
6467
.build()?;
6568

6669
let get_list_of_artifact_ids = || async {

mithril-client-cli/src/commands/mithril_stake_distribution/list.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ impl MithrilStakeDistributionListCommand {
2323
/// Main command execution
2424
pub async fn execute(&self, context: CommandContext) -> MithrilResult<()> {
2525
let params = context.config_parameters()?;
26-
let client = client_builder_with_fallback_genesis_key(&params)?.build()?;
26+
let client = client_builder_with_fallback_genesis_key(&params)?
27+
.with_logger(context.logger().clone())
28+
.build()?;
2729
let lines = client.mithril_stake_distribution().list().await?;
2830

2931
if self.is_json_output_enabled() {

mithril-client-cli/src/commands/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub use deprecation::{DeprecatedCommand, Deprecation};
1313

1414
use clap::Args;
1515
use mithril_client::{ClientBuilder, MithrilResult};
16-
use slog_scope::logger;
1716

1817
use crate::configuration::ConfigParameters;
1918

@@ -29,8 +28,7 @@ pub(crate) fn client_builder(params: &ConfigParameters) -> MithrilResult<ClientB
2928
let builder = ClientBuilder::aggregator(
3029
&params.require("aggregator_endpoint")?,
3130
&params.require("genesis_verification_key")?,
32-
)
33-
.with_logger(logger());
31+
);
3432

3533
Ok(builder)
3634
}
@@ -50,8 +48,7 @@ pub(crate) fn client_builder_with_fallback_genesis_key(
5048
"genesis_verification_key",
5149
fallback_genesis_verification_key,
5250
),
53-
)
54-
.with_logger(logger());
51+
);
5552

5653
Ok(builder)
5754
}

mithril-client-cli/src/main.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
use anyhow::{anyhow, Context};
44
use clap::{CommandFactory, Parser, Subcommand};
55
use config::{builder::DefaultState, ConfigBuilder, Map, Source, Value, ValueKind};
6-
use slog::{Drain, Fuse, Level, Logger};
7-
use slog_async::Async;
8-
use slog_scope::debug;
6+
use slog::{debug, Drain, Fuse, Level, Logger};
97
use slog_term::Decorator;
108
use std::io::Write;
119
use std::sync::Arc;
@@ -86,9 +84,9 @@ pub struct Args {
8684

8785
impl Args {
8886
pub async fn execute(&self, root_logger: Logger) -> MithrilResult<()> {
89-
debug!("Run Mode: {}", self.run_mode);
87+
debug!(root_logger, "Run Mode: {}", self.run_mode);
9088
let filename = format!("{}/{}.json", self.config_directory.display(), self.run_mode);
91-
debug!("Reading configuration file '{}'.", filename);
89+
debug!(root_logger, "Reading configuration file '{filename}'.");
9290
let config: ConfigBuilder<DefaultState> = config::Config::builder()
9391
.add_source(config::File::with_name(&filename).required(false))
9492
.add_source(self.clone())
@@ -116,7 +114,7 @@ impl Args {
116114
}
117115
}
118116

119-
fn wrap_drain<D: Decorator + Send + 'static>(&self, decorator: D) -> Fuse<Async> {
117+
fn wrap_drain<D: Decorator + Send + 'static>(&self, decorator: D) -> Fuse<slog_async::Async> {
120118
let drain = slog_term::CompactFormat::new(decorator).build().fuse();
121119
let drain = slog::LevelFilter::new(drain, self.log_level()).fuse();
122120

@@ -128,7 +126,10 @@ impl Args {
128126
let writer = log_output_type.get_writer()?;
129127

130128
let drain = if self.log_format_json {
131-
let drain = slog_bunyan::new(writer).set_pretty(false).build().fuse();
129+
let drain = slog_bunyan::with_name("mithril-client", writer)
130+
.set_pretty(false)
131+
.build()
132+
.fuse();
132133
let drain = slog::LevelFilter::new(drain, self.log_level()).fuse();
133134

134135
slog_async::Async::new(drain).build().fuse()
@@ -240,7 +241,6 @@ async fn main() -> MithrilResult<()> {
240241
)
241242
});
242243
let logger = args.build_logger()?;
243-
let _guard = slog_scope::set_global_logger(logger.clone());
244244

245245
#[cfg(feature = "bundle_openssl")]
246246
openssl_probe::init_ssl_cert_env_vars();

0 commit comments

Comments
 (0)