From 52506aac03a4806c2266984368ec322a28d77e00 Mon Sep 17 00:00:00 2001 From: Francisco Moura Date: Fri, 18 Aug 2023 01:20:12 -0300 Subject: [PATCH] feat: standardize logs --- offchain/Cargo.lock | 33 +++++++++- offchain/Cargo.toml | 2 + offchain/advance-runner/Cargo.toml | 1 + offchain/advance-runner/src/config.rs | 9 +++ offchain/advance-runner/src/main.rs | 10 ++- offchain/advance-runner/tests/fixtures/mod.rs | 7 +++ offchain/authority-claimer/Cargo.toml | 3 +- offchain/authority-claimer/src/config/cli.rs | 7 +++ offchain/authority-claimer/src/config/mod.rs | 2 + offchain/authority-claimer/src/main.rs | 13 ++-- offchain/dispatcher/Cargo.toml | 1 + offchain/dispatcher/src/config.rs | 8 +++ offchain/dispatcher/src/main.rs | 12 ++-- offchain/graphql-server/Cargo.toml | 1 + offchain/graphql-server/src/config.rs | 16 +++-- offchain/graphql-server/src/main.rs | 27 ++------ offchain/host-runner/Cargo.toml | 1 + offchain/host-runner/src/config.rs | 35 ++++++++++- offchain/host-runner/src/main.rs | 17 +++-- offchain/indexer/Cargo.toml | 1 + offchain/indexer/src/config.rs | 6 ++ offchain/indexer/src/main.rs | 14 ++--- offchain/indexer/tests/integration.rs | 6 ++ offchain/inspect-server/Cargo.toml | 6 +- offchain/inspect-server/src/config.rs | 6 ++ offchain/inspect-server/src/inspect.rs | 8 +-- offchain/inspect-server/src/lib.rs | 2 +- offchain/inspect-server/src/main.rs | 14 ++--- offchain/inspect-server/src/server.rs | 14 +++-- offchain/inspect-server/tests/common/mod.rs | 7 +-- offchain/logs/Cargo.toml | 11 ++++ offchain/logs/src/lib.rs | 62 +++++++++++++++++++ offchain/state-server/Cargo.toml | 3 +- offchain/state-server/src/config.rs | 40 ++++++++++++ offchain/state-server/src/main.rs | 15 ++--- 35 files changed, 322 insertions(+), 98 deletions(-) create mode 100644 offchain/logs/Cargo.toml create mode 100644 offchain/logs/src/lib.rs create mode 100644 offchain/state-server/src/config.rs diff --git a/offchain/Cargo.lock b/offchain/Cargo.lock index 72f904bda..e10d3fcba 100644 --- a/offchain/Cargo.lock +++ b/offchain/Cargo.lock @@ -251,6 +251,7 @@ dependencies = [ "grpc-interfaces", "hex", "http-health-check", + "logs", "rand 0.8.5", "rollups-events", "sha3", @@ -485,6 +486,7 @@ dependencies = [ "eth-tx-manager", "ethers", "http-server", + "logs", "rollups-events", "rusoto_core", "serde", @@ -1516,6 +1518,7 @@ dependencies = [ "http-server", "hyper", "im", + "logs", "rand 0.8.5", "redis", "rollups-events", @@ -2425,6 +2428,7 @@ dependencies = [ "hex", "http-health-check", "juniper", + "logs", "rollups-data", "serde", "serde_json", @@ -2556,6 +2560,7 @@ dependencies = [ "grpc-interfaces", "hex", "http-health-check", + "logs", "mockall", "rand 0.8.5", "reqwest", @@ -2803,6 +2808,7 @@ dependencies = [ "clap", "env_logger", "http-health-check", + "logs", "rand 0.8.5", "rollups-data", "rollups-events", @@ -2853,11 +2859,10 @@ dependencies = [ "actix-cors", "actix-web", "clap", - "env_logger", "futures", "hex", "http-health-check", - "log", + "logs", "prost", "regex", "reqwest", @@ -2869,6 +2874,7 @@ dependencies = [ "tonic", "tonic-build", "tracing", + "tracing-actix-web", "uuid 1.4.1", ] @@ -3091,6 +3097,15 @@ version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" +[[package]] +name = "logs" +version = "1.0.0" +dependencies = [ + "clap", + "tracing", + "tracing-subscriber", +] + [[package]] name = "matchers" version = "0.1.0" @@ -4878,10 +4893,12 @@ dependencies = [ name = "state-server" version = "1.0.0" dependencies = [ + "clap", "eth-block-history", "eth-state-fold", "eth-state-fold-types", "eth-state-server-lib", + "logs", "serde", "snafu", "tokio", @@ -5443,6 +5460,18 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-actix-web" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c0b08ce08cbde6a96fc1e4ebb8132053e53ec7a5cd27eef93ede6b73ebbda06" +dependencies = [ + "actix-web", + "pin-project", + "tracing", + "uuid 1.4.1", +] + [[package]] name = "tracing-attributes" version = "0.1.26" diff --git a/offchain/Cargo.toml b/offchain/Cargo.toml index 7fbb45844..1103b5801 100644 --- a/offchain/Cargo.toml +++ b/offchain/Cargo.toml @@ -12,6 +12,7 @@ members = [ "http-server", "indexer", "inspect-server", + "logs", "redacted", "rollups-events", "rollups-http-client", @@ -83,6 +84,7 @@ tonic = "0.9" tonic-build = "0.9" tonic-health = "0.9" tracing = "0.1" +tracing-actix-web = "0.7" tracing-subscriber = "0.3" tracing-test = "0.2" url = "2" diff --git a/offchain/advance-runner/Cargo.toml b/offchain/advance-runner/Cargo.toml index 61b404286..a56a6cfc1 100644 --- a/offchain/advance-runner/Cargo.toml +++ b/offchain/advance-runner/Cargo.toml @@ -12,6 +12,7 @@ path = "src/main.rs" grpc-interfaces = { path = "../grpc-interfaces" } http-health-check = { path = "../http-health-check" } rollups-events = { path = "../rollups-events" } +logs = {path = "../logs"} async-trait.workspace = true backoff = { workspace = true, features = ["tokio"] } diff --git a/offchain/advance-runner/src/config.rs b/offchain/advance-runner/src/config.rs index 398078cd0..db1a2a2ba 100644 --- a/offchain/advance-runner/src/config.rs +++ b/offchain/advance-runner/src/config.rs @@ -9,6 +9,7 @@ use crate::server_manager::ServerManagerCLIConfig; pub use crate::server_manager::ServerManagerConfig; pub use crate::snapshot::config::{FSManagerConfig, SnapshotConfig}; use crate::snapshot::config::{SnapshotCLIConfig, SnapshotConfigError}; +use logs::{LogsConfig, LogsEnvCliConfig}; pub use rollups_events::{ BrokerCLIConfig, BrokerConfig, DAppMetadata, DAppMetadataCLIConfig, }; @@ -19,6 +20,7 @@ pub struct AdvanceRunnerConfig { pub broker_config: BrokerConfig, pub dapp_metadata: DAppMetadata, pub snapshot_config: SnapshotConfig, + pub logs_config: LogsConfig, pub backoff_max_elapsed_duration: Duration, pub healthcheck_port: u16, } @@ -36,11 +38,15 @@ impl AdvanceRunnerConfig { let backoff_max_elapsed_duration = Duration::from_millis(cli_config.backoff_max_elapsed_duration); let healthcheck_port = cli_config.healthcheck_port; + + let logs_config = LogsConfig::initialize(cli_config.logs_config); + Ok(Self { server_manager_config, broker_config, dapp_metadata, snapshot_config, + logs_config, backoff_max_elapsed_duration, healthcheck_port, }) @@ -67,6 +73,9 @@ struct CLIConfig { #[command(flatten)] snapshot_cli_config: SnapshotCLIConfig, + #[command(flatten)] + pub logs_config: LogsEnvCliConfig, + /// The max elapsed time for backoff in ms #[arg(long, env, default_value = "120000")] backoff_max_elapsed_duration: u64, diff --git a/offchain/advance-runner/src/main.rs b/offchain/advance-runner/src/main.rs index 2156f688a..7e19df436 100644 --- a/offchain/advance-runner/src/main.rs +++ b/offchain/advance-runner/src/main.rs @@ -2,16 +2,14 @@ // SPDX-License-Identifier: Apache-2.0 (see LICENSE) use advance_runner::config::AdvanceRunnerConfig; -use tracing_subscriber::filter::{EnvFilter, LevelFilter}; +use tracing::info; #[tokio::main] async fn main() -> Result<(), Box> { - let filter = EnvFilter::builder() - .with_default_directive(LevelFilter::INFO.into()) - .from_env_lossy(); - tracing_subscriber::fmt().with_env_filter(filter).init(); - let config = AdvanceRunnerConfig::parse()?; + logs::initialize_logs(&config.logs_config); + + info!("Starting Advance Runner"); advance_runner::run(config).await.map_err(|e| e.into()) } diff --git a/offchain/advance-runner/tests/fixtures/mod.rs b/offchain/advance-runner/tests/fixtures/mod.rs index f36d6f18e..a1beb008e 100644 --- a/offchain/advance-runner/tests/fixtures/mod.rs +++ b/offchain/advance-runner/tests/fixtures/mod.rs @@ -10,6 +10,7 @@ use grpc_interfaces::cartesi_machine::{ ConcurrencyConfig, MachineRuntimeConfig, }; use grpc_interfaces::cartesi_server_manager::{CyclesConfig, DeadlineConfig}; +use logs::LogsConfig; use rollups_events::{Address, BrokerEndpoint}; use std::cell::RefCell; use std::path::Path; @@ -90,6 +91,11 @@ impl AdvanceRunnerFixture { let backoff_max_elapsed_duration = Duration::from_millis(1); + let logs_config = LogsConfig { + enable_color: false, + enable_timestamp: false, + }; + let config = AdvanceRunnerConfig { server_manager_config, broker_config, @@ -97,6 +103,7 @@ impl AdvanceRunnerFixture { snapshot_config, backoff_max_elapsed_duration, healthcheck_port: 0, + logs_config, }; let handler = RefCell::new(Some(start_advance_runner(config.clone()))); Self { config, handler } diff --git a/offchain/authority-claimer/Cargo.toml b/offchain/authority-claimer/Cargo.toml index 8cdc8cb51..9359df594 100644 --- a/offchain/authority-claimer/Cargo.toml +++ b/offchain/authority-claimer/Cargo.toml @@ -12,9 +12,10 @@ test = false [dependencies] http-server = { path = "../http-server" } rollups-events = { path = "../rollups-events" } +logs = { path = "../logs" } async-trait.workspace = true -clap = { workspace = true, features = ["derive"] } +clap = { workspace = true, features = ["derive", "env"] } eth-tx-manager.workspace = true ethers.workspace = true rusoto_core.workspace = true diff --git a/offchain/authority-claimer/src/config/cli.rs b/offchain/authority-claimer/src/config/cli.rs index fe5bc790b..8cc7ce1dc 100644 --- a/offchain/authority-claimer/src/config/cli.rs +++ b/offchain/authority-claimer/src/config/cli.rs @@ -6,6 +6,7 @@ use eth_tx_manager::{ config::{TxEnvCLIConfig as TxManagerCLIConfig, TxManagerConfig}, Priority, }; +use logs::{LogsConfig, LogsEnvCliConfig}; use rollups_events::{BrokerCLIConfig, BrokerConfig}; use rusoto_core::Region; use snafu::ResultExt; @@ -37,6 +38,9 @@ pub(crate) struct AuthorityClaimerCLI { #[command(flatten)] broker_config: BrokerCLIConfig, + #[command(flatten)] + pub logs_config: LogsEnvCliConfig, + /// Path to a file with the deployment json of the dapp #[arg(long, env, default_value = "./dapp_deployment.json")] dapp_deployment_file: PathBuf, @@ -61,11 +65,14 @@ impl TryFrom for AuthorityClaimerConfig { let dapp_address = dapp_deployment.dapp_address; let dapp_deploy_block_hash = dapp_deployment.dapp_deploy_block_hash; + let logs_config = LogsConfig::initialize(cli_config.logs_config); + Ok(AuthorityClaimerConfig { tx_manager_config, tx_signing_config, tx_manager_priority: Priority::Normal, broker_config, + logs_config, dapp_address, dapp_deploy_block_hash, }) diff --git a/offchain/authority-claimer/src/config/mod.rs b/offchain/authority-claimer/src/config/mod.rs index 8c2e0a9ba..8141f5dcb 100644 --- a/offchain/authority-claimer/src/config/mod.rs +++ b/offchain/authority-claimer/src/config/mod.rs @@ -10,6 +10,7 @@ pub use error::{AuthorityClaimerConfigError, TxSigningConfigError}; use cli::AuthorityClaimerCLI; use eth_tx_manager::{config::TxManagerConfig, Priority}; use http_server::HttpServerConfig; +use logs::LogsConfig; use rollups_events::{Address, BrokerConfig, Hash}; use rusoto_core::Region; @@ -25,6 +26,7 @@ pub struct AuthorityClaimerConfig { pub tx_signing_config: TxSigningConfig, pub tx_manager_priority: Priority, pub broker_config: BrokerConfig, + pub logs_config: LogsConfig, pub dapp_address: Address, pub dapp_deploy_block_hash: Hash, } diff --git a/offchain/authority-claimer/src/main.rs b/offchain/authority-claimer/src/main.rs index 3febe0c7f..10214d1dd 100644 --- a/offchain/authority-claimer/src/main.rs +++ b/offchain/authority-claimer/src/main.rs @@ -3,18 +3,17 @@ use authority_claimer::config::Config; use std::error::Error; -use tracing_subscriber::filter::{EnvFilter, LevelFilter}; +use tracing::info; #[tokio::main] async fn main() -> Result<(), Box> { + // Getting the configuration. + let config: Config = Config::new().map_err(Box::new)?; + // Settin up the logging environment. - let env_filter = EnvFilter::builder() - .with_default_directive(LevelFilter::INFO.into()) - .from_env_lossy(); - tracing_subscriber::fmt().with_env_filter(env_filter).init(); + logs::initialize_logs(&config.authority_claimer_config.logs_config); - // Getting the configuration. - let config = Config::new().map_err(Box::new)?; + info!("Starting Authority Claimer"); authority_claimer::run(config).await } diff --git a/offchain/dispatcher/Cargo.toml b/offchain/dispatcher/Cargo.toml index a5d966aa9..475912c3c 100644 --- a/offchain/dispatcher/Cargo.toml +++ b/offchain/dispatcher/Cargo.toml @@ -13,6 +13,7 @@ contracts = { path = "../contracts" } http-server = { path = "../http-server" } rollups-events = { path = "../rollups-events" } types = { path = "../types" } +logs = {path = "../logs"} async-trait.workspace = true axum.workspace = true diff --git a/offchain/dispatcher/src/config.rs b/offchain/dispatcher/src/config.rs index 58a363ced..ed27f8b75 100644 --- a/offchain/dispatcher/src/config.rs +++ b/offchain/dispatcher/src/config.rs @@ -10,6 +10,7 @@ use eth_tx_manager::{ Priority, }; use http_server::HttpServerConfig; +use logs::{LogsConfig, LogsEnvCliConfig}; use snafu::{ResultExt, Snafu}; use std::{fs::File, io::BufReader, path::PathBuf}; @@ -37,6 +38,9 @@ pub struct DispatcherEnvCLIConfig { #[command(flatten)] pub auth_config: AuthEnvCLIConfig, + #[command(flatten)] + pub logs_config: LogsEnvCliConfig, + /// Path to file with deployment json of dapp #[arg(long, env, default_value = "./dapp_deployment.json")] pub rd_dapp_deployment_file: PathBuf, @@ -56,6 +60,7 @@ pub struct DispatcherConfig { pub tx_config: TxManagerConfig, pub broker_config: BrokerConfig, pub auth_config: AuthConfig, + pub logs_config: LogsConfig, pub dapp_deployment: DappDeployment, pub rollups_deployment: RollupsDeployment, @@ -114,6 +119,8 @@ impl Config { let auth_config = AuthConfig::initialize(dispatcher_config.auth_config) .context(AuthSnafu)?; + let logs_config = LogsConfig::initialize(dispatcher_config.logs_config); + let path = dispatcher_config.rd_dapp_deployment_file; let dapp_deployment: DappDeployment = read_json(path)?; @@ -133,6 +140,7 @@ impl Config { tx_config, broker_config, auth_config, + logs_config, dapp_deployment, rollups_deployment, diff --git a/offchain/dispatcher/src/main.rs b/offchain/dispatcher/src/main.rs index 371007a4c..d6885be4b 100644 --- a/offchain/dispatcher/src/main.rs +++ b/offchain/dispatcher/src/main.rs @@ -1,17 +1,17 @@ // (c) Cartesi and individual authors (see AUTHORS) // SPDX-License-Identifier: Apache-2.0 (see LICENSE) -use tracing_subscriber::filter::{EnvFilter, LevelFilter}; +use logs; +use tracing::info; // NOTE: doesn't support History upgradability. // NOTE: doesn't support changing epoch_duration in the middle of things. #[tokio::main] async fn main() -> Result<(), Box> { - let filter = EnvFilter::builder() - .with_default_directive(LevelFilter::INFO.into()) - .from_env_lossy(); - tracing_subscriber::fmt().with_env_filter(filter).init(); - let config = dispatcher::config::Config::initialize()?; + + logs::initialize_logs(&config.dispatcher_config.logs_config); + + info!("Starting Dispatcher"); dispatcher::run(config).await.map_err(|e| e.into()) } diff --git a/offchain/graphql-server/Cargo.toml b/offchain/graphql-server/Cargo.toml index ab8cb870a..3482ec7bd 100644 --- a/offchain/graphql-server/Cargo.toml +++ b/offchain/graphql-server/Cargo.toml @@ -15,6 +15,7 @@ path = "src/schema/generate_schema.rs" [dependencies] http-health-check = { path = "../http-health-check" } rollups-data = { path = "../data" } +logs = { path = "../logs" } actix-cors.workspace = true actix-web.workspace = true diff --git a/offchain/graphql-server/src/config.rs b/offchain/graphql-server/src/config.rs index 71d7578a4..816a6180e 100644 --- a/offchain/graphql-server/src/config.rs +++ b/offchain/graphql-server/src/config.rs @@ -2,27 +2,32 @@ // SPDX-License-Identifier: Apache-2.0 (see LICENSE) use clap::Parser; +use logs::{LogsConfig, LogsEnvCliConfig}; use rollups_data::{RepositoryCLIConfig, RepositoryConfig}; #[derive(Debug)] pub struct GraphQLConfig { + pub repository_config: RepositoryConfig, + pub logs_config: LogsConfig, pub graphql_host: String, pub graphql_port: u16, - pub repository_config: RepositoryConfig, pub healthcheck_port: u16, } #[derive(Parser)] pub struct CLIConfig { + #[command(flatten)] + repository_config: RepositoryCLIConfig, + + #[command(flatten)] + pub logs_config: LogsEnvCliConfig, + #[arg(long, env, default_value = "127.0.0.1")] pub graphql_host: String, #[arg(long, env, default_value_t = 4000)] pub graphql_port: u16, - #[command(flatten)] - repository_config: RepositoryCLIConfig, - /// Port of health check #[arg(long, env = "GRAPHQL_HEALTHCHECK_PORT", default_value_t = 8080)] pub healthcheck_port: u16, @@ -31,9 +36,10 @@ pub struct CLIConfig { impl From for GraphQLConfig { fn from(cli_config: CLIConfig) -> Self { Self { + repository_config: cli_config.repository_config.into(), + logs_config: cli_config.logs_config.into(), graphql_host: cli_config.graphql_host, graphql_port: cli_config.graphql_port, - repository_config: cli_config.repository_config.into(), healthcheck_port: cli_config.healthcheck_port, } } diff --git a/offchain/graphql-server/src/main.rs b/offchain/graphql-server/src/main.rs index 93c06527c..c0e923e69 100644 --- a/offchain/graphql-server/src/main.rs +++ b/offchain/graphql-server/src/main.rs @@ -2,32 +2,17 @@ // SPDX-License-Identifier: Apache-2.0 (see LICENSE) use clap::Parser; -use tracing::level_filters::LevelFilter; -use tracing_subscriber::EnvFilter; -use graphql_server::CLIConfig; +use graphql_server::{CLIConfig, GraphQLConfig}; + +use tracing::info; #[actix_web::main] async fn main() -> Result<(), Box> { - let tracing_format = tracing_subscriber::fmt::format() - .without_time() - .with_level(true) - .with_target(true) - .with_ansi(false) - .compact(); - if std::env::var(EnvFilter::DEFAULT_ENV).is_ok() { - tracing_subscriber::fmt() - .event_format(tracing_format) - .with_env_filter(EnvFilter::from_default_env()) - .init(); - } else { - tracing_subscriber::fmt() - .event_format(tracing_format) - .with_max_level(LevelFilter::INFO) - .init(); - } + let config: GraphQLConfig = CLIConfig::parse().into(); - let config = CLIConfig::parse().into(); + logs::initialize_logs(&config.logs_config); + info!("Starting GraphQL Server"); graphql_server::run(config).await.map_err(|e| e.into()) } diff --git a/offchain/host-runner/Cargo.toml b/offchain/host-runner/Cargo.toml index 3d7eb949c..5ba125113 100644 --- a/offchain/host-runner/Cargo.toml +++ b/offchain/host-runner/Cargo.toml @@ -11,6 +11,7 @@ path = "src/main.rs" [dependencies] grpc-interfaces = { path = "../grpc-interfaces" } http-health-check = { path = "../http-health-check" } +logs = { path = "../logs" } actix-web.workspace = true async-trait.workspace = true diff --git a/offchain/host-runner/src/config.rs b/offchain/host-runner/src/config.rs index dbfc7ecfb..df6aad39c 100644 --- a/offchain/host-runner/src/config.rs +++ b/offchain/host-runner/src/config.rs @@ -2,11 +2,28 @@ // SPDX-License-Identifier: Apache-2.0 (see LICENSE) use clap::Parser; +use logs::{LogsConfig, LogsEnvCliConfig}; const DEFAULT_ADDRESS: &str = "0.0.0.0"; +#[derive(Debug, Clone)] +pub struct Config { + pub logs_config: LogsConfig, + pub grpc_server_manager_address: String, + pub grpc_server_manager_port: u16, + pub http_inspect_address: String, + pub http_inspect_port: u16, + pub http_rollup_server_address: String, + pub http_rollup_server_port: u16, + pub finish_timeout: u64, + pub healthcheck_port: u16, +} #[derive(Parser, Clone, Debug)] -pub struct Config { +pub struct CLIConfig { + /// Logs Config + #[command(flatten)] + pub logs_config: LogsEnvCliConfig, + /// gRPC address of the Server Manager endpoint #[arg(long, env, default_value = DEFAULT_ADDRESS)] pub grpc_server_manager_address: String, @@ -39,3 +56,19 @@ pub struct Config { #[arg(long, env = "HOST_RUNNER_HEALTHCHECK_PORT", default_value_t = 8080)] pub healthcheck_port: u16, } + +impl From for Config { + fn from(cli_config: CLIConfig) -> Self { + Self { + logs_config: cli_config.logs_config.into(), + grpc_server_manager_address: cli_config.grpc_server_manager_address, + grpc_server_manager_port: cli_config.grpc_server_manager_port, + http_inspect_address: cli_config.http_inspect_address, + http_inspect_port: cli_config.http_inspect_port, + http_rollup_server_address: cli_config.http_rollup_server_address, + http_rollup_server_port: cli_config.http_rollup_server_port, + finish_timeout: cli_config.finish_timeout, + healthcheck_port: cli_config.healthcheck_port, + } + } +} diff --git a/offchain/host-runner/src/main.rs b/offchain/host-runner/src/main.rs index 6c04a571e..4fb57c74a 100644 --- a/offchain/host-runner/src/main.rs +++ b/offchain/host-runner/src/main.rs @@ -12,14 +12,14 @@ mod merkle_tree; mod model; mod proofs; -use clap::Parser; use futures_util::FutureExt; use std::sync::{atomic::AtomicBool, atomic::Ordering, Arc}; use std::time::Duration; use tokio::sync::oneshot; -use tracing_subscriber::filter::{EnvFilter, LevelFilter}; +use tracing::info; -use config::Config; +use clap::Parser; +use config::{CLIConfig, Config}; use controller::Controller; fn log_result(name: &str, result: Result) { @@ -32,13 +32,12 @@ fn log_result(name: &str, result: Result) { #[actix_web::main] async fn main() { - let filter = EnvFilter::builder() - .with_default_directive(LevelFilter::INFO.into()) - .from_env_lossy(); - tracing_subscriber::fmt().with_env_filter(filter).init(); + let config: Config = CLIConfig::parse().into(); + + logs::initialize_logs(&config.logs_config); + info!("Starting Host Runner"); - let config = Config::parse(); - tracing::info!("{:#?}", config); + info!("{:#?}", config); let controller = Controller::new(Duration::from_millis(config.finish_timeout)); diff --git a/offchain/indexer/Cargo.toml b/offchain/indexer/Cargo.toml index 02cf7795d..920b03e29 100644 --- a/offchain/indexer/Cargo.toml +++ b/offchain/indexer/Cargo.toml @@ -13,6 +13,7 @@ test = false http-health-check = { path = "../http-health-check" } rollups-data = { path = "../data" } rollups-events = { path = "../rollups-events" } +logs = {path = "../logs"} chrono.workspace = true clap = { workspace = true, features = ["derive", "env"] } diff --git a/offchain/indexer/src/config.rs b/offchain/indexer/src/config.rs index a05d233aa..8cb0bdb4a 100644 --- a/offchain/indexer/src/config.rs +++ b/offchain/indexer/src/config.rs @@ -3,6 +3,7 @@ use clap::Parser; +use logs::{LogsConfig, LogsEnvCliConfig}; pub use rollups_data::{RepositoryCLIConfig, RepositoryConfig}; pub use rollups_events::{ BrokerCLIConfig, BrokerConfig, DAppMetadata, DAppMetadataCLIConfig, @@ -13,6 +14,7 @@ pub struct IndexerConfig { pub repository_config: RepositoryConfig, pub dapp_metadata: DAppMetadata, pub broker_config: BrokerConfig, + pub logs_config: LogsConfig, pub healthcheck_port: u16, } @@ -27,6 +29,9 @@ pub struct CLIConfig { #[command(flatten)] broker_config: BrokerCLIConfig, + #[command(flatten)] + pub logs_config: LogsEnvCliConfig, + /// Port of health check #[arg( long = "healthcheck-port", @@ -42,6 +47,7 @@ impl From for IndexerConfig { repository_config: cli_config.repository_config.into(), dapp_metadata: cli_config.dapp_metadata_config.into(), broker_config: cli_config.broker_config.into(), + logs_config: cli_config.logs_config.into(), healthcheck_port: cli_config.healthcheck_port, } } diff --git a/offchain/indexer/src/main.rs b/offchain/indexer/src/main.rs index d37bb93d3..a8c6e87ac 100644 --- a/offchain/indexer/src/main.rs +++ b/offchain/indexer/src/main.rs @@ -2,17 +2,17 @@ // SPDX-License-Identifier: Apache-2.0 (see LICENSE) use clap::Parser; -use tracing_subscriber::filter::{EnvFilter, LevelFilter}; -use indexer::CLIConfig; +use indexer::{CLIConfig, IndexerConfig}; +use logs; +use tracing::info; #[tokio::main] async fn main() -> Result<(), Box> { - let filter = EnvFilter::builder() - .with_default_directive(LevelFilter::INFO.into()) - .from_env_lossy(); - tracing_subscriber::fmt().with_env_filter(filter).init(); + let config: IndexerConfig = CLIConfig::parse().into(); - let config = CLIConfig::parse().into(); + logs::initialize_logs(&config.logs_config); + + info!("Starting Indexer"); indexer::run(config).await.map_err(|e| e.into()) } diff --git a/offchain/indexer/tests/integration.rs b/offchain/indexer/tests/integration.rs index d0b51960a..cd39c5529 100644 --- a/offchain/indexer/tests/integration.rs +++ b/offchain/indexer/tests/integration.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 (see LICENSE) use indexer::IndexerError; +use logs::LogsConfig; use rand::Rng; use rollups_data::{ Input, Notice, OutputEnum, Proof, Report, RepositoryConfig, Voucher, @@ -442,11 +443,16 @@ async fn spawn_indexer( consume_timeout: BROKER_CONSUME_TIMEOUT, backoff: Default::default(), }; + let logs_config = LogsConfig { + enable_timestamp: false, + enable_color: false, + }; let indexer_config = indexer::IndexerConfig { repository_config, dapp_metadata, broker_config, healthcheck_port: 0, + logs_config, }; tokio::spawn(async move { indexer::run(indexer_config).await.map_err(|e| { diff --git a/offchain/inspect-server/Cargo.toml b/offchain/inspect-server/Cargo.toml index 34dd15904..72f604f82 100644 --- a/offchain/inspect-server/Cargo.toml +++ b/offchain/inspect-server/Cargo.toml @@ -10,13 +10,14 @@ path = "src/main.rs" [dependencies] http-health-check = { path = "../http-health-check" } +logs = { path = "../logs" } + + actix-cors.workspace = true actix-web.workspace = true clap = { workspace = true, features = ["derive", "env"] } -env_logger.workspace = true hex.workspace = true -log.workspace = true prost.workspace = true regex.workspace = true serde = { workspace = true, features = ["rc", "derive"] } @@ -25,6 +26,7 @@ tokio = { workspace = true, features = ["macros", "time", "rt-multi-thread"] } toml.workspace = true tonic.workspace = true tracing.workspace = true +tracing-actix-web.workspace = true uuid = { workspace = true, features = ["v4"] } [dev-dependencies] diff --git a/offchain/inspect-server/src/config.rs b/offchain/inspect-server/src/config.rs index fd1612685..8d10223d5 100644 --- a/offchain/inspect-server/src/config.rs +++ b/offchain/inspect-server/src/config.rs @@ -6,6 +6,7 @@ /// Command-line parameters take precedence over environment variables and environment variables /// take precedence over same parameter from file configuration. use clap::Parser; +use logs::{LogsConfig, LogsEnvCliConfig}; use serde::Deserialize; use snafu::{whatever, ResultExt, Snafu}; @@ -26,6 +27,7 @@ pub enum ConfigError { } #[derive(Debug)] pub struct InspectServerConfig { + pub logs_config: LogsConfig, pub inspect_server_address: String, pub server_manager_address: String, pub session_id: String, @@ -36,6 +38,9 @@ pub struct InspectServerConfig { #[derive(Parser)] pub struct CLIConfig { + #[command(flatten)] + pub logs_config: LogsEnvCliConfig, + /// HTTP address for the inspect server #[arg(long, env)] inspect_server_address: Option, @@ -102,6 +107,7 @@ impl From for InspectServerConfig { .expect("invalid inspect path"); Self { + logs_config: cli_config.logs_config.into(), inspect_server_address, server_manager_address, session_id, diff --git a/offchain/inspect-server/src/inspect.rs b/offchain/inspect-server/src/inspect.rs index 76205ee91..45c9e9afa 100644 --- a/offchain/inspect-server/src/inspect.rs +++ b/offchain/inspect-server/src/inspect.rs @@ -46,7 +46,7 @@ impl InspectClient { message: e.to_string(), }); } else { - log::debug!("inspect request added to the queue"); + tracing::debug!("inspect request added to the queue"); } response_rx.await.expect("handle_inspect never fails") } @@ -62,7 +62,7 @@ fn respond( response: Result, ) { if response_tx.send(response).is_err() { - log::warn!("failed to respond inspect request (client dropped)"); + tracing::warn!("failed to respond inspect request (client dropped)"); } } @@ -90,7 +90,7 @@ async fn handle_inspect( query_payload: request.payload, }; - log::debug!( + tracing::debug!( "calling grpc inspect_state request={:?} request_id={}", grpc_request, request_id @@ -101,7 +101,7 @@ async fn handle_inspect( .insert("request-id", request_id.parse().unwrap()); let grpc_response = client.inspect_state(grpc_request).await; - log::debug!("got grpc response from inspect_state response={:?} request_id={}", grpc_response, request_id); + tracing::debug!("got grpc response from inspect_state response={:?} request_id={}", grpc_response, request_id); let response = grpc_response .map(|result| result.into_inner()) diff --git a/offchain/inspect-server/src/lib.rs b/offchain/inspect-server/src/lib.rs index f2a176a5b..f122410d7 100644 --- a/offchain/inspect-server/src/lib.rs +++ b/offchain/inspect-server/src/lib.rs @@ -15,7 +15,7 @@ pub mod server; #[tracing::instrument(level = "trace", skip_all)] pub async fn run(config: InspectServerConfig) -> Result<(), InspectError> { - log::info!("starting inspect server with {:?}", config); + tracing::info!("starting inspect server with {:?}", config); let health_handle = http_health_check::start(config.healthcheck_port); let inspect_client = InspectClient::new(&config); let inspect_server = diff --git a/offchain/inspect-server/src/main.rs b/offchain/inspect-server/src/main.rs index ea85624d7..2d84d763a 100644 --- a/offchain/inspect-server/src/main.rs +++ b/offchain/inspect-server/src/main.rs @@ -3,16 +3,16 @@ use clap::Parser; -use inspect_server::config::CLIConfig; +use inspect_server::{config::CLIConfig, InspectServerConfig}; +use tracing::info; #[tokio::main] async fn main() -> Result<(), Box> { - env_logger::Builder::from_env( - env_logger::Env::default().default_filter_or("info"), - ) - .target(env_logger::fmt::Target::Stdout) - .init(); - let config = CLIConfig::parse().into(); + let config: InspectServerConfig = CLIConfig::parse().into(); + + logs::initialize_logs(&config.logs_config); + + info!("Starting Inpect Server"); inspect_server::run(config).await.map_err(|e| e.into()) } diff --git a/offchain/inspect-server/src/server.rs b/offchain/inspect-server/src/server.rs index cf9cc85de..8829a46a4 100644 --- a/offchain/inspect-server/src/server.rs +++ b/offchain/inspect-server/src/server.rs @@ -3,10 +3,11 @@ use actix_cors::Cors; use actix_web::{ - dev::Server, error, middleware, web, App, HttpRequest, HttpResponse, - HttpServer, Responder, + dev::Server, error, web, App, HttpRequest, HttpResponse, HttpServer, + Responder, }; use serde::{Deserialize, Serialize}; +use tracing_actix_web::TracingLogger; use crate::config::InspectServerConfig; use crate::error::InspectError; @@ -23,7 +24,7 @@ pub fn create( let cors = Cors::permissive(); App::new() .app_data(web::Data::new(inspect_client.clone())) - .wrap(middleware::Logger::default()) + .wrap(TracingLogger::default()) .wrap(cors) .service( web::resource(inspect_path.clone()) @@ -89,7 +90,10 @@ fn convert_status(status: i32) -> String { } else if status == CompletionStatus::PayloadLengthLimitExceeded as i32 { String::from("PayloadLengthLimitExceeded") } else { - log::error!("Invalid status received from server-manager: {}", status); + tracing::error!( + "Invalid status received from server-manager: {}", + status + ); String::from("Unknown") } } @@ -113,7 +117,7 @@ fn hex_encode(payload: Vec) -> String { impl From for error::Error { fn from(e: InspectError) -> error::Error { - log::warn!("{}", e.to_string()); + tracing::warn!("{}", e.to_string()); match e { InspectError::FailedToConnect { .. } => { error::ErrorBadGateway(e.to_string()) diff --git a/offchain/inspect-server/tests/common/mod.rs b/offchain/inspect-server/tests/common/mod.rs index 5a68aed7b..c36c9915a 100644 --- a/offchain/inspect-server/tests/common/mod.rs +++ b/offchain/inspect-server/tests/common/mod.rs @@ -5,6 +5,7 @@ use actix_web::dev::ServerHandle; use inspect_server::config::InspectServerConfig; +use logs::LogsConfig; pub use reqwest::StatusCode; use std::sync::Arc; use tokio::sync::{oneshot, Notify}; @@ -40,10 +41,7 @@ pub struct TestState { impl TestState { /// Start the inspect-server and the mock-server-manager pub async fn setup(mock: impl MockInspect) -> Self { - let _ = env_logger::builder() - .filter_level(log::LevelFilter::Debug) - .is_test(true) - .try_init(); + logs::initialize_logs(&LogsConfig::default()); let server_manager = MockServerManagerWrapper::start(mock).await; let inspect_server = InspectServerWrapper::start().await; Self { @@ -88,6 +86,7 @@ impl InspectServerWrapper { queue_size: QUEUE_SIZE, inspect_path_prefix: String::from("/inspect"), healthcheck_port: 0, + logs_config: LogsConfig::default(), }; let inspect_client = InspectClient::new(&inspect_server_config); diff --git a/offchain/logs/Cargo.toml b/offchain/logs/Cargo.toml new file mode 100644 index 000000000..a9d0b9f65 --- /dev/null +++ b/offchain/logs/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "logs" +version = "1.0.0" +edition = "2021" +license = "Apache-2.0" + +[dependencies] +clap = { workspace = true, features = ["derive", "env"] } +tracing.workspace = true +tracing-subscriber = { workspace = true, features = ["env-filter"] } + diff --git a/offchain/logs/src/lib.rs b/offchain/logs/src/lib.rs new file mode 100644 index 000000000..c16e18aa0 --- /dev/null +++ b/offchain/logs/src/lib.rs @@ -0,0 +1,62 @@ +use clap::Parser; +use tracing_subscriber::filter::{EnvFilter, LevelFilter}; + +#[derive(Clone, Parser, Debug)] +#[command(name = "log_config")] +#[command(about = "Configuration for Logs")] +pub struct LogsEnvCliConfig { + #[arg(long, env, default_value = "false")] + pub logs_enable_timestamp: bool, + + #[arg(long, env, default_value = "false")] + pub logs_enable_color: bool, +} + +#[derive(Clone, Debug)] +pub struct LogsConfig { + pub enable_timestamp: bool, + pub enable_color: bool, +} + +impl LogsConfig { + pub fn initialize(env_cli_config: LogsEnvCliConfig) -> Self { + let enable_timestamp = env_cli_config.logs_enable_timestamp; + + let enable_color = env_cli_config.logs_enable_color; + + LogsConfig { + enable_timestamp, + enable_color, + } + } + + pub fn default() -> Self { + LogsConfig { + enable_timestamp: false, + enable_color: false, + } + } +} + +impl From for LogsConfig { + fn from(cli_config: LogsEnvCliConfig) -> LogsConfig { + LogsConfig::initialize(cli_config) + } +} + +pub fn initialize_logs(config: &LogsConfig) { + let filter = EnvFilter::builder() + .with_default_directive(LevelFilter::INFO.into()) + .from_env_lossy(); + + let subscribe_builder = tracing_subscriber::fmt() + .compact() + .with_env_filter(filter) + .with_ansi(config.enable_color); + + if !config.enable_timestamp { + subscribe_builder.without_time().init(); + } else { + subscribe_builder.init(); + } +} diff --git a/offchain/state-server/Cargo.toml b/offchain/state-server/Cargo.toml index 2c7bfe02c..37f1f9e4f 100644 --- a/offchain/state-server/Cargo.toml +++ b/offchain/state-server/Cargo.toml @@ -9,8 +9,9 @@ name = "cartesi-rollups-state-server" path = "src/main.rs" [dependencies] +logs = { path = "../logs" } types = { path = "../types" } - +clap = { workspace = true, features = ["derive", "env"] } eth-block-history.workspace = true eth-state-fold-types.workspace = true eth-state-fold.workspace = true diff --git a/offchain/state-server/src/config.rs b/offchain/state-server/src/config.rs new file mode 100644 index 000000000..382aaab23 --- /dev/null +++ b/offchain/state-server/src/config.rs @@ -0,0 +1,40 @@ +use clap::Parser; +use logs::{LogsConfig, LogsEnvCliConfig}; +use state_server_lib::config::{ + Result, StateServerConfig, StateServerEnvCLIConfig, +}; + +#[derive(Debug, Clone, Parser)] +#[command(name = "sate_server_config")] +#[command(about = "Configuration for state-fold state-server")] +pub struct EnvCLIConfig { + #[command(flatten)] + pub state_server_config: StateServerEnvCLIConfig, + + #[command(flatten)] + pub logs_config: LogsEnvCliConfig, +} + +#[derive(Debug, Clone)] +pub struct Config { + pub state_server_config: StateServerConfig, + pub logs_config: LogsConfig, +} + +impl Config { + pub fn initialize(env_cli_config: EnvCLIConfig) -> Result { + let state_server_config = + StateServerConfig::initialize(env_cli_config.state_server_config); + let logs_config = LogsConfig::initialize(env_cli_config.logs_config); + + Ok(Self { + state_server_config: state_server_config?, + logs_config: logs_config, + }) + } + + pub fn initialize_from_args() -> Result { + let env_cli_config = EnvCLIConfig::parse(); + Self::initialize(env_cli_config) + } +} diff --git a/offchain/state-server/src/main.rs b/offchain/state-server/src/main.rs index cd6216f7a..9eaffd8a8 100644 --- a/offchain/state-server/src/main.rs +++ b/offchain/state-server/src/main.rs @@ -1,22 +1,19 @@ // (c) Cartesi and individual authors (see AUTHORS) // SPDX-License-Identifier: Apache-2.0 (see LICENSE) - use eth_state_server_lib::config::StateServerConfig; -use tracing_subscriber::filter::{EnvFilter, LevelFilter}; +mod config; +use config::Config; use types::foldables::authority::rollups::RollupsState; #[tokio::main] async fn main() -> Result<(), Box> { - let filter = EnvFilter::builder() - .with_default_directive(LevelFilter::INFO.into()) - .from_env_lossy(); - tracing_subscriber::fmt().with_env_filter(filter).init(); + let config: Config = Config::initialize_from_args()?; - let config = StateServerConfig::initialize_from_args()?; + logs::initialize_logs(&config.logs_config); - tracing::info!(?config, "starting state server"); + tracing::info!(?config, "Starting State Server"); - state_server::run_server::(config) + state_server::run_server::(config.state_server_config) .await .map_err(|e| e.into()) }