Skip to content

Commit

Permalink
Support JSON logs (#42)
Browse files Browse the repository at this point in the history
* Add JSON logging to proxy

* Remove typo
  • Loading branch information
hatchan authored Nov 29, 2021
1 parent 3314bf5 commit 7002bb4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
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 proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ tokio = { version = "1.10.1", features = ["full"] }
tokio-tungstenite-reconnect = { git = "ssh://git@github.com/fiberplane/tokio-tungstenite-reconnect.git", branch = "main" }
# tokio-tungstenite-reconnect = { path = "../../tokio-tungstenite-reconnect" }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] }
url = "2.2.2"

[dev-dependencies]
Expand Down
26 changes: 24 additions & 2 deletions proxy/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::service::ProxyService;
use clap::{AppSettings, Clap};
use data_sources::DataSources;
use std::io;
use std::net::SocketAddr;
use std::path::PathBuf;
use std::process;
Expand Down Expand Up @@ -66,13 +67,17 @@ pub struct Arguments {
about = "Address to bind HTTP server to (used for health check endpoints)"
)]
listen_address: Option<SocketAddr>,

#[clap(long, env = "LOG_JSON")]
log_json: bool,
}

#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
let mut args = Arguments::parse();

initialize_logger(args.log_json);

// Update the endpoint to include the default path if nothing is set
if args.fiberplane_endpoint.path() == "/" {
args.fiberplane_endpoint.set_path("/api/proxies/ws");
Expand Down Expand Up @@ -116,8 +121,25 @@ async fn main() {
info!("proxy shutdown successfully");
}
Err(err) => {
error!(?err, "proxy encounterd a error");
error!(?err, "proxy encountered a error");
process::exit(1);
}
};
}

fn initialize_logger(log_json: bool) {
// Initialize the builder with some defaults
let builder = tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.with_writer(io::stderr);

if log_json {
// Add a JSON formatter
builder
.json()
.try_init()
.expect("unable to initialize logging");
} else {
builder.try_init().expect("unable to initialize logging");
}
}

0 comments on commit 7002bb4

Please sign in to comment.