From fc0cceb1808eb65418f58a5a62eb6bd3e60b2720 Mon Sep 17 00:00:00 2001 From: Philipp Hoenisch Date: Mon, 10 May 2021 12:03:43 +1000 Subject: [PATCH] Toggle json logs using commandline flag. --- CHANGELOG.md | 1 + Cargo.lock | 13 +++++++++++++ swap/Cargo.toml | 2 +- swap/src/asb/command.rs | 7 +++++++ swap/src/asb/tracing.rs | 12 ++++++++---- swap/src/bin/asb.rs | 3 +-- 6 files changed, 31 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f499a386..5914aabe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 When started with `--resume-only` the ASB does not accept new, incoming swap requests but only finishes swaps that are resumed upon startup. - A minimum accepted Bitcoin amount for the ASB similar to the maximum amount already present. For the CLI the minimum amount is enforced by waiting until at least the minimum is available as max-giveable amount. +- Added a new argument to ASB: `--json` or `-j`. If set, log messages will be printed in JSON format. ### Fixed diff --git a/Cargo.lock b/Cargo.lock index 23f48cfa..b1e7b0d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4289,6 +4289,16 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-serde" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb65ea441fbb84f9f6748fd496cf7f63ec9af5bca94dd86456978d055e8eb28b" +dependencies = [ + "serde", + "tracing-core", +] + [[package]] name = "tracing-subscriber" version = "0.2.18" @@ -4300,11 +4310,14 @@ dependencies = [ "lazy_static", "matchers", "regex", + "serde", + "serde_json", "sharded-slab", "thread_local", "tracing", "tracing-core", "tracing-log", + "tracing-serde", ] [[package]] diff --git a/swap/Cargo.toml b/swap/Cargo.toml index b236c334..824eb31f 100644 --- a/swap/Cargo.toml +++ b/swap/Cargo.toml @@ -61,7 +61,7 @@ torut = { version = "0.1", default-features = false, features = [ "v3", "control tracing = { version = "0.1", features = [ "attributes" ] } tracing-appender = "0.1" tracing-futures = { version = "0.2", features = [ "std-future", "futures-03" ] } -tracing-subscriber = { version = "0.2", default-features = false, features = [ "fmt", "ansi", "env-filter", "chrono", "tracing-log" ] } +tracing-subscriber = { version = "0.2", default-features = false, features = [ "fmt", "ansi", "env-filter", "chrono", "tracing-log", "json" ] } url = { version = "2", features = [ "serde" ] } uuid = { version = "0.8", features = [ "serde", "v4" ] } void = "1" diff --git a/swap/src/asb/command.rs b/swap/src/asb/command.rs index 3f641653..b669e5fb 100644 --- a/swap/src/asb/command.rs +++ b/swap/src/asb/command.rs @@ -10,6 +10,13 @@ use uuid::Uuid; author )] pub struct Arguments { + #[structopt( + short, + long = "json", + help = "Changes the log messages to json vs plain-text. If you run ASB as a service, it is recommended to set this to true to simplify log analyses." + )] + pub json: bool, + #[structopt( long = "config", help = "Provide a custom path to the configuration file. The configuration file must be a toml file.", diff --git a/swap/src/asb/tracing.rs b/swap/src/asb/tracing.rs index 65a84970..bced94d4 100644 --- a/swap/src/asb/tracing.rs +++ b/swap/src/asb/tracing.rs @@ -1,8 +1,9 @@ use anyhow::Result; use tracing_subscriber::filter::LevelFilter; +use tracing_subscriber::fmt::time::ChronoLocal; use tracing_subscriber::FmtSubscriber; -pub fn init(level: LevelFilter) -> Result<()> { +pub fn init(level: LevelFilter, json_format: bool) -> Result<()> { if level == LevelFilter::OFF { return Ok(()); } @@ -13,12 +14,15 @@ pub fn init(level: LevelFilter) -> Result<()> { .with_env_filter(format!("asb={},swap={}", level, level)) .with_writer(std::io::stderr) .with_ansi(is_terminal) + .with_timer(ChronoLocal::with_format("%F %T".to_owned())) .with_target(false); - if !is_terminal { - builder.without_time().init(); - } else { + if json_format { + builder.json().init(); + } else if is_terminal { builder.init(); + } else { + builder.without_time().init(); } tracing::info!(%level, "Initialized tracing"); diff --git a/swap/src/bin/asb.rs b/swap/src/bin/asb.rs index 7a671802..e9284373 100644 --- a/swap/src/bin/asb.rs +++ b/swap/src/bin/asb.rs @@ -45,9 +45,8 @@ const DEFAULT_WALLET_NAME: &str = "asb-wallet"; #[tokio::main] async fn main() -> Result<()> { - asb::tracing::init(LevelFilter::DEBUG).expect("initialize tracing"); - let opt = Arguments::from_args(); + asb::tracing::init(LevelFilter::DEBUG, opt.json).expect("initialize tracing"); let config_path = if let Some(config_path) = opt.config { config_path