Use env!("CARGO_CRATE_NAME") in the example to simplify the tracing setup code (#2884)

This commit is contained in:
Weipeng Hong 2024-08-24 14:36:08 +08:00 committed by GitHub
parent bd3eaa7886
commit ef1448a3f5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
38 changed files with 70 additions and 50 deletions

View file

@ -36,7 +36,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_chat=trace".into()), .unwrap_or_else(|_| format!("{}=trace", env!("CARGO_CRATE_NAME")).into()),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -12,7 +12,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example-compression=trace".into()), .unwrap_or_else(|_| format!("{}=trace", env!("CARGO_CRATE_NAME")).into()),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -22,7 +22,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_consume_body_in_extractor_or_middleware=debug".into()), .unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -16,7 +16,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_customize_extractor_error=trace".into()), .unwrap_or_else(|_| format!("{}=trace", env!("CARGO_CRATE_NAME")).into()),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -20,7 +20,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_customize_path_rejection=debug".into()), .unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -25,7 +25,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_dependency_injection=debug".into()), .unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -57,7 +57,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_diesel_async_postgres=debug".into()), .unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -54,7 +54,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_tokio_postgres=debug".into()), .unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -45,8 +45,9 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
async fn main() { async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
.unwrap_or_else(|_| "example_error_handling=debug,tower_http=debug".into()), format!("{}=debug,tower_http=debug", env!("CARGO_CRATE_NAME")).into()
}),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -13,7 +13,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_form=debug".into()), .unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -17,7 +17,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_global_404_handler=debug".into()), .unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -21,7 +21,11 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| { tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
"example_graceful_shutdown=debug,tower_http=debug,axum=trace".into() format!(
"{}=debug,tower_http=debug,axum=trace",
env!("CARGO_CRATE_NAME")
)
.into()
}), }),
) )
.with(tracing_subscriber::fmt::layer().without_time()) .with(tracing_subscriber::fmt::layer().without_time())

View file

@ -36,8 +36,9 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
async fn main() { async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
.unwrap_or_else(|_| "example_http_proxy=trace,tower_http=debug".into()), format!("{}=trace,tower_http=debug", env!("CARGO_CRATE_NAME")).into()
}),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -61,7 +61,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_jwt=debug".into()), .unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -33,8 +33,9 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
async fn main() { async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
.unwrap_or_else(|_| "example_key_value_store=debug,tower_http=debug".into()), format!("{}=debug,tower_http=debug", env!("CARGO_CRATE_NAME")).into()
}),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -15,7 +15,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_low_level_openssl=debug".into()), .unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -29,7 +29,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_low_level_rustls=debug".into()), .unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -17,8 +17,9 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
async fn main() { async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
.unwrap_or_else(|_| "example_multipart_form=debug,tower_http=debug".into()), format!("{}=debug,tower_http=debug", env!("CARGO_CRATE_NAME")).into()
}),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -35,7 +35,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_oauth=debug".into()), .unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -22,7 +22,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| { tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
"example_parse_body_based_on_content_type=debug,tower_http=debug".into() format!("{}=debug,tower_http=debug", env!("CARGO_CRATE_NAME")).into()
}), }),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())

View file

@ -20,8 +20,9 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
async fn main() { async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
.unwrap_or_else(|_| "example_print_request_response=debug,tower_http=debug".into()), format!("{}=debug,tower_http=debug", env!("CARGO_CRATE_NAME")).into()
}),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -63,8 +63,9 @@ async fn start_metrics_server() {
async fn main() { async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
.unwrap_or_else(|_| "example_prometheus_metrics=debug,tower_http=debug".into()), format!("{}=debug,tower_http=debug", env!("CARGO_CRATE_NAME")).into()
}),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -23,8 +23,9 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
async fn main() { async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
.unwrap_or_else(|_| "example_reqwest_response=debug,tower_http=debug".into()), format!("{}=debug,tower_http=debug", env!("CARGO_CRATE_NAME")).into()
}),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -31,7 +31,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_tokio_postgres=debug".into()), .unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -24,8 +24,9 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
async fn main() { async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
.unwrap_or_else(|_| "example_sse=debug,tower_http=debug".into()), format!("{}=debug,tower_http=debug", env!("CARGO_CRATE_NAME")).into()
}),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -19,8 +19,9 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
async fn main() { async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
.unwrap_or_else(|_| "example_static_file_server=debug,tower_http=debug".into()), format!("{}=debug,tower_http=debug", env!("CARGO_CRATE_NAME")).into()
}),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -25,7 +25,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_stream_to_file=debug".into()), .unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -19,7 +19,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_templates=debug".into()), .unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -18,8 +18,9 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
async fn main() { async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
.unwrap_or_else(|_| "example_testing=debug,tower_http=debug".into()), format!("{}=debug,tower_http=debug", env!("CARGO_CRATE_NAME")).into()
}),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -28,7 +28,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_tls_graceful_shutdown=debug".into()), .unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -30,7 +30,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_tls_rustls=debug".into()), .unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -36,8 +36,9 @@ use uuid::Uuid;
async fn main() { async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
.unwrap_or_else(|_| "example_todos=debug,tower_http=debug".into()), format!("{}=debug,tower_http=debug", env!("CARGO_CRATE_NAME")).into()
}),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -21,7 +21,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_tokio_postgres=debug".into()), .unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -23,7 +23,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_tokio_redis=debug".into()), .unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -25,7 +25,11 @@ async fn main() {
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| { tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
// axum logs rejections from built-in extractors with the `axum::rejection` // axum logs rejections from built-in extractors with the `axum::rejection`
// target, at `TRACE` level. `axum::rejection=trace` enables showing those events // target, at `TRACE` level. `axum::rejection=trace` enables showing those events
"example_tracing_aka_logging=debug,tower_http=debug,axum::rejection=trace".into() format!(
"{}=debug,tower_http=debug,axum::rejection=trace",
env!("CARGO_CRATE_NAME")
)
.into()
}), }),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())

View file

@ -29,7 +29,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_validator=debug".into()), .unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -20,7 +20,7 @@ async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_versioning=debug".into()), .unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View file

@ -45,8 +45,9 @@ use futures::{sink::SinkExt, stream::StreamExt};
async fn main() { async fn main() {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env() tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
.unwrap_or_else(|_| "example_websockets=debug,tower_http=debug".into()), format!("{}=debug,tower_http=debug", env!("CARGO_CRATE_NAME")).into()
}),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();