Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(logging): enhance logging setup and usage #224

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion bin/vault-admin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ struct Arguments {
async fn main() -> Result<()> {
let args = Arguments::parse();

setup_logging(&args.log_level)?;
tracing::subscriber::set_global_default(setup_logging(
env!("CARGO_CRATE_NAME"),
&args.log_level,
)?)?;

info!("Quote verified! Connection secure!");

Expand Down
6 changes: 5 additions & 1 deletion bin/verify-era-proof-attestation/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ use zksync_basic_types::L1BatchNumber;
#[tokio::main]
async fn main() -> Result<()> {
let args = Arguments::parse();
setup_logging(&args.log_level)?;
tracing::subscriber::set_global_default(setup_logging(
env!("CARGO_CRATE_NAME"),
&args.log_level,
)?)?;

validate_arguments(&args)?;
let (stop_sender, stop_receiver) = watch::channel(false);
let mut process_handle = tokio::spawn(verify_batches_proofs(stop_receiver, args));
Expand Down
24 changes: 16 additions & 8 deletions crates/teepot/src/log/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

use anyhow::Context;
use tracing::level_filters::LevelFilter;
use tracing::Subscriber;
use tracing_log::LogTracer;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::Registry;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
use tracing_subscriber::{prelude::*, EnvFilter};

/// A log level parser for clap, with "off", "error", "warn", "info", "debug", "trace" as valid values
#[derive(Clone)]
Expand Down Expand Up @@ -42,22 +44,28 @@ impl clap::builder::TypedValueParser for LogLevelParser {
}

/// Setup standard logging and loglevel for the current crate and the `teepot` crate.
pub fn setup_logging(log_level: &LevelFilter) -> anyhow::Result<()> {
pub fn setup_logging(
crate_name: &str,
log_level: &LevelFilter,
) -> anyhow::Result<impl Subscriber + Send + Sync + 'static> {
LogTracer::init().context("Failed to set logger")?;
let filter = EnvFilter::builder()
.try_from_env()
.unwrap_or(match *log_level {
LevelFilter::OFF => EnvFilter::new("off"),
_ => EnvFilter::new(format!(
"warn,{crate_name}={log_level},teepot={log_level}",
crate_name = env!("CARGO_CRATE_NAME"),
log_level = log_level
)),
});
let subscriber = Registry::default()
.with(filter)
.with(fmt::layer().with_writer(std::io::stderr));
tracing::subscriber::set_global_default(subscriber)?;

Ok(())
let fmt_layer = tracing_subscriber::fmt::layer()
.with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
.with_file(false)
.with_line_number(false)
.with_writer(std::io::stderr);

let subscriber = Registry::default().with(filter).with(fmt_layer);

Ok(subscriber)
}