Skip to content

Commit a4ed3dc

Browse files
committed
feat(example_wallet_rpc): enhance logging with structured JSON output
1 parent 71bf53d commit a4ed3dc

File tree

2 files changed

+58
-31
lines changed

2 files changed

+58
-31
lines changed

example-crates/example_wallet_rpc/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ name = "example_wallet_rpc"
33
version = "0.1.0"
44
edition = "2021"
55

6+
[[bin]]
7+
name = "example_wallet_rpc"
8+
path = "src/main.rs"
9+
610
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
711

812
[dependencies]
@@ -12,3 +16,4 @@ bdk_bitcoind_rpc = { version = "0.18" }
1216
anyhow = "1"
1317
clap = { version = "4.5.17", features = ["derive", "env"] }
1418
ctrlc = "2.0.1"
19+
serde_json = "1.0"

example-crates/example_wallet_rpc/src/main.rs

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use bdk_wallet::{
99
};
1010
use clap::{self, Parser};
1111
use std::{path::PathBuf, sync::mpsc::sync_channel, thread::spawn, time::Instant};
12+
use serde_json::json;
1213

1314
const DB_MAGIC: &str = "bdk-rpc-wallet-example";
1415

@@ -81,8 +82,11 @@ fn main() -> anyhow::Result<()> {
8182

8283
let rpc_client = args.client()?;
8384
println!(
84-
"Connected to Bitcoin Core RPC at {:?}",
85-
rpc_client.get_blockchain_info().unwrap()
85+
"{}",
86+
serde_json::to_string_pretty(&json!({
87+
"event": "connection",
88+
"blockchain_info": rpc_client.get_blockchain_info().unwrap()
89+
}))?
8690
);
8791

8892
let start_load_wallet = Instant::now();
@@ -105,19 +109,19 @@ fn main() -> anyhow::Result<()> {
105109
.create_wallet(&mut db)?,
106110
},
107111
};
108-
println!(
109-
"Loaded wallet in {}s",
110-
start_load_wallet.elapsed().as_secs_f32()
111-
);
112-
113112
let balance = wallet.balance();
114-
println!("Wallet balance before syncing: {}", balance.total());
115-
116113
let wallet_tip = wallet.latest_checkpoint();
117114
println!(
118-
"Wallet tip: {} at height {}",
119-
wallet_tip.hash(),
120-
wallet_tip.height()
115+
"{}",
116+
serde_json::to_string_pretty(&json!({
117+
"event": "wallet_loaded",
118+
"duration_seconds": start_load_wallet.elapsed().as_secs_f32(),
119+
"initial_balance": balance.total(),
120+
"tip": {
121+
"hash": wallet_tip.hash().to_string(),
122+
"height": wallet_tip.height()
123+
}
124+
}))?
121125
);
122126

123127
let (sender, receiver) = sync_channel::<Emission>(21);
@@ -143,7 +147,13 @@ fn main() -> anyhow::Result<()> {
143147
for emission in receiver {
144148
match emission {
145149
Emission::SigTerm => {
146-
println!("Sigterm received, exiting...");
150+
println!(
151+
"{}",
152+
serde_json::to_string_pretty(&json!({
153+
"event": "sigterm",
154+
"message": "Sigterm received, exiting..."
155+
}))?
156+
);
147157
break;
148158
}
149159
Emission::Block(block_emission) => {
@@ -156,39 +166,51 @@ fn main() -> anyhow::Result<()> {
156166
wallet.persist(&mut db)?;
157167
let elapsed = start_apply_block.elapsed().as_secs_f32();
158168
println!(
159-
"Applied block {} at height {} in {}s",
160-
hash, height, elapsed
169+
"{}",
170+
serde_json::to_string_pretty(&json!({
171+
"event": "block_applied",
172+
"block": {
173+
"hash": hash.to_string(),
174+
"height": height
175+
},
176+
"duration_seconds": elapsed
177+
}))?
161178
);
162179
}
163180
Emission::Mempool(mempool_emission) => {
164181
let start_apply_mempool = Instant::now();
165182
wallet.apply_unconfirmed_txs(mempool_emission);
166183
wallet.persist(&mut db)?;
167184
println!(
168-
"Applied unconfirmed transactions in {}s",
169-
start_apply_mempool.elapsed().as_secs_f32()
185+
"{}",
186+
serde_json::to_string_pretty(&json!({
187+
"event": "mempool_applied",
188+
"duration_seconds": start_apply_mempool.elapsed().as_secs_f32()
189+
}))?
170190
);
171191
break;
172192
}
173193
}
174194
}
195+
175196
let wallet_tip_end = wallet.latest_checkpoint();
176197
let balance = wallet.balance();
177198
println!(
178-
"Synced {} blocks in {}s",
179-
blocks_received,
180-
start_load_wallet.elapsed().as_secs_f32(),
181-
);
182-
println!(
183-
"Wallet tip is '{}:{}'",
184-
wallet_tip_end.height(),
185-
wallet_tip_end.hash()
186-
);
187-
println!("Wallet balance is {}", balance.total());
188-
println!(
189-
"Wallet has {} transactions and {} utxos",
190-
wallet.transactions().count(),
191-
wallet.list_unspent().count()
199+
"{}",
200+
serde_json::to_string_pretty(&json!({
201+
"event": "sync_complete",
202+
"blocks_processed": blocks_received,
203+
"total_duration_seconds": start_load_wallet.elapsed().as_secs_f32(),
204+
"final_state": {
205+
"tip": {
206+
"height": wallet_tip_end.height(),
207+
"hash": wallet_tip_end.hash().to_string()
208+
},
209+
"balance": balance.total(),
210+
"transaction_count": wallet.transactions().count(),
211+
"utxo_count": wallet.list_unspent().count()
212+
}
213+
}))?
192214
);
193215

194216
Ok(())

0 commit comments

Comments
 (0)