Skip to content

Commit

Permalink
Toggle json logs using commandline flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
bonomat committed May 10, 2021
1 parent 8af31eb commit dcf9867
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
If you want to access data created by a previous version you will have to rename the data folder or one of the following:
1. For the CLI you can use `--data-dir` to point to the old directory.
2. For the ASB you can change the data-dir in the config file of the ASB.

### Added

- Added a new argument to ASB: `--json` or `-j`. If set, log messages will be printed in JSON format.

## [0.5.0] - 2021-04-17

Expand Down
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion swap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,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"
Expand Down
7 changes: 7 additions & 0 deletions swap/src/asb/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ use std::path::PathBuf;
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.",
Expand Down
12 changes: 8 additions & 4 deletions swap/src/asb/tracing.rs
Original file line number Diff line number Diff line change
@@ -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(());
}
Expand All @@ -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.");
Expand Down
3 changes: 1 addition & 2 deletions swap/src/bin/asb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,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
Expand Down

0 comments on commit dcf9867

Please sign in to comment.