Skip to content

Commit 57cb54e

Browse files
committed
feat(pyth-lazer-agent) Make the logging format configurable
1 parent fb000d4 commit 57cb54e

File tree

1 file changed

+40
-14
lines changed

1 file changed

+40
-14
lines changed

apps/pyth-lazer-agent/src/main.rs

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use serde::Deserialize;
12
use {
23
crate::lazer_publisher::LazerPublisher,
34
anyhow::Context,
@@ -19,33 +20,58 @@ mod websocket_utils;
1920
struct Cli {
2021
#[clap(short, long, default_value = "config/config.toml")]
2122
config: String,
23+
#[clap(short, long, default_value = "json")]
24+
log_format: LogFormat,
25+
}
26+
27+
#[derive(clap::ValueEnum, Clone, Deserialize, Default)]
28+
#[serde(rename_all = "kebab-case")]
29+
enum LogFormat {
30+
#[default]
31+
Json,
32+
Compact,
33+
Pretty,
2234
}
2335

2436
#[tokio::main]
2537
async fn main() -> anyhow::Result<()> {
38+
let args = Cli::parse();
39+
init_tracing_subscriber(args.log_format);
40+
41+
let config =
42+
config::load_config(args.config.to_string()).context("Failed to read config file")?;
43+
info!(?config, "starting lazer-agent");
44+
45+
let lazer_publisher = LazerPublisher::new(&config).await;
46+
http_server::run(config, lazer_publisher).await?;
47+
48+
Ok(())
49+
}
50+
51+
fn init_tracing_subscriber(log_format: LogFormat) {
52+
2653
#[allow(
2754
clippy::expect_used,
2855
reason = "application can fail on invalid RUST_LOG"
2956
)]
30-
tracing_subscriber::fmt()
57+
let subscriber = tracing_subscriber::fmt()
3158
.with_env_filter(
3259
EnvFilter::builder()
3360
.with_default_directive(LevelFilter::INFO.into())
3461
.from_env()
3562
.expect("invalid RUST_LOG env var"),
3663
)
37-
.with_span_events(FmtSpan::NONE)
38-
.json()
39-
.with_span_list(false)
40-
.init();
41-
42-
let args = Cli::parse();
43-
let config =
44-
config::load_config(args.config.to_string()).context("Failed to read config file")?;
45-
info!(?config, "starting lazer-agent");
46-
47-
let lazer_publisher = LazerPublisher::new(&config).await;
48-
http_server::run(config, lazer_publisher).await?;
64+
.with_span_events(FmtSpan::NONE);
4965

50-
Ok(())
66+
match log_format {
67+
LogFormat::Json => {
68+
subscriber.json().with_span_list(false).init();
69+
}
70+
LogFormat::Compact => {
71+
subscriber.compact().init();
72+
}
73+
LogFormat::Pretty => {
74+
subscriber.pretty().init();
75+
}
76+
}
5177
}

0 commit comments

Comments
 (0)