Add support for JSON log output.

This commit is contained in:
Orne Brocaar 2023-06-29 13:17:28 +01:00
parent 96767e954f
commit 3aa8bdbecc
5 changed files with 44 additions and 4 deletions

13
Cargo.lock generated
View File

@ -4534,6 +4534,16 @@ dependencies = [
"tracing-core",
]
[[package]]
name = "tracing-serde"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc6b213177105856957181934e4920de57730fc69bf42c37ee5bb664d406d9e1"
dependencies = [
"serde",
"tracing-core",
]
[[package]]
name = "tracing-subscriber"
version = "0.3.17"
@ -4541,11 +4551,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77"
dependencies = [
"nu-ansi-term",
"serde",
"serde_json",
"sharded-slab",
"smallvec",
"thread_local",
"tracing-core",
"tracing-log",
"tracing-serde",
]
[[package]]

View File

@ -42,6 +42,7 @@ tracing = "0.1"
tracing-subscriber = { version = "0.3", features = [
"fmt",
"ansi",
"json",
], default-features = true }
# ChirpStack API definitions

View File

@ -4,6 +4,23 @@ use super::super::config;
pub fn run() {
let template = r#"
# Logging configuration
[logging]
# Log level.
#
# Valid options are:
# * TRACE
# * DEBUG
# * INFO
# * WARN
# * ERROR
# * OFF
level="{{ logging.level }}"
# Log as JSON.
json={{ logging.json }}
# PostgreSQL configuration.
[postgresql]

View File

@ -37,12 +37,14 @@ pub struct Configuration {
#[serde(default)]
pub struct Logging {
pub level: String,
pub json: bool,
}
impl Default for Logging {
fn default() -> Self {
Logging {
level: "info".into(),
json: false,
}
}
}

View File

@ -92,10 +92,17 @@ async fn main() -> Result<()> {
("lrwn", Level::from_str(&conf.logging.level).unwrap()),
]);
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer())
.with(filter)
.init();
if conf.logging.json {
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer().json())
.with(filter)
.init();
} else {
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer())
.with(filter)
.init();
}
if let Some(Commands::Configfile {}) = &cli.command {
cmd::configfile::run();