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", "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]] [[package]]
name = "tracing-subscriber" name = "tracing-subscriber"
version = "0.3.17" version = "0.3.17"
@ -4541,11 +4551,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77" checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77"
dependencies = [ dependencies = [
"nu-ansi-term", "nu-ansi-term",
"serde",
"serde_json",
"sharded-slab", "sharded-slab",
"smallvec", "smallvec",
"thread_local", "thread_local",
"tracing-core", "tracing-core",
"tracing-log", "tracing-log",
"tracing-serde",
] ]
[[package]] [[package]]

View File

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

View File

@ -4,6 +4,23 @@ use super::super::config;
pub fn run() { pub fn run() {
let template = r#" 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 configuration.
[postgresql] [postgresql]

View File

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

View File

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