Skip to content

Commit

Permalink
feat: add toggleable JSON logs formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospb19-cw committed May 10, 2024
1 parent 45e2604 commit 5f1d0a8
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions docker/Dockerfile.importer_online_postgres
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ RUN apt update
RUN apt-get install -y libclang-dev cmake

ENV CARGO_PROFILE_RELEASE_DEBUG=1
ENV JSON_LOGS=1
RUN cargo build --release --bin importer-online --features metrics

# Runtime
Expand Down
1 change: 1 addition & 0 deletions docker/Dockerfile.run_stratus
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ RUN apt update
RUN apt-get install -y libclang-dev cmake

ENV CARGO_PROFILE_RELEASE_DEBUG=1
ENV JSON_LOGS=1
RUN cargo build --release --features metrics

# Runtime
Expand Down
1 change: 1 addition & 0 deletions docker/Dockerfile.run_with_importer
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ RUN apt update
RUN apt-get install -y libclang-dev cmake

ENV CARGO_PROFILE_RELEASE_DEBUG=1
ENV JSON_LOGS=1
RUN cargo build --release --bin run-with-importer --features metrics,rocks

# Runtime
Expand Down
1 change: 1 addition & 0 deletions docker/Dockerfile.run_with_importer_postgres
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ RUN apt update
RUN apt-get install -y libclang-dev cmake

ENV CARGO_PROFILE_RELEASE_DEBUG=1
ENV JSON_LOGS=1
RUN cargo build --release --bin run-with-importer --features metrics

# Runtime
Expand Down
20 changes: 13 additions & 7 deletions src/infra/tracing.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Tracing services.
use std::env;

use opentelemetry::KeyValue;
use opentelemetry_otlp::WithExportConfig;
use opentelemetry_sdk::runtime;
Expand All @@ -24,19 +26,23 @@ pub fn init_tracing(url: Option<&String>) {

fn init_stdout_tracing() {
// if tracing level not configured, set default
if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "stratus=debug");
if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", "stratus=debug");
}

tracing_subscriber::fmt()
.compact()
let subscriber = tracing_subscriber::fmt()
.with_target(false)
.with_thread_ids(true)
.with_thread_names(true)
.with_ansi(false)
.with_env_filter(EnvFilter::from_default_env())
.try_init()
.expect("failed to start tracing");
.with_env_filter(EnvFilter::from_default_env());

let json_logs = env::var_os("JSON_LOGS").is_some();
if json_logs {
subscriber.json().init();
} else {
subscriber.compact().init();
}
}

// Only works if run inside the tokio runtime
Expand Down

0 comments on commit 5f1d0a8

Please sign in to comment.