From 679b155db1746bb839dda81e5c4bae3cf9f36b7f Mon Sep 17 00:00:00 2001 From: rishflab Date: Thu, 9 Sep 2021 21:35:03 +1000 Subject: [PATCH] Enable log timestamps using explicit command line flag Previously logs were only timestamped when the ASB was run in an interactive terminal or if the logs were output as JSON. JSON logs and ASB output in an interactive terminal are no longer timestamped by default. --- CHANGELOG.md | 2 ++ swap/src/asb/command.rs | 34 ++++++++++++++++++++++++++++++++++ swap/src/asb/tracing.rs | 13 ++++++------- swap/src/bin/asb.rs | 3 ++- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f205f00..471c65e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Timestamping can now be enabled using the `timestamp` command line argument to the ASB. + JSON logs and ASB output in an interactive terminal are no longer timestamped by default. - The `cancel`, `refund` and `punish` subcommands in ASB and CLI are run with the `--force` by default and the `--force` option has been removed. The force flag was used to ignore blockheight and protocol state checks. Users can still restart a swap with these checks using the `resume` subcommand. diff --git a/swap/src/asb/command.rs b/swap/src/asb/command.rs index 548a4f29..a5a0699d 100644 --- a/swap/src/asb/command.rs +++ b/swap/src/asb/command.rs @@ -19,6 +19,7 @@ where let args = RawArguments::from_clap(&matches); let json = args.json; + let timestamp = args.timestamp; let testnet = args.testnet; let config = args.config; let command: RawCommand = args.cmd; @@ -27,6 +28,7 @@ where RawCommand::Start { resume_only } => Arguments { testnet, json, + timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::Start { resume_only }, @@ -34,6 +36,7 @@ where RawCommand::History => Arguments { testnet, json, + timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::History, @@ -41,6 +44,7 @@ where RawCommand::WithdrawBtc { amount, address } => Arguments { testnet, json, + timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::WithdrawBtc { @@ -51,6 +55,7 @@ where RawCommand::Balance => Arguments { testnet, json, + timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::Balance, @@ -61,6 +66,7 @@ where }) => Arguments { testnet, json, + timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::Redeem { @@ -74,6 +80,7 @@ where }) => Arguments { testnet, json, + timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::Cancel { swap_id }, @@ -83,6 +90,7 @@ where }) => Arguments { testnet, json, + timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::Refund { swap_id }, @@ -92,6 +100,7 @@ where }) => Arguments { testnet, json, + timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::Punish { swap_id }, @@ -99,6 +108,7 @@ where RawCommand::ManualRecovery(ManualRecovery::SafelyAbort { swap_id }) => Arguments { testnet, json, + timestamp, config_path: config_path(config, testnet)?, env_config: env_config(testnet), cmd: Command::SafelyAbort { swap_id }, @@ -158,6 +168,7 @@ pub struct BitcoinAddressNetworkMismatch { pub struct Arguments { pub testnet: bool, pub json: bool, + pub timestamp: bool, pub config_path: PathBuf, pub env_config: env::Config, pub cmd: Command, @@ -210,6 +221,13 @@ pub struct RawArguments { )] pub json: bool, + #[structopt( + short, + long = "timestamp", + help = "Adds a timestamp to the log messages" + )] + pub timestamp: bool, + #[structopt( long = "config", help = "Provide a custom path to the configuration file. The configuration file must be a toml file.", @@ -326,6 +344,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, + timestamp: false, config_path: default_mainnet_conf_path.clone(), env_config: mainnet_env_config, cmd: Command::Start { resume_only: false }, @@ -337,6 +356,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, + timestamp: false, config_path: default_mainnet_conf_path.clone(), env_config: mainnet_env_config, cmd: Command::History, @@ -348,6 +368,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, + timestamp: false, config_path: default_mainnet_conf_path.clone(), env_config: mainnet_env_config, cmd: Command::Balance, @@ -364,6 +385,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, + timestamp: false, config_path: default_mainnet_conf_path.clone(), env_config: mainnet_env_config, cmd: Command::WithdrawBtc { @@ -384,6 +406,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, + timestamp: false, config_path: default_mainnet_conf_path.clone(), env_config: mainnet_env_config, cmd: Command::Cancel { @@ -403,6 +426,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, + timestamp: false, config_path: default_mainnet_conf_path.clone(), env_config: mainnet_env_config, cmd: Command::Refund { @@ -422,6 +446,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, + timestamp: false, config_path: default_mainnet_conf_path.clone(), env_config: mainnet_env_config, cmd: Command::Punish { @@ -441,6 +466,7 @@ mod tests { let expected_args = Arguments { testnet: false, json: false, + timestamp: false, config_path: default_mainnet_conf_path, env_config: mainnet_env_config, cmd: Command::SafelyAbort { @@ -460,6 +486,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, + timestamp: false, config_path: default_testnet_conf_path.clone(), env_config: testnet_env_config, cmd: Command::Start { resume_only: false }, @@ -471,6 +498,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, + timestamp: false, config_path: default_testnet_conf_path.clone(), env_config: testnet_env_config, cmd: Command::History, @@ -482,6 +510,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, + timestamp: false, config_path: default_testnet_conf_path.clone(), env_config: testnet_env_config, cmd: Command::Balance, @@ -499,6 +528,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, + timestamp: false, config_path: default_testnet_conf_path.clone(), env_config: testnet_env_config, cmd: Command::WithdrawBtc { @@ -520,6 +550,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, + timestamp: false, config_path: default_testnet_conf_path.clone(), env_config: testnet_env_config, cmd: Command::Cancel { @@ -540,6 +571,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, + timestamp: false, config_path: default_testnet_conf_path.clone(), env_config: testnet_env_config, cmd: Command::Refund { @@ -560,6 +592,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, + timestamp: false, config_path: default_testnet_conf_path.clone(), env_config: testnet_env_config, cmd: Command::Punish { @@ -580,6 +613,7 @@ mod tests { let expected_args = Arguments { testnet: true, json: false, + timestamp: false, config_path: default_testnet_conf_path, env_config: testnet_env_config, cmd: Command::SafelyAbort { diff --git a/swap/src/asb/tracing.rs b/swap/src/asb/tracing.rs index bced94d4..dc3f7cca 100644 --- a/swap/src/asb/tracing.rs +++ b/swap/src/asb/tracing.rs @@ -3,7 +3,7 @@ use tracing_subscriber::filter::LevelFilter; use tracing_subscriber::fmt::time::ChronoLocal; use tracing_subscriber::FmtSubscriber; -pub fn init(level: LevelFilter, json_format: bool) -> Result<()> { +pub fn init(level: LevelFilter, json_format: bool, timestamp: bool) -> Result<()> { if level == LevelFilter::OFF { return Ok(()); } @@ -17,12 +17,11 @@ pub fn init(level: LevelFilter, json_format: bool) -> Result<()> { .with_timer(ChronoLocal::with_format("%F %T".to_owned())) .with_target(false); - if json_format { - builder.json().init(); - } else if is_terminal { - builder.init(); - } else { - builder.without_time().init(); + match (json_format, timestamp) { + (true, true) => builder.json().init(), + (true, false) => builder.json().without_time().init(), + (false, true) => builder.init(), + (false, false) => builder.without_time().init(), } tracing::info!(%level, "Initialized tracing"); diff --git a/swap/src/bin/asb.rs b/swap/src/bin/asb.rs index d8583aeb..79cd71e2 100644 --- a/swap/src/bin/asb.rs +++ b/swap/src/bin/asb.rs @@ -45,6 +45,7 @@ async fn main() -> Result<()> { let Arguments { testnet, json, + timestamp, config_path, env_config, cmd, @@ -66,7 +67,7 @@ async fn main() -> Result<()> { } }; - asb::tracing::init(LevelFilter::DEBUG, json).expect("initialize tracing"); + asb::tracing::init(LevelFilter::DEBUG, json, timestamp).expect("initialize tracing"); let config = match read_config(config_path.clone())? { Ok(config) => config,