Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Add support for json logging. (#138)
Browse files Browse the repository at this point in the history
This introduces a new _optional_ config option `log_format` which can be
either `"pretty"` (the default) or `"json"`.
  • Loading branch information
joel-u410 authored Feb 19, 2024
1 parent 160d159 commit 7d52663
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 6 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ bytes = "1.0"
time = "0.3"
tracing-subscriber = { version = "0.3.17", features = [
"fmt",
"json",
"std",
"env-filter",
] }
Expand Down
1 change: 1 addition & 0 deletions config/Settings.example.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
log_level = "info"
log_format = "pretty"
network = "public-testnet-15"

[database]
Expand Down
1 change: 1 addition & 0 deletions contrib/helm/indexer-values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ configMap:
settings:
Settings.toml: |
log_level = "info"
log_format = "json"
network = "public-testnet-14"
[database]
Expand Down
1 change: 1 addition & 0 deletions contrib/helm/server-values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ configMap:
settings:
Settings.toml: |
log_level = "info"
log_format = "json"
network = "public-testnet-14"
[database]
Expand Down
3 changes: 2 additions & 1 deletion docs/03-indexer.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/04-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
Expand All @@ -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(),
Expand All @@ -192,6 +207,7 @@ impl From<CliSettings> 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,
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 8 additions & 4 deletions src/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();
Expand All @@ -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)
}

Expand Down

0 comments on commit 7d52663

Please sign in to comment.