-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Makes JSON logging depend on the setting of a bool.
- Loading branch information
Showing
2 changed files
with
26 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,19 @@ | ||
use std::env; | ||
|
||
use tracing_subscriber::{fmt::format::FmtSpan, EnvFilter}; | ||
|
||
fn get_env() -> &'static str { | ||
let env_str = std::env::var("ENV"); | ||
match env_str { | ||
Err(env::VarError::NotPresent) => { | ||
println!("no ENV in env, assuming dev"); | ||
"dev" | ||
} | ||
Ok(str) => match str.as_ref() { | ||
"dev" => "dev", | ||
"development" => "dev", | ||
"stag" => "stag", | ||
"staging" => "stag", | ||
"prod" => "prod", | ||
"production" => "prod", | ||
_ => { | ||
panic!("ENV present: {str}, but not one of dev, stag, prod, panicking!") | ||
} | ||
}, | ||
err => { | ||
panic!("error getting ENV from env: {:?}", err) | ||
} | ||
} | ||
} | ||
use crate::env::ENV_CONFIG; | ||
|
||
pub fn init() { | ||
// We want to load log before env, we can't use our env module here. | ||
let log_perf = std::env::var("LOG_PERF") | ||
.map(|s| s == "true" || s == "1") | ||
.unwrap_or(false); | ||
let builder = tracing_subscriber::fmt().with_env_filter(EnvFilter::from_default_env()); | ||
|
||
let span_format = if log_perf { | ||
FmtSpan::CLOSE | ||
let builder = if ENV_CONFIG.log_perf { | ||
builder.with_span_events(FmtSpan::CLOSE) | ||
} else { | ||
FmtSpan::NONE | ||
builder | ||
}; | ||
|
||
if get_env() == "dev" { | ||
tracing_subscriber::fmt() | ||
.with_span_events(span_format) | ||
.with_env_filter(EnvFilter::from_default_env()) | ||
.init(); | ||
if ENV_CONFIG.log_json { | ||
builder.json().init(); | ||
} else { | ||
tracing_subscriber::fmt() | ||
.with_span_events(span_format) | ||
.with_env_filter(EnvFilter::from_default_env()) | ||
.json() | ||
.init(); | ||
} | ||
builder.init(); | ||
}; | ||
} |