add json logging

This commit is contained in:
Daiderd Jordan 2020-05-01 22:27:02 +02:00
parent 52e2105df7
commit 72b41d9ec1
No known key found for this signature in database
GPG key ID: D02435D05B810C96
3 changed files with 22 additions and 7 deletions

View file

@ -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; }

View file

@ -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.*"

View file

@ -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();
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");
}