Skip to content

Commit 7eb4ccd

Browse files
committed
fix:fix logging
Signed-off-by: Chen Kai <[email protected]>
1 parent b37d879 commit 7eb4ccd

File tree

1 file changed

+45
-32
lines changed

1 file changed

+45
-32
lines changed

bin/archiver/src/main.rs

+45-32
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,25 @@ use std::sync::Arc;
55
use std::time::Duration;
66

77
use again::RetryPolicy;
8+
use blob_archiver_beacon::beacon_client;
9+
use blob_archiver_beacon::beacon_client::BeaconClientEth2;
10+
use blob_archiver_storage::fs::FSStorage;
11+
use blob_archiver_storage::s3::{S3Config, S3Storage};
12+
use blob_archiver_storage::storage;
13+
use blob_archiver_storage::storage::{Storage, StorageType};
814
use clap::Parser;
915
use ctrlc::set_handler;
1016
use eth2::types::{BlockId, Hash256};
1117
use eth2::{BeaconNodeHttpClient, SensitiveUrl, Timeouts};
18+
use eyre::eyre;
1219
use serde::{Deserialize, Serialize};
1320
use tokio::sync::Mutex;
1421
use tracing::log::error;
22+
use tracing::Level;
1523
use tracing_appender::rolling::{RollingFileAppender, Rotation};
24+
use tracing_subscriber::fmt;
25+
use tracing_subscriber::fmt::writer::MakeWriterExt;
1626
use tracing_subscriber::layer::SubscriberExt;
17-
use tracing_subscriber::util::SubscriberInitExt;
18-
use tracing_subscriber::{fmt, EnvFilter};
19-
20-
use blob_archiver_beacon::beacon_client;
21-
use blob_archiver_beacon::beacon_client::BeaconClientEth2;
22-
use blob_archiver_storage::fs::FSStorage;
23-
use blob_archiver_storage::s3::{S3Config, S3Storage};
24-
use blob_archiver_storage::storage;
25-
use blob_archiver_storage::storage::{Storage, StorageType};
2627

2728
use crate::archiver::{Archiver, Config, STARTUP_FETCH_BLOB_MAXIMUM_RETRIES};
2829

@@ -119,18 +120,6 @@ pub fn setup_tracing(
119120
log_dir: Option<PathBuf>,
120121
rotation: Option<Rotation>,
121122
) -> eyre::Result<()> {
122-
let filter = match verbose {
123-
0 => EnvFilter::new("error"),
124-
1 => EnvFilter::new("warn"),
125-
2 => EnvFilter::new("info"),
126-
3 => EnvFilter::new("debug"),
127-
_ => EnvFilter::new("trace"),
128-
};
129-
130-
let subscriber = tracing_subscriber::registry()
131-
.with(EnvFilter::from_default_env())
132-
.with(filter);
133-
134123
if let Some(log_dir) = log_dir {
135124
fs::create_dir_all(&log_dir)
136125
.map_err(|e| eyre::eyre!("Failed to create log directory: {}", e))?;
@@ -141,19 +130,43 @@ pub fn setup_tracing(
141130
"blob-archiver.log",
142131
);
143132
let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
144-
let file_layer = fmt::layer().with_writer(non_blocking).with_ansi(false);
145-
146-
subscriber
147-
.with(file_layer)
148-
.with(fmt::layer().with_writer(std::io::stdout))
149-
.try_init()?;
133+
let file_layer = fmt::layer()
134+
.with_writer(non_blocking.with_max_level(match verbose {
135+
0 => Level::ERROR,
136+
1 => Level::WARN,
137+
2 => Level::INFO,
138+
3 => Level::DEBUG,
139+
_ => Level::TRACE,
140+
}))
141+
.with_ansi(false);
142+
143+
let subscriber =
144+
tracing_subscriber::registry()
145+
.with(file_layer)
146+
.with(
147+
fmt::layer().with_writer(std::io::stdout.with_max_level(match verbose {
148+
0 => Level::ERROR,
149+
1 => Level::WARN,
150+
2 => Level::INFO,
151+
3 => Level::DEBUG,
152+
_ => Level::TRACE,
153+
})),
154+
);
155+
tracing::subscriber::set_global_default(subscriber).map_err(|e| eyre!(e))?;
156+
Ok(())
150157
} else {
151-
subscriber
152-
.with(fmt::layer().with_writer(std::io::stdout))
153-
.try_init()?;
158+
let subscriber = tracing_subscriber::registry().with(fmt::layer().with_writer(
159+
std::io::stdout.with_max_level(match verbose {
160+
0 => Level::ERROR,
161+
1 => Level::WARN,
162+
2 => Level::INFO,
163+
3 => Level::DEBUG,
164+
_ => Level::TRACE,
165+
}),
166+
));
167+
tracing::subscriber::set_global_default(subscriber).map_err(|e| eyre!(e))?;
168+
Ok(())
154169
}
155-
156-
Ok(())
157170
}
158171

159172
#[derive(Parser, Serialize)]

0 commit comments

Comments
 (0)