Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add json logging via tracing_subscriber #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 111 additions & 38 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