diff --git a/Cargo.nix b/Cargo.nix index 10c1aaf..9f40397 100644 --- a/Cargo.nix +++ b/Cargo.nix @@ -160,7 +160,10 @@ rec { sys_info."${deps.ofborg."0.1.9".sys_info}".default = true; tempfile."${deps.ofborg."0.1.9".tempfile}".default = true; tracing."${deps.ofborg."0.1.9".tracing}".default = true; - tracing_subscriber."${deps.ofborg."0.1.9".tracing_subscriber}".default = true; + tracing_subscriber = fold recursiveUpdate {} [ + { "${deps.ofborg."0.1.9".tracing_subscriber}"."json" = true; } + { "${deps.ofborg."0.1.9".tracing_subscriber}".default = true; } + ]; uuid = fold recursiveUpdate {} [ { "${deps.ofborg."0.1.9".uuid}"."v4" = true; } { "${deps.ofborg."0.1.9".uuid}".default = true; } diff --git a/ofborg/Cargo.toml b/ofborg/Cargo.toml index 6108122..097978e 100644 --- a/ofborg/Cargo.toml +++ b/ofborg/Cargo.toml @@ -18,7 +18,7 @@ serde_derive = "1.0" serde_json = "1.0" tempfile = "2.2.0" tracing = "0.1.13" -tracing-subscriber = "0.2.5" +tracing-subscriber = { version = "0.2.5", features = ["json"] } hubcaps = { git = "https://github.com/grahamc/hubcaps.git" } #hubcaps = { path = "../../../grahamc/hubcaps/" } # for testing patches hyper = "0.10.*" diff --git a/ofborg/src/lib.rs b/ofborg/src/lib.rs index 4f15971..35928c3 100644 --- a/ofborg/src/lib.rs +++ b/ofborg/src/lib.rs @@ -11,6 +11,8 @@ extern crate serde_derive; #[macro_use] extern crate nom; +use std::env; + use tracing_subscriber::prelude::*; use tracing_subscriber::EnvFilter; @@ -88,15 +90,25 @@ pub mod ofborg { } pub fn setup_log() { - let fmt_layer = tracing_subscriber::fmt::layer(); let filter_layer = EnvFilter::try_from_default_env() .or_else(|_| EnvFilter::try_new("info")) .unwrap(); - tracing_subscriber::registry() - .with(filter_layer) - .with(fmt_layer) - .init(); + let log_json = env::var("RUST_LOG_JSON").map_or(false, |s| s == "1"); + + if log_json { + let fmt_layer = tracing_subscriber::fmt::layer().json(); + tracing_subscriber::registry() + .with(filter_layer) + .with(fmt_layer) + .init(); + } else { + let fmt_layer = tracing_subscriber::fmt::layer(); + tracing_subscriber::registry() + .with(filter_layer) + .with(fmt_layer) + .init(); + } tracing::info!("Logging configured"); }