Skip to content

Commit

Permalink
feat: add json logging with LOG_FORMAT option (#173)
Browse files Browse the repository at this point in the history
* feat: add json logging with LOG_FORMAT option

* refactor: make common log config into a shared struct
  • Loading branch information
joel-u410 authored Dec 4, 2024
1 parent 5ec3619 commit 838301d
Show file tree
Hide file tree
Showing 24 changed files with 82 additions and 150 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ tower-http = { version = "0.5.0", features = [
] }
tower-layer = "0.3.2"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] }
serde = { version = "1.0.138", features = ["derive"] }
serde_json = "1.0"
clap = { version = "4.4.2", features = ["derive", "env"] }
Expand Down
2 changes: 0 additions & 2 deletions chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ test_helpers.workspace = true
tokio.workspace = true
tokio-retry.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
serde_json.workspace = true
chrono.workspace = true
clap.workspace = true
Expand All @@ -32,7 +31,6 @@ shared.workspace = true
deadpool-diesel.workspace = true
diesel.workspace = true
orm.workspace = true
clap-verbosity-flag.workspace = true
futures.workspace = true

[build-dependencies]
Expand Down
6 changes: 3 additions & 3 deletions chain/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::fmt;
use std::fmt::Display;

use clap_verbosity_flag::{InfoLevel, Verbosity};
use shared::log_config::LogConfig;

#[derive(clap::ValueEnum, Clone, Debug, Copy)]
pub enum CargoEnv {
Expand Down Expand Up @@ -34,6 +34,6 @@ pub struct AppConfig {
#[clap(long, env, default_value = "5")]
pub initial_query_retry_attempts: usize,

#[command(flatten)]
pub verbosity: Verbosity<InfoLevel>,
#[clap(flatten)]
pub log: LogConfig,
}
17 changes: 1 addition & 16 deletions chain/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use chain::services::{
};
use chrono::{NaiveDateTime, Utc};
use clap::Parser;
use clap_verbosity_flag::LevelFilter;
use deadpool_diesel::postgres::Object;
use namada_sdk::time::DateTimeUtc;
use orm::migrations::run_migrations;
Expand All @@ -31,8 +30,6 @@ use shared::validator::ValidatorSet;
use tendermint_rpc::HttpClient;
use tokio_retry::strategy::{jitter, ExponentialBackoff};
use tokio_retry::Retry;
use tracing::Level;
use tracing_subscriber::FmtSubscriber;

#[tokio::main]
async fn main() -> Result<(), MainError> {
Expand All @@ -50,19 +47,7 @@ async fn main() -> Result<(), MainError> {
checksums.add(code_path, code.to_lowercase());
}

let log_level = match config.verbosity.log_level_filter() {
LevelFilter::Off => None,
LevelFilter::Error => Some(Level::ERROR),
LevelFilter::Warn => Some(Level::WARN),
LevelFilter::Info => Some(Level::INFO),
LevelFilter::Debug => Some(Level::DEBUG),
LevelFilter::Trace => Some(Level::TRACE),
};
if let Some(log_level) = log_level {
let subscriber =
FmtSubscriber::builder().with_max_level(log_level).finish();
tracing::subscriber::set_global_default(subscriber).unwrap();
}
config.log.init();

let client = Arc::new(client);

Expand Down
2 changes: 0 additions & 2 deletions governance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ path = "src/main.rs"
[dependencies]
tokio.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
chrono.workspace = true
clap.workspace = true
anyhow.workspace = true
Expand All @@ -27,7 +26,6 @@ futures.workspace = true
deadpool-diesel.workspace = true
diesel.workspace = true
orm.workspace = true
clap-verbosity-flag.workspace = true
tokio-retry.workspace = true

[build-dependencies]
Expand Down
6 changes: 3 additions & 3 deletions governance/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::fmt;
use std::fmt::Display;

use clap_verbosity_flag::{InfoLevel, Verbosity};
use shared::log_config::LogConfig;

#[derive(clap::ValueEnum, Clone, Debug, Copy)]
pub enum CargoEnv {
Expand All @@ -26,6 +26,6 @@ pub struct AppConfig {
#[clap(long, env)]
pub database_url: String,

#[command(flatten)]
pub verbosity: Verbosity<InfoLevel>,
#[clap(flatten)]
pub log: LogConfig,
}
18 changes: 1 addition & 17 deletions governance/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::time::Duration;

use chrono::{NaiveDateTime, Utc};
use clap::Parser;
use clap_verbosity_flag::LevelFilter;
use deadpool_diesel::postgres::Object;
use governance::config::AppConfig;
use governance::repository;
Expand All @@ -18,27 +17,12 @@ use shared::error::{AsDbError, AsRpcError, ContextDbInteractError, MainError};
use tendermint_rpc::HttpClient;
use tokio::sync::{Mutex, MutexGuard};
use tokio::time::Instant;
use tracing::Level;
use tracing_subscriber::FmtSubscriber;

#[tokio::main]
async fn main() -> Result<(), MainError> {
let config = AppConfig::parse();

let log_level = match config.verbosity.log_level_filter() {
LevelFilter::Off => None,
LevelFilter::Error => Some(Level::ERROR),
LevelFilter::Warn => Some(Level::WARN),
LevelFilter::Info => Some(Level::INFO),
LevelFilter::Debug => Some(Level::DEBUG),
LevelFilter::Trace => Some(Level::TRACE),
};

if let Some(log_level) = log_level {
let subscriber =
FmtSubscriber::builder().with_max_level(log_level).finish();
tracing::subscriber::set_global_default(subscriber).unwrap();
}
config.log.init();

tracing::info!("version: {}", env!("VERGEN_GIT_SHA").to_string());

Expand Down
2 changes: 0 additions & 2 deletions parameters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ path = "src/main.rs"
[dependencies]
tokio.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
chrono.workspace = true
clap.workspace = true
anyhow.workspace = true
Expand All @@ -30,7 +29,6 @@ deadpool-diesel.workspace = true
diesel.workspace = true
diesel_migrations.workspace = true
orm.workspace = true
clap-verbosity-flag.workspace = true
tokio-retry.workspace = true
smooth-operator.workspace = true

Expand Down
8 changes: 4 additions & 4 deletions parameters/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::fmt;
use std::fmt::Display;

use clap_verbosity_flag::{InfoLevel, Verbosity};
use shared::log_config::LogConfig;

#[derive(clap::ValueEnum, Clone, Debug, Copy)]
pub enum CargoEnv {
Expand All @@ -23,9 +23,9 @@ pub struct AppConfig {
#[clap(long, env)]
pub database_url: String,

#[command(flatten)]
pub verbosity: Verbosity<InfoLevel>,

#[clap(long, env, default_value_t = 30)]
pub sleep_for: u64,

#[clap(flatten)]
pub log: LogConfig,
}
17 changes: 1 addition & 16 deletions parameters/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::time::Duration;

use chrono::NaiveDateTime;
use clap::Parser;
use clap_verbosity_flag::LevelFilter;
use deadpool_diesel::postgres::Object;
use namada_sdk::state::EPOCH_SWITCH_BLOCKS_DELAY;
use namada_sdk::time::{DateTimeUtc, Utc};
Expand All @@ -23,26 +22,12 @@ use shared::error::{AsDbError, AsRpcError, ContextDbInteractError, MainError};
use tendermint_rpc::HttpClient;
use tokio::sync::{Mutex, MutexGuard};
use tokio::time::Instant;
use tracing::Level;
use tracing_subscriber::FmtSubscriber;

#[tokio::main]
async fn main() -> Result<(), MainError> {
let config = AppConfig::parse();

let log_level = match config.verbosity.log_level_filter() {
LevelFilter::Off => None,
LevelFilter::Error => Some(Level::ERROR),
LevelFilter::Warn => Some(Level::WARN),
LevelFilter::Info => Some(Level::INFO),
LevelFilter::Debug => Some(Level::DEBUG),
LevelFilter::Trace => Some(Level::TRACE),
};
if let Some(log_level) = log_level {
let subscriber =
FmtSubscriber::builder().with_max_level(log_level).finish();
tracing::subscriber::set_global_default(subscriber).unwrap();
}
config.log.init();

let client =
Arc::new(HttpClient::new(config.tendermint_url.as_str()).unwrap());
Expand Down
2 changes: 0 additions & 2 deletions pos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ path = "src/main.rs"
[dependencies]
tokio.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
chrono.workspace = true
clap.workspace = true
anyhow.workspace = true
Expand All @@ -28,7 +27,6 @@ deadpool-diesel.workspace = true
diesel.workspace = true
diesel_migrations.workspace = true
orm.workspace = true
clap-verbosity-flag.workspace = true

[build-dependencies]
vergen = { version = "8.0.0", features = ["build", "git", "gitcl"] }
6 changes: 3 additions & 3 deletions pos/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::fmt;
use std::fmt::Display;

use clap_verbosity_flag::{InfoLevel, Verbosity};
use shared::log_config::LogConfig;

#[derive(clap::ValueEnum, Clone, Debug, Copy)]
pub enum CargoEnv {
Expand All @@ -23,6 +23,6 @@ pub struct AppConfig {
#[clap(long, env)]
pub database_url: String,

#[command(flatten)]
pub verbosity: Verbosity<InfoLevel>,
#[clap(flatten)]
pub log: LogConfig,
}
17 changes: 1 addition & 16 deletions pos/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::sync::Arc;

use chrono::{NaiveDateTime, Utc};
use clap::Parser;
use clap_verbosity_flag::LevelFilter;
use deadpool_diesel::postgres::Object;
use namada_sdk::time::DateTimeUtc;
use orm::crawler_state::EpochStateInsertDb;
Expand All @@ -17,26 +16,12 @@ use shared::crawler;
use shared::crawler_state::{CrawlerName, EpochCrawlerState};
use shared::error::{AsDbError, AsRpcError, ContextDbInteractError, MainError};
use tendermint_rpc::HttpClient;
use tracing::Level;
use tracing_subscriber::FmtSubscriber;

#[tokio::main]
async fn main() -> Result<(), MainError> {
let config = AppConfig::parse();

let log_level = match config.verbosity.log_level_filter() {
LevelFilter::Off => None,
LevelFilter::Error => Some(Level::ERROR),
LevelFilter::Warn => Some(Level::WARN),
LevelFilter::Info => Some(Level::INFO),
LevelFilter::Debug => Some(Level::DEBUG),
LevelFilter::Trace => Some(Level::TRACE),
};
if let Some(log_level) = log_level {
let subscriber =
FmtSubscriber::builder().with_max_level(log_level).finish();
tracing::subscriber::set_global_default(subscriber).unwrap();
}
config.log.init();

let client =
Arc::new(HttpClient::new(config.tendermint_url.as_str()).unwrap());
Expand Down
6 changes: 3 additions & 3 deletions rewards/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::fmt;
use std::fmt::Display;

use clap_verbosity_flag::{InfoLevel, Verbosity};
use shared::log_config::LogConfig;

#[derive(clap::ValueEnum, Clone, Debug, Copy)]
pub enum CargoEnv {
Expand All @@ -26,6 +26,6 @@ pub struct AppConfig {
#[clap(long, env)]
pub database_url: String,

#[command(flatten)]
pub verbosity: Verbosity<InfoLevel>,
#[clap(flatten)]
pub log: LogConfig,
}
18 changes: 1 addition & 17 deletions rewards/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::time::Duration;

use chrono::NaiveDateTime;
use clap::Parser;
use clap_verbosity_flag::LevelFilter;
use deadpool_diesel::postgres::Object;
use namada_sdk::time::{DateTimeUtc, Utc};
use orm::migrations::run_migrations;
Expand All @@ -17,27 +16,12 @@ use shared::crawler_state::{CrawlerName, IntervalCrawlerState};
use shared::error::{AsDbError, AsRpcError, ContextDbInteractError, MainError};
use tendermint_rpc::HttpClient;
use tokio::time::sleep;
use tracing::Level;
use tracing_subscriber::FmtSubscriber;

#[tokio::main]
async fn main() -> Result<(), MainError> {
let config = AppConfig::parse();

let log_level = match config.verbosity.log_level_filter() {
LevelFilter::Off => None,
LevelFilter::Error => Some(Level::ERROR),
LevelFilter::Warn => Some(Level::WARN),
LevelFilter::Info => Some(Level::INFO),
LevelFilter::Debug => Some(Level::DEBUG),
LevelFilter::Trace => Some(Level::TRACE),
};

if let Some(log_level) = log_level {
let subscriber =
FmtSubscriber::builder().with_max_level(log_level).finish();
tracing::subscriber::set_global_default(subscriber).unwrap();
}
config.log.init();

tracing::info!("version: {}", env!("VERGEN_GIT_SHA").to_string());

Expand Down
2 changes: 0 additions & 2 deletions seeder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ path = "src/main.rs"
[dependencies]
tokio.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
clap.workspace = true
anyhow.workspace = true
shared.workspace = true
deadpool-diesel.workspace = true
diesel.workspace = true
orm.workspace = true
clap-verbosity-flag.workspace = true
rand.workspace = true

[build-dependencies]
Expand Down
6 changes: 3 additions & 3 deletions seeder/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::fmt;
use std::fmt::Display;

use clap_verbosity_flag::{InfoLevel, Verbosity};
use shared::log_config::LogConfig;

#[derive(clap::ValueEnum, Clone, Debug, Copy)]
pub enum CargoEnv {
Expand Down Expand Up @@ -41,6 +41,6 @@ pub struct AppConfig {
#[clap(long, env, default_value_t = 10)]
pub total_balances: u64,

#[command(flatten)]
pub verbosity: Verbosity<InfoLevel>,
#[clap(flatten)]
pub log: LogConfig,
}
Loading

0 comments on commit 838301d

Please sign in to comment.