Skip to content

Commit

Permalink
Add json logging via tracing_subscriber
Browse files Browse the repository at this point in the history
  • Loading branch information
Ancient123 committed Oct 10, 2024
1 parent 85e660a commit 38c4e3e
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 39 deletions.
143 changes: 108 additions & 35 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ anyhow = "1.0"
async-trait = "0.1"
clap = { version = "4.4", features = ["derive"] }
dashmap = { version = "5.5", features = ["serde"] }
env_logger = "0.11"
hex = "0.4"
r2d2 = "0.8"
redis = { version = "0.24", features = ["r2d2", "async-std"] }
Expand All @@ -21,3 +20,4 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["std"] }
sha1 = "0.10"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter", "std", "json", "fmt"] }
16 changes: 13 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use actix_web::{error, web, App, Error, HttpResponse, HttpServer};
use anyhow::Context;
use cache::{memory_backend, CacheBackendFactory};
use clap::Parser;
use env_logger::Env;
use reqwest::Url;
use serde::Serialize;
use serde_json::{json, Value};
use tracing_subscriber::EnvFilter;

use crate::args::Args;
use crate::cache::redis_backend::RedisBackendFactory;
Expand Down Expand Up @@ -296,10 +296,20 @@ fn extract_single_request_info(

#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init_from_env(Env::default().default_filter_or("info"));

let args = Args::parse();

// Initialize tracing
if std::env::var("RUST_LOG_FORMAT") == Ok("json".to_string()) {
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.json()
.init();
} else {
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.init();
}

let mut app_state = AppState {
chains: Default::default(),
http_client: reqwest::Client::new(),
Expand Down

0 comments on commit 38c4e3e

Please sign in to comment.