Skip to content

Commit

Permalink
feat(env): log JSON boolean
Browse files Browse the repository at this point in the history
Makes JSON logging depend on the setting of a bool.
  • Loading branch information
alextes committed Nov 4, 2023
1 parent 4a72ca3 commit c76d22d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 50 deletions.
24 changes: 17 additions & 7 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,31 @@ 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<bool> {
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,
}

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()),
}
}
Expand Down Expand Up @@ -130,31 +140,31 @@ mod tests {
#[test]
fn test_get_env_bool_not_there() {
let flag = get_env_bool("DOESNT_EXIST");
assert!(!flag);
assert_eq!(flag, None);
}

#[test]
fn test_get_env_bool_true() {
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]
fn test_get_env_bool_true_upper() {
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]
fn test_get_env_bool_false() {
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]
Expand Down
52 changes: 9 additions & 43 deletions src/log.rs
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();
};
}

0 comments on commit c76d22d

Please sign in to comment.