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.
This commit is contained in:
rishflab 2021-09-09 21:35:03 +10:00
parent 19836a1051
commit 679b155db1
4 changed files with 44 additions and 8 deletions

View File

@ -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.

View File

@ -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 {

View File

@ -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");

View File

@ -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,