Skip to content

Commit ac0b1b4

Browse files
authored
chore: add structured logging params (#77)
1 parent d7a988a commit ac0b1b4

File tree

7 files changed

+83
-14
lines changed

7 files changed

+83
-14
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ TIPS_INGRESS_KAFKA_INGRESS_TOPIC=tips-ingress
88
TIPS_INGRESS_KAFKA_AUDIT_PROPERTIES_FILE=/app/docker/ingress-audit-kafka-properties
99
TIPS_INGRESS_KAFKA_AUDIT_TOPIC=tips-audit
1010
TIPS_INGRESS_LOG_LEVEL=info
11+
TIPS_INGRESS_LOG_FORMAT=pretty
1112
TIPS_INGRESS_SEND_TRANSACTION_DEFAULT_LIFETIME_SECONDS=10800
1213
TIPS_INGRESS_RPC_SIMULATION=http://localhost:8549
1314
TIPS_INGRESS_METRICS_ADDR=0.0.0.0:9002
@@ -20,6 +21,7 @@ TIPS_INGRESS_BUILDER_RPCS=http://localhost:2222,http://localhost:2222,http://loc
2021
TIPS_AUDIT_KAFKA_PROPERTIES_FILE=/app/docker/audit-kafka-properties
2122
TIPS_AUDIT_KAFKA_TOPIC=tips-audit
2223
TIPS_AUDIT_LOG_LEVEL=info
24+
TIPS_AUDIT_LOG_FORMAT=pretty
2325
TIPS_AUDIT_S3_BUCKET=tips
2426
TIPS_AUDIT_S3_CONFIG_TYPE=manual
2527
TIPS_AUDIT_S3_ENDPOINT=http://localhost:7000

Cargo.lock

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ resolver = "2"
1212

1313
[workspace.dependencies]
1414
tips-audit = { path = "crates/audit" }
15-
tips-bundle-pool = { path = "crates/bundle-pool" }
1615
tips-core = { path = "crates/core" }
1716

1817
# Reth
@@ -37,7 +36,7 @@ op-alloy-flz = { version = "0.13.1" }
3736

3837
tokio = { version = "1.47.1", features = ["full"] }
3938
tracing = "0.1.41"
40-
tracing-subscriber = { version = "0.3.20", features = ["env-filter"] }
39+
tracing-subscriber = { version = "0.3.20", features = ["env-filter", "json"] }
4140
anyhow = "1.0.99"
4241
clap = { version = "4.5.47", features = ["derive", "env"] }
4342
url = "2.5.7"

crates/audit/src/bin/main.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rdkafka::consumer::Consumer;
77
use tips_audit::{
88
KafkaAuditArchiver, KafkaAuditLogReader, S3EventReaderWriter, create_kafka_consumer,
99
};
10-
use tips_core::logger::init_logger;
10+
use tips_core::logger::init_logger_with_format;
1111
use tracing::info;
1212

1313
#[derive(Debug, Clone, ValueEnum)]
@@ -31,6 +31,9 @@ struct Args {
3131
#[arg(long, env = "TIPS_AUDIT_LOG_LEVEL", default_value = "info")]
3232
log_level: String,
3333

34+
#[arg(long, env = "TIPS_AUDIT_LOG_FORMAT", default_value = "pretty")]
35+
log_format: tips_core::logger::LogFormat,
36+
3437
#[arg(long, env = "TIPS_AUDIT_S3_CONFIG_TYPE", default_value = "aws")]
3538
s3_config_type: S3ConfigType,
3639

@@ -53,7 +56,7 @@ async fn main() -> Result<()> {
5356

5457
let args = Args::parse();
5558

56-
init_logger(&args.log_level);
59+
init_logger_with_format(&args.log_level, args.log_format);
5760

5861
info!(
5962
kafka_properties_file = %args.kafka_properties_file,

crates/core/src/logger.rs

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
1+
use std::str::FromStr;
12
use tracing::warn;
2-
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
3+
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt};
4+
5+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6+
pub enum LogFormat {
7+
Pretty,
8+
Json,
9+
Compact,
10+
}
11+
12+
impl FromStr for LogFormat {
13+
type Err = String;
14+
15+
fn from_str(s: &str) -> Result<Self, Self::Err> {
16+
match s.to_lowercase().as_str() {
17+
"json" => Ok(LogFormat::Json),
18+
"compact" => Ok(LogFormat::Compact),
19+
"pretty" => Ok(LogFormat::Pretty),
20+
_ => {
21+
warn!("Invalid log format '{}', defaulting to 'pretty'", s);
22+
Ok(LogFormat::Pretty)
23+
}
24+
}
25+
}
26+
}
327

428
pub fn init_logger(log_level: &str) {
29+
init_logger_with_format(log_level, LogFormat::Pretty);
30+
}
31+
32+
pub fn init_logger_with_format(log_level: &str, format: LogFormat) {
533
let level = match log_level.to_lowercase().as_str() {
634
"trace" => tracing::Level::TRACE,
735
"debug" => tracing::Level::DEBUG,
@@ -14,11 +42,32 @@ pub fn init_logger(log_level: &str) {
1442
}
1543
};
1644

17-
tracing_subscriber::registry()
18-
.with(
19-
tracing_subscriber::EnvFilter::try_from_default_env()
20-
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(level.to_string())),
21-
)
22-
.with(tracing_subscriber::fmt::layer())
23-
.init();
45+
let env_filter = tracing_subscriber::EnvFilter::try_from_default_env()
46+
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(level.to_string()));
47+
48+
match format {
49+
LogFormat::Json => {
50+
tracing_subscriber::registry()
51+
.with(env_filter)
52+
.with(
53+
fmt::layer()
54+
.json()
55+
.flatten_event(true)
56+
.with_current_span(true),
57+
)
58+
.init();
59+
}
60+
LogFormat::Compact => {
61+
tracing_subscriber::registry()
62+
.with(env_filter)
63+
.with(fmt::layer().compact())
64+
.init();
65+
}
66+
LogFormat::Pretty => {
67+
tracing_subscriber::registry()
68+
.with(env_filter)
69+
.with(fmt::layer().pretty())
70+
.init();
71+
}
72+
}
2473
}

crates/ingress-rpc/src/bin/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rdkafka::producer::FutureProducer;
77
use tips_audit::{BundleEvent, KafkaBundleEventPublisher, connect_audit_to_publisher};
88
use tips_core::MeterBundleResponse;
99
use tips_core::kafka::load_kafka_config_from_file;
10-
use tips_core::logger::init_logger;
10+
use tips_core::logger::init_logger_with_format;
1111
use tips_ingress_rpc::Config;
1212
use tips_ingress_rpc::connect_ingress_to_builder;
1313
use tips_ingress_rpc::metrics::init_prometheus_exporter;
@@ -24,7 +24,7 @@ async fn main() -> anyhow::Result<()> {
2424
// clone once instead of cloning each field before passing to `IngressService::new`
2525
let cfg = config.clone();
2626

27-
init_logger(&config.log_level);
27+
init_logger_with_format(&config.log_level, config.log_format);
2828

2929
init_prometheus_exporter(config.metrics_addr).expect("Failed to install Prometheus exporter");
3030

crates/ingress-rpc/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ pub struct Config {
8686
#[arg(long, env = "TIPS_INGRESS_LOG_LEVEL", default_value = "info")]
8787
pub log_level: String,
8888

89+
#[arg(long, env = "TIPS_INGRESS_LOG_FORMAT", default_value = "pretty")]
90+
pub log_format: tips_core::logger::LogFormat,
91+
8992
/// Default lifetime for sent transactions in seconds (default: 3 hours)
9093
#[arg(
9194
long,

0 commit comments

Comments
 (0)