From af3ab51320a67f20cbf5a47679559298f983e03c Mon Sep 17 00:00:00 2001 From: Harald Hoyer Date: Wed, 18 Sep 2024 13:21:12 +0200 Subject: [PATCH] feat(logging): centralize logging setup in teepot crate - Added a new logging module in `teepot` crate. - Removed redundant logging setup code from individual projects. - Updated dependencies and references for logging setup. Signed-off-by: Harald Hoyer --- Cargo.lock | 2 + bin/vault-admin/src/main.rs | 17 +++--- bin/verify-era-proof-attestation/src/args.rs | 34 +---------- bin/verify-era-proof-attestation/src/main.rs | 32 ++-------- crates/teepot/Cargo.toml | 2 + crates/teepot/src/lib.rs | 1 + crates/teepot/src/log/mod.rs | 63 ++++++++++++++++++++ 7 files changed, 84 insertions(+), 67 deletions(-) create mode 100644 crates/teepot/src/log/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 1010857..0bf6a65 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4952,6 +4952,8 @@ dependencies = [ "testaso", "thiserror", "tracing", + "tracing-log 0.2.0", + "tracing-subscriber", "webpki-roots", "x509-cert", "zeroize", diff --git a/bin/vault-admin/src/main.rs b/bin/vault-admin/src/main.rs index 91d17e0..db6d6eb 100644 --- a/bin/vault-admin/src/main.rs +++ b/bin/vault-admin/src/main.rs @@ -15,12 +15,11 @@ use teepot::json::http::{ SignRequest, SignRequestData, SignResponse, VaultCommandRequest, VaultCommands, VaultCommandsResponse, DIGEST_URL, }; +use teepot::log::{setup_logging, LogLevelParser}; use teepot::server::signatures::verify_sig; use teepot::sgx::sign::Signature; +use tracing::level_filters::LevelFilter; use tracing::{error, info}; -use tracing_log::LogTracer; -use tracing_subscriber::Registry; -use tracing_subscriber::{fmt, prelude::*, EnvFilter}; #[derive(Args, Debug)] struct SendArgs { @@ -101,18 +100,18 @@ enum SubCommands { struct Arguments { #[clap(subcommand)] cmd: SubCommands, + /// Log level for the log output. + /// Valid values are: `off`, `error`, `warn`, `info`, `debug`, `trace` + #[clap(long, default_value_t = LevelFilter::WARN, value_parser = LogLevelParser)] + pub log_level: LevelFilter, } #[actix_web::main] async fn main() -> Result<()> { - LogTracer::init().context("Failed to set logger")?; + let args = Arguments::parse(); - let subscriber = Registry::default() - .with(EnvFilter::from_default_env()) - .with(fmt::layer().with_writer(std::io::stderr)); - tracing::subscriber::set_global_default(subscriber).unwrap(); + setup_logging(&args.log_level)?; - let args = Arguments::parse(); info!("Quote verified! Connection secure!"); match args.cmd { diff --git a/bin/verify-era-proof-attestation/src/args.rs b/bin/verify-era-proof-attestation/src/args.rs index 95555a5..cea2a1c 100644 --- a/bin/verify-era-proof-attestation/src/args.rs +++ b/bin/verify-era-proof-attestation/src/args.rs @@ -4,6 +4,7 @@ use anyhow::{anyhow, Result}; use clap::{ArgGroup, Args, Parser}; use std::time::Duration; +use teepot::log::LogLevelParser; use teepot::sgx::{parse_tcb_levels, EnumSet, TcbLevel}; use tracing_subscriber::filter::LevelFilter; use url::Url; @@ -18,6 +19,8 @@ use zksync_types::L2ChainId; .args(&["batch_range", "continuous"]), ))] pub struct Arguments { + /// Log level for the log output. + /// Valid values are: `off`, `error`, `warn`, `info`, `debug`, `trace` #[clap(long, default_value_t = LevelFilter::WARN, value_parser = LogLevelParser)] pub log_level: LevelFilter, /// The batch number or range of batch numbers to verify the attestation and signature (e.g., @@ -90,34 +93,3 @@ fn parse_duration(s: &str) -> Result { let millis = s.parse()?; Ok(Duration::from_millis(millis)) } - -#[derive(Clone)] -struct LogLevelParser; - -impl clap::builder::TypedValueParser for LogLevelParser { - type Value = LevelFilter; - - fn parse_ref( - &self, - cmd: &clap::Command, - arg: Option<&clap::Arg>, - value: &std::ffi::OsStr, - ) -> Result { - clap::builder::TypedValueParser::parse(self, cmd, arg, value.to_owned()) - } - - fn parse( - &self, - cmd: &clap::Command, - arg: Option<&clap::Arg>, - value: std::ffi::OsString, - ) -> std::result::Result { - use std::str::FromStr; - let p = clap::builder::PossibleValuesParser::new([ - "off", "error", "warn", "info", "debug", "trace", - ]); - let v = p.parse(cmd, arg, value)?; - - Ok(LevelFilter::from_str(&v).unwrap()) - } -} diff --git a/bin/verify-era-proof-attestation/src/main.rs b/bin/verify-era-proof-attestation/src/main.rs index cbb263a..affdc0a 100644 --- a/bin/verify-era-proof-attestation/src/main.rs +++ b/bin/verify-era-proof-attestation/src/main.rs @@ -8,23 +8,21 @@ mod client; mod proof; mod verification; -use anyhow::{Context, Result}; +use crate::verification::{ + log_quote_verification_summary, verify_attestation_quote, verify_batch_proof, +}; +use anyhow::Result; use args::{Arguments, AttestationPolicyArgs}; use clap::Parser; use client::MainNodeClient; use proof::get_proofs; use reqwest::Client; +use teepot::log::setup_logging; use tokio::{signal, sync::watch}; use tracing::{debug, error, info, trace, warn}; -use tracing_log::LogTracer; -use tracing_subscriber::{filter::LevelFilter, fmt, prelude::*, EnvFilter, Registry}; use url::Url; use zksync_basic_types::L1BatchNumber; -use crate::verification::{ - log_quote_verification_summary, verify_attestation_quote, verify_batch_proof, -}; - #[tokio::main] async fn main() -> Result<()> { let args = Arguments::parse(); @@ -45,26 +43,6 @@ async fn main() -> Result<()> { Ok(()) } -fn setup_logging(log_level: &LevelFilter) -> Result<()> { - 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(()) -} - fn validate_arguments(args: &Arguments) -> Result<()> { if args.attestation_policy.sgx_mrsigners.is_none() && args.attestation_policy.sgx_mrenclaves.is_none() diff --git a/crates/teepot/Cargo.toml b/crates/teepot/Cargo.toml index a2c48bf..c33aa89 100644 --- a/crates/teepot/Cargo.toml +++ b/crates/teepot/Cargo.toml @@ -39,6 +39,8 @@ sha2.workspace = true signature.workspace = true thiserror.workspace = true tracing.workspace = true +tracing-log.workspace = true +tracing-subscriber.workspace = true webpki-roots.workspace = true x509-cert.workspace = true zeroize.workspace = true diff --git a/crates/teepot/src/lib.rs b/crates/teepot/src/lib.rs index 8d38678..b3bcf16 100644 --- a/crates/teepot/src/lib.rs +++ b/crates/teepot/src/lib.rs @@ -11,4 +11,5 @@ pub mod json; pub mod server; pub mod sgx; +pub mod log; pub mod quote; diff --git a/crates/teepot/src/log/mod.rs b/crates/teepot/src/log/mod.rs new file mode 100644 index 0000000..e91b7fa --- /dev/null +++ b/crates/teepot/src/log/mod.rs @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright (c) 2024 Matter Labs + +//! Logging related stuff + +use anyhow::Context; +use tracing::level_filters::LevelFilter; +use tracing_log::LogTracer; +use tracing_subscriber::Registry; +use tracing_subscriber::{fmt, prelude::*, EnvFilter}; + +/// A log level parser for clap, with "off", "error", "warn", "info", "debug", "trace" as valid values +#[derive(Clone)] +pub struct LogLevelParser; + +impl clap::builder::TypedValueParser for LogLevelParser { + type Value = LevelFilter; + + fn parse_ref( + &self, + cmd: &clap::Command, + arg: Option<&clap::Arg>, + value: &std::ffi::OsStr, + ) -> anyhow::Result { + clap::builder::TypedValueParser::parse(self, cmd, arg, value.to_owned()) + } + + fn parse( + &self, + cmd: &clap::Command, + arg: Option<&clap::Arg>, + value: std::ffi::OsString, + ) -> std::result::Result { + use std::str::FromStr; + let p = clap::builder::PossibleValuesParser::new([ + "off", "error", "warn", "info", "debug", "trace", + ]); + let v = p.parse(cmd, arg, value)?; + + Ok(LevelFilter::from_str(&v).unwrap()) + } +} + +/// Setup standard logging and loglevel for the current crate and the `teepot` crate. +pub fn setup_logging(log_level: &LevelFilter) -> anyhow::Result<()> { + 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(()) +}