Skip to content

Commit efe5c7a

Browse files
committed
feat(logging): enable info-level logging during startup, drop to configured verbosity after
Electrs has been observed starting, running for a few minutes, then dying silently in environments with no verbosity flags set. With the default verbosity (error-only), there is zero diagnostic output during startup. Initialize stderrlog at info level minimum during startup so key lifecycle milestones are always visible (daemon connection, DB open, sync progress, mempool load, server ready). After startup completes, log level drops to the user's configured verbosity via log::set_max_level(). Also promotes a handful of debug!() calls in schema.rs and mempool.rs to info!() so they fire during the startup window.
1 parent 8915a40 commit efe5c7a

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

src/bin/electrs.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,13 @@ fn run_server(config: Arc<Config>, salt_rwlock: Arc<RwLock<String>>) -> Result<(
5757
let metrics = Metrics::new(config.monitoring_addr);
5858
metrics.start();
5959

60+
info!("starting electrs");
61+
6062
if let Some(zmq_addr) = config.zmq_addr.as_ref() {
6163
zmq::start(&format!("tcp://{zmq_addr}"), block_hash_notify);
6264
}
6365

66+
info!("connecting to daemon at {}", config.daemon_rpc_addr);
6467
let daemon = Arc::new(Daemon::new(
6568
&config.daemon_dir,
6669
&config.blocks_dir,
@@ -71,14 +74,17 @@ fn run_server(config: Arc<Config>, salt_rwlock: Arc<RwLock<String>>) -> Result<(
7174
signal.clone(),
7275
&metrics,
7376
)?);
77+
info!("opening database at {}", config.db_path.display());
7478
let store = Arc::new(Store::open(&config, &metrics, true));
7579
let mut indexer = Indexer::open(
7680
Arc::clone(&store),
7781
fetch_from(&config, &store),
7882
&config,
7983
&metrics,
8084
);
85+
info!("starting initial sync");
8186
let mut tip = indexer.update(&daemon)?;
87+
info!("initial sync complete, tip at {}", tip);
8288

8389
let chain = Arc::new(ChainQuery::new(
8490
Arc::clone(&store),
@@ -93,6 +99,7 @@ fn run_server(config: Arc<Config>, salt_rwlock: Arc<RwLock<String>>) -> Result<(
9399
precache::precache(&chain, precache_scripthashes);
94100
}
95101

102+
info!("loading mempool");
96103
let mempool = Arc::new(RwLock::new(Mempool::new(
97104
Arc::clone(&chain),
98105
&metrics,
@@ -130,6 +137,9 @@ fn run_server(config: Arc<Config>, salt_rwlock: Arc<RwLock<String>>) -> Result<(
130137
Arc::clone(&salt_rwlock),
131138
);
132139

140+
info!("startup complete, switching to configured log verbosity");
141+
log::set_max_level(config.log_level_filter());
142+
133143
let main_loop_count = metrics.gauge(MetricOpts::new(
134144
"electrs_main_loop_count",
135145
"count of iterations of electrs main loop each 5 seconds or after interrupts",

src/config.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ pub struct Config {
4343
pub rpc_logging: RpcLogging,
4444
pub zmq_addr: Option<SocketAddr>,
4545

46+
/// The user's requested log verbosity level (number of -v flags).
47+
/// Used to restore log level after startup logging completes.
48+
pub log_verbosity: usize,
49+
4650
/// RocksDB block cache size in MB (per database)
4751
/// Caches decompressed data blocks, plus index and filter blocks (via cache_index_and_filter_blocks).
4852
/// Total memory usage = cache_size * 3_databases (txstore, history, cache)
@@ -463,8 +467,13 @@ impl Config {
463467
.value_of("electrum_public_hosts")
464468
.map(|s| serde_json::from_str(s).expect("invalid --electrum-public-hosts"));
465469

470+
let user_verbosity = m.occurrences_of("verbosity") as usize;
471+
// Initialize logging at info level (verbosity 2) minimum so that
472+
// startup milestone logs are always visible. After startup completes,
473+
// the log level is dropped to the user's configured verbosity.
474+
let startup_verbosity = std::cmp::max(user_verbosity, 2);
466475
let mut log = stderrlog::new();
467-
log.verbosity(m.occurrences_of("verbosity") as usize);
476+
log.verbosity(startup_verbosity);
468477
log.timestamp(if m.is_present("timestamp") {
469478
stderrlog::Timestamp::Millisecond
470479
} else {
@@ -509,6 +518,7 @@ impl Config {
509518
initial_sync_batch_size: value_t_or_exit!(m, "initial_sync_batch_size", usize),
510519
db_cache_index_filter_blocks: m.is_present("cache_index_filter_blocks"),
511520
zmq_addr,
521+
log_verbosity: user_verbosity,
512522

513523
#[cfg(feature = "liquid")]
514524
parent_network,
@@ -526,6 +536,16 @@ impl Config {
526536
config
527537
}
528538

539+
pub fn log_level_filter(&self) -> log::LevelFilter {
540+
match self.log_verbosity {
541+
0 => log::LevelFilter::Error,
542+
1 => log::LevelFilter::Warn,
543+
2 => log::LevelFilter::Info,
544+
3 => log::LevelFilter::Debug,
545+
_ => log::LevelFilter::Trace,
546+
}
547+
}
548+
529549
pub fn cookie_getter(&self) -> Arc<dyn CookieGetter> {
530550
if let Some(ref value) = self.cookie {
531551
Arc::new(StaticCookie {

src/new_index/mempool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ impl Mempool {
559559
.difference(&indexed_txids)
560560
.collect::<Vec<_>>();
561561

562-
debug!(
562+
info!(
563563
"mempool with total {} txs, {} indexed locally, {} to fetch",
564564
bitcoind_txids.len(),
565565
indexed_txids.len(),

src/new_index/schema.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ impl Store {
6363

6464
let txstore_db = DB::open(&path.join("txstore"), config, verify_compat);
6565
let added_blockhashes = load_blockhashes(&txstore_db, &BlockRow::done_filter());
66-
debug!("{} blocks were added", added_blockhashes.len());
66+
info!("{} blocks were added", added_blockhashes.len());
6767

6868
let history_db = DB::open(&path.join("history"), config, verify_compat);
6969
let indexed_blockhashes = load_blockhashes(&history_db, &BlockRow::done_filter());
70-
debug!("{} blocks were indexed", indexed_blockhashes.len());
70+
info!("{} blocks were indexed", indexed_blockhashes.len());
7171

7272
let cache_db = DB::open(&path.join("cache"), config, verify_compat);
7373

@@ -92,7 +92,7 @@ impl Store {
9292
.expect("invalid header chain")
9393
.prev_blockhash;
9494
}
95-
debug!(
95+
info!(
9696
"{} headers were loaded, tip at {:?}",
9797
headers_map.len(),
9898
tip_hash

tests/common.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ impl TestRunner {
112112
electrum_banner: "".into(),
113113
rpc_logging: RpcLogging::default(),
114114
zmq_addr: None,
115+
log_verbosity: 0,
115116

116117
#[cfg(feature = "liquid")]
117118
asset_db_path: None, // XXX

0 commit comments

Comments
 (0)