refactor(telemetry): make bunyan as a feature

This commit is contained in:
Kristofers Solo 2025-09-23 09:02:00 +03:00
parent fe0b153caa
commit e21e73f0b4
Signed by: kristoferssolo
GPG Key ID: 74FF8144483D82C8
3 changed files with 25 additions and 7 deletions

1
Cargo.lock generated
View File

@ -1754,7 +1754,6 @@ dependencies = [
"tracing", "tracing",
"tracing-appender", "tracing-appender",
"tracing-bunyan-formatter", "tracing-bunyan-formatter",
"tracing-log 0.2.0",
"tracing-subscriber", "tracing-subscriber",
"url", "url",
] ]

View File

@ -9,7 +9,7 @@ edition = "2024"
async-trait = "0.1" async-trait = "0.1"
color-eyre = "0.6" color-eyre = "0.6"
dotenv = "0.15" dotenv = "0.15"
futures = "0.3.31" futures = "0.3"
infer = "0.19" infer = "0.19"
rand = "0.9" rand = "0.9"
regex = "1.11" regex = "1.11"
@ -26,11 +26,13 @@ tokio = { version = "1", features = [
] } ] }
tracing = "0.1" tracing = "0.1"
tracing-appender = "0.2" tracing-appender = "0.2"
tracing-bunyan-formatter = { version = "0.3", default-features = false } tracing-bunyan-formatter = { version = "0.3", default-features = false, optional = true }
tracing-log = "0.2.0"
tracing-subscriber = { version = "0.3", features = ["registry", "env-filter"] } tracing-subscriber = { version = "0.3", features = ["registry", "env-filter"] }
url = "2.5" url = "2.5"
[features]
bunyan = ["tracing-bunyan-formatter"]
[lints.clippy] [lints.clippy]
pedantic = "warn" pedantic = "warn"
nursery = "warn" nursery = "warn"

View File

@ -1,14 +1,31 @@
#[cfg(feature = "bunyan")]
use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer}; use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer};
use tracing_subscriber::{EnvFilter, layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{EnvFilter, layer::SubscriberExt, util::SubscriberInitExt};
/// Initialise tracing with bunyan-style JSON output. /// Initialise tracing with bunyan-style JSON output.
#[cfg(feature = "bunyan")]
pub fn setup_logger() { pub fn setup_logger() {
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| "info".into()); let env_filter = create_env_filter();
let formatter = BunyanFormattingLayer::new("tg-relay-rs".into(), std::io::stdout); let formatter = BunyanFormattingLayer::new("tg-relay-rs".into(), std::io::stdout);
tracing_subscriber::registry() tracing_subscriber::registry()
.with(env_filter) .with(env_filter)
.with(JsonStorageLayer)
.with(formatter) .with(formatter)
.init(); .with(JsonStorageLayer)
.init()
}
#[cfg(not(feature = "bunyan"))]
pub fn setup_logger() {
let env_filter = create_env_filter();
let formatter = tracing_subscriber::fmt::Layer::default();
tracing_subscriber::registry()
.with(env_filter)
.with(formatter)
.init()
}
fn create_env_filter() -> EnvFilter {
EnvFilter::try_from_default_env().unwrap_or_else(|_| "info".into())
} }