From d7e25a7a946eb7457513b889514c5a1907841447 Mon Sep 17 00:00:00 2001 From: 4TT1L4 <2914096+4TT1L4@users.noreply.github.com> Date: Wed, 21 Sep 2022 16:42:23 +0200 Subject: [PATCH] Support for structured logging (JSON) #437 - Added json_logs feature and made the used logging library dependend on the current feature flag setting. --- Cargo.lock | 45 ++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/bin/oura/daemon.rs | 8 +++++++- src/bin/oura/dump.rs | 8 +++++++- src/bin/oura/watch.rs | 8 +++++++- 5 files changed, 68 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3cf572b4..747dc346 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -741,6 +741,16 @@ dependencies = [ "sct", ] +[[package]] +name = "ctor" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f877be4f7c9f246b183111634f75baa039715e3f46ce860677d3b19a69fb229c" +dependencies = [ + "quote", + "syn", +] + [[package]] name = "darling" version = "0.13.1" @@ -1288,6 +1298,20 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "json_env_logger2" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c7e71ad9bc3cde3ac35a75ec4bd7e49b0628a6478524b1656af04d593ceb13b" +dependencies = [ + "backtrace", + "chrono", + "env_logger", + "kv-log-macro", + "log 0.4.17", + "serde_json", +] + [[package]] name = "kafka" version = "0.8.0" @@ -1306,6 +1330,15 @@ dependencies = [ "twox-hash", ] +[[package]] +name = "kv-log-macro" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f" +dependencies = [ + "log 0.4.17", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -1349,6 +1382,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" dependencies = [ "cfg-if 1.0.0", + "value-bag", ] [[package]] @@ -1636,6 +1670,7 @@ dependencies = [ "env_logger", "file-rotate", "hex", + "json_env_logger2", "kafka", "log 0.4.17", "merge", @@ -2873,6 +2908,16 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68b90931029ab9b034b300b797048cf23723400aa757e8a2bfb9d748102f9821" +[[package]] +name = "value-bag" +version = "1.0.0-alpha.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2209b78d1249f7e6f3293657c9779fe31ced465df091bbd433a1cf88e916ec55" +dependencies = [ + "ctor", + "version_check", +] + [[package]] name = "vcpkg" version = "0.2.15" diff --git a/Cargo.toml b/Cargo.toml index 416952ac..2711d6d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ bech32 = "0.9.1" clap = "3.2.22" log = "0.4.17" env_logger = "0.9.1" +json_env_logger2 = { version = "0.2", features = ["iso-timestamps", "backtrace"], optional = true } crossterm = "0.25" merge = "0.1.0" config = { version = "0.13.2", default-features = false, features = [ @@ -79,3 +80,4 @@ fingerprint = ["murmur3"] aws = ["aws-config", "aws-sdk-sqs", "aws-sdk-lambda", "aws-sdk-s3", "tokio"] redissink = ["redis", "tokio"] gcp = ["cloud-pubsub", "tokio", "web"] +json_logs = ["json_env_logger2"] diff --git a/src/bin/oura/daemon.rs b/src/bin/oura/daemon.rs index a257367e..35d1e369 100644 --- a/src/bin/oura/daemon.rs +++ b/src/bin/oura/daemon.rs @@ -5,6 +5,12 @@ use config::{Config, ConfigError, Environment, File}; use log::debug; use serde::Deserialize; +#[cfg(not(feature = "json_logs"))] +use env_logger as logger; + +#[cfg(feature = "json_logs")] +use json_env_logger2 as logger; + use oura::{ pipelining::{ BootstrapResult, FilterProvider, PartialBootstrapResult, SinkProvider, SourceProvider, @@ -293,7 +299,7 @@ fn bootstrap( } pub fn run(args: &ArgMatches) -> Result<(), Error> { - env_logger::init(); + logger::init(); let explicit_config = match args.is_present("config") { true => Some(args.value_of_t("config")?), diff --git a/src/bin/oura/dump.rs b/src/bin/oura/dump.rs index 4b6d28ae..c7aea5bb 100644 --- a/src/bin/oura/dump.rs +++ b/src/bin/oura/dump.rs @@ -14,6 +14,12 @@ use oura::sinks::stdout::Config as StdoutConfig; use oura::sources::n2c::Config as N2CConfig; use oura::sources::n2n::Config as N2NConfig; +#[cfg(not(feature = "json_logs"))] +use env_logger as logger; + +#[cfg(feature = "json_logs")] +use json_env_logger2 as logger; + #[cfg(feature = "logs")] use oura::sinks::logs::Config as LogsConfig; @@ -58,7 +64,7 @@ fn bootstrap_sink(sink: DumpSink, input: StageReceiver, utils: Arc) -> Bo } pub fn run(args: &ArgMatches) -> Result<(), Error> { - env_logger::builder() + logger::builder() .filter_module("oura::dump", log::LevelFilter::Info) .init(); diff --git a/src/bin/oura/watch.rs b/src/bin/oura/watch.rs index 31cf1a7a..731ee461 100644 --- a/src/bin/oura/watch.rs +++ b/src/bin/oura/watch.rs @@ -15,6 +15,12 @@ use oura::sources::n2n::Config as N2NConfig; use crate::Error; +#[cfg(not(feature = "json_logs"))] +use env_logger as logger; + +#[cfg(feature = "json_logs")] +use json_env_logger2 as logger; + #[derive(Clone, Debug, Deserialize)] pub enum PeerMode { AsNode, @@ -39,7 +45,7 @@ enum WatchSource { } pub fn run(args: &ArgMatches) -> Result<(), Error> { - env_logger::builder() + logger::builder() .filter_level(log::LevelFilter::Error) .init();