diff --git a/Cargo.lock b/Cargo.lock index 4bbfe31..ca4192a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6948,6 +6948,16 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "tracing-serde" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc6b213177105856957181934e4920de57730fc69bf42c37ee5bb664d406d9e1" +dependencies = [ + "serde", + "tracing-core", +] + [[package]] name = "tracing-subscriber" version = "0.3.18" @@ -6958,12 +6968,15 @@ dependencies = [ "nu-ansi-term", "once_cell", "regex", + "serde", + "serde_json", "sharded-slab", "smallvec", "thread_local", "tracing", "tracing-core", "tracing-log 0.2.0", + "tracing-serde", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 514c1dd..a5b2d62 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ bytes = "1.0" time = "0.3" tracing-subscriber = { version = "0.3.17", features = [ "fmt", + "json", "std", "env-filter", ] } diff --git a/config/Settings.example.toml b/config/Settings.example.toml index 2b46e51..8e28bff 100644 --- a/config/Settings.example.toml +++ b/config/Settings.example.toml @@ -1,4 +1,5 @@ log_level = "info" +log_format = "pretty" network = "public-testnet-15" [database] diff --git a/contrib/helm/indexer-values.yaml b/contrib/helm/indexer-values.yaml index 8284d01..159d04c 100644 --- a/contrib/helm/indexer-values.yaml +++ b/contrib/helm/indexer-values.yaml @@ -64,6 +64,7 @@ configMap: settings: Settings.toml: | log_level = "info" + log_format = "json" network = "public-testnet-14" [database] diff --git a/contrib/helm/server-values.yaml b/contrib/helm/server-values.yaml index 8990955..21a7d73 100644 --- a/contrib/helm/server-values.yaml +++ b/contrib/helm/server-values.yaml @@ -65,6 +65,7 @@ configMap: settings: Settings.toml: | log_level = "info" + log_format = "json" network = "public-testnet-14" [database] diff --git a/docs/03-indexer.md b/docs/03-indexer.md index 6af8afa..92d3f93 100644 --- a/docs/03-indexer.md +++ b/docs/03-indexer.md @@ -8,8 +8,9 @@ The indexer must have the following configuration: Settings.toml ```toml -# Level of logging in the indexer +# Level and format of logging in the indexer log_level = "info" +log_format = "pretty" # optional; either "pretty" or "json" network = "public-testnet-15" # IMPORANT! Do not use `.` just put the name of the network and don't have the hash (e.g 'shielded-expedition.b40d8e9055' becomes 'shielded-expedition') # Connection information for the PostgreSQL database diff --git a/docs/04-server.md b/docs/04-server.md index ea95f5f..36ead45 100644 --- a/docs/04-server.md +++ b/docs/04-server.md @@ -8,6 +8,7 @@ Settings.toml ```toml log_level = "info" +log_format = "pretty" # optional; either "pretty" or "json" network = "public-testnet-15" # IMPORANT! Do not use `.` just put the name of the network and don't have the hash (e.g 'shielded-expedition.b40d8e9055' becomes 'shielded-expedition') # Connection information for the PostgreSQL database diff --git a/src/config.rs b/src/config.rs index 0f4acd4..3ae4b01 100644 --- a/src/config.rs +++ b/src/config.rs @@ -20,6 +20,8 @@ pub const PROMETHEUS_PORT: u16 = 9000; pub const DEFAULT_NETWORK: &str = "public-testnet-15"; +pub const DEFAULT_LOG_FORMAT: &str = "pretty"; + #[derive(Debug, Deserialize)] pub struct IndexerConfig { pub tendermint_addr: String, @@ -125,10 +127,20 @@ impl Default for DatabaseConfig { } } +#[derive(Debug, Default, Deserialize, Clone, clap::ValueEnum)] +#[serde(rename_all = "kebab-case")] +pub enum LogFormat { + Json, + #[default] + Pretty, +} + #[derive(Debug, Deserialize, clap::Parser)] pub struct CliSettings { #[clap(long, env, default_value = "")] pub log_level: String, + #[clap(long, env, default_value = DEFAULT_LOG_FORMAT)] + pub log_format: LogFormat, #[clap(long, env, default_value = DEFAULT_NETWORK)] pub network: String, #[clap(long, env, default_value = SERVER_ADDR)] @@ -166,6 +178,8 @@ pub struct CliSettings { #[derive(Debug, Deserialize)] pub struct Settings { pub log_level: String, + #[serde(default)] + pub log_format: LogFormat, pub network: String, pub database: DatabaseConfig, pub server: ServerConfig, @@ -178,6 +192,7 @@ impl Default for Settings { fn default() -> Self { Self { log_level: Default::default(), + log_format: Default::default(), network: DEFAULT_NETWORK.to_string(), database: Default::default(), server: Default::default(), @@ -192,6 +207,7 @@ impl From for Settings { fn from(value: CliSettings) -> Self { Self { log_level: value.log_level, + log_format: value.log_format, network: value.network, database: DatabaseConfig { host: value.database_host, diff --git a/src/lib.rs b/src/lib.rs index 68c33a1..4464335 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,9 @@ pub mod tables; mod telemetry; pub mod utils; -pub use crate::config::{IndexerConfig, JaegerConfig, PrometheusConfig, ServerConfig, Settings}; +pub use crate::config::{ + IndexerConfig, JaegerConfig, LogFormat, PrometheusConfig, ServerConfig, Settings, +}; pub use database::Database; pub use error::Error; pub use indexer::start_indexing; diff --git a/src/telemetry.rs b/src/telemetry.rs index 9688f49..8fcb194 100644 --- a/src/telemetry.rs +++ b/src/telemetry.rs @@ -3,7 +3,7 @@ use tracing_subscriber::Layer; use tracing_subscriber::{filter::filter_fn, layer::SubscriberExt, EnvFilter, Registry}; -use crate::{JaegerConfig, Settings}; +use crate::{JaegerConfig, LogFormat, Settings}; use opentelemetry_api::global; /// Setup looging this includes a fmt::layer and jaeger(if enable) @@ -25,8 +25,11 @@ pub fn get_subscriber(cfg: &Settings) -> impl Subscriber + Send + Sync { let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(&cfg.log_level)); - // send any logs to stdout - let std_out = tracing_subscriber::fmt::layer().pretty(); + // send logs to stdout in the configured format + let (json, pretty) = match cfg.log_format { + LogFormat::Json => (Some(tracing_subscriber::fmt::layer().json()), None), + LogFormat::Pretty => (None, Some(tracing_subscriber::fmt::layer().pretty())), + }; // check if jaeger telemetry is enable and configure it accordingly let jaeger_cfg = cfg.jaeger_config(); @@ -37,7 +40,8 @@ pub fn get_subscriber(cfg: &Settings) -> impl Subscriber + Send + Sync { Registry::default() .with(env_filter) - .with(std_out) + .with(json) + .with(pretty) .with(jaeger) }