diff --git a/src/env.rs b/src/env.rs index 8829955..9bca058 100644 --- a/src/env.rs +++ b/src/env.rs @@ -76,13 +76,22 @@ pub fn get_env() -> Env { } } -pub fn get_env_bool(key: &str) -> bool { - get_env_var(key).map_or(false, |var| var.to_lowercase() == "true") +pub fn get_env_bool(key: &str) -> Option { + get_env_var(key).map(|var| match var.to_lowercase().as_str() { + "true" => true, + "false" => false, + "t" => true, + "f" => false, + "1" => true, + "0" => false, + str => panic!("invalid bool {str} for {key}"), + }) } #[derive(Debug, Clone)] pub struct EnvConfig { pub env: Env, + pub log_json: bool, pub log_perf: bool, pub s3_bucket: String, } @@ -90,7 +99,8 @@ pub struct EnvConfig { fn get_env_config() -> EnvConfig { EnvConfig { env: get_env(), - log_perf: get_env_bool("LOG_PERF"), + log_json: get_env_bool("LOG_JSON").unwrap_or(get_env() != Env::Dev), + log_perf: get_env_bool("LOG_PERF").unwrap_or(false), s3_bucket: get_env_var("S3_BUCKET").unwrap_or("block-submission-archive-dev".to_string()), } } @@ -130,7 +140,7 @@ mod tests { #[test] fn test_get_env_bool_not_there() { let flag = get_env_bool("DOESNT_EXIST"); - assert!(!flag); + assert_eq!(flag, None); } #[test] @@ -138,7 +148,7 @@ mod tests { let test_key = "TEST_KEY_BOOL_TRUE"; let test_value = "true"; std::env::set_var(test_key, test_value); - assert!(get_env_bool(test_key)); + assert_eq!(get_env_bool(test_key), Some(true)); } #[test] @@ -146,7 +156,7 @@ mod tests { let test_key = "TEST_KEY_BOOL_TRUE2"; let test_value = "TRUE"; std::env::set_var(test_key, test_value); - assert!(get_env_bool(test_key)); + assert_eq!(get_env_bool(test_key), Some(true)); } #[test] @@ -154,7 +164,7 @@ mod tests { let test_key = "TEST_KEY_BOOL_FALSE"; let test_value = "false"; std::env::set_var(test_key, test_value); - assert!(!get_env_bool(test_key)); + assert_eq!(get_env_bool(test_key), Some(false)); } #[test] diff --git a/src/log.rs b/src/log.rs index 3609401..481b17b 100644 --- a/src/log.rs +++ b/src/log.rs @@ -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(); + }; }