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 as an option for logging by env var #960

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 13 additions & 0 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 apps/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ tower-abci-abcipp = {package = "tower-abci", git = "https://github.com/heliaxdev
tower-abci = {version = "0.1.0", optional = true}
tracing = "0.1.30"
tracing-log = "0.1.2"
tracing-subscriber = {version = "0.3.7", features = ["env-filter"]}
tracing-subscriber = {version = "0.3.7", features = ["env-filter", "json"]}
websocket = "0.26.2"
winapi = "0.3.9"
#libmasp = { git = "https://github.com/anoma/masp", branch = "murisi/masp-incentive" }
Expand Down
30 changes: 23 additions & 7 deletions apps/src/lib/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub const ENV_KEY: &str = "NAMADA_LOG";

// Env var to enable/disable color log
const COLOR_ENV_KEY: &str = "NAMADA_LOG_COLOR";
// Env var to enable/disable json formatting
const JSON_ENV_KEY: &str = "NAMADA_JSON_FMT";

pub fn init_from_env_or(default: impl Into<Directive>) -> Result<()> {
let filter = filter_from_env_or(default);
Expand All @@ -30,13 +32,27 @@ pub fn set_subscriber(filter: EnvFilter) -> Result<()> {
} else {
true
};

let my_collector = Subscriber::builder()
.with_ansi(with_color)
.with_env_filter(filter)
.finish();
tracing::subscriber::set_global_default(my_collector)
.wrap_err("Failed to set log subscriber")
let json_format = if let Ok(val) = env::var(JSON_ENV_KEY) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this makes the json fmt the default

val.to_ascii_lowercase() != "false"
} else {
true
};
if json_format {
let my_collector = Subscriber::builder()
.with_ansi(with_color)
.json()
.with_env_filter(filter)
.finish();
tracing::subscriber::set_global_default(my_collector)
.wrap_err("Failed to set log subscriber")
} else {
let my_collector = Subscriber::builder()
.with_ansi(with_color)
.with_env_filter(filter)
.finish();
tracing::subscriber::set_global_default(my_collector)
.wrap_err("Failed to set log subscriber")
}
}

pub fn init_log_tracer() -> Result<()> {
Expand Down