From bc632556fdf3bb34768fa2ccb369116ccce85838 Mon Sep 17 00:00:00 2001 From: Wim Looman Date: Fri, 24 Nov 2023 14:07:23 +0100 Subject: [PATCH] Improve formatting of `rustwide` log messages Enables `tracing-subscriber/tracing-log` feature to get its support for formatting based on these messages custom attributes, requires a workaround during registration to ignore the failure of `tracing-subscriber` to register its own `tracing-log` instance as we've already registered one through `rustwide`. --- Cargo.toml | 2 +- src/bin/cratesfyi.rs | 39 +++++++++++++++++++++++++++------------ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6f006435b..7d8dee145 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ sentry-tower = { version = "0.31.0", features = ["http"] } sentry-anyhow = { version = "0.31.0", features = ["backtrace"] } log = "0.4" tracing = "0.1.37" -tracing-subscriber = { version = "0.3.16", default-features = false, features = ["ansi", "fmt", "env-filter"] } +tracing-subscriber = { version = "0.3.16", default-features = false, features = ["ansi", "fmt", "env-filter", "tracing-log"] } tracing-log = "0.2.0" regex = "1" clap = { version = "4.0.22", features = [ "derive" ] } diff --git a/src/bin/cratesfyi.rs b/src/bin/cratesfyi.rs index abc694c1a..f4012b609 100644 --- a/src/bin/cratesfyi.rs +++ b/src/bin/cratesfyi.rs @@ -5,7 +5,7 @@ use std::path::PathBuf; use std::str::FromStr; use std::sync::Arc; -use anyhow::{anyhow, Context as _, Error, Result}; +use anyhow::{anyhow, ensure, Context as _, Error, Result}; use axum::async_trait; use clap::{Parser, Subcommand, ValueEnum}; use docs_rs::cdn::CdnBackend; @@ -24,7 +24,7 @@ use humantime::Duration; use once_cell::sync::OnceCell; use tokio::runtime::{Builder, Runtime}; use tracing_log::LogTracer; -use tracing_subscriber::{filter::Directive, prelude::*, EnvFilter}; +use tracing_subscriber::{filter::Directive, prelude::*, util::TryInitError, EnvFilter}; fn main() { // set the global log::logger for backwards compatibility @@ -40,16 +40,30 @@ fn main() { .from_env_lossy(), ); + // workaround https://github.com/tokio-rs/tracing/issues/2817 + // tracing fully initializes its own infrastructure before trying to set the global logger, + // so we can just ignore that failure + let check_tracing_result = |result: Result<(), TryInitError>| { + if let Err(err) = result { + // workaround https://github.com/tokio-rs/tracing/issues/2818 + ensure!(format!("{:?}", err).contains("SetLoggerError"), err); + } + Ok(()) + }; + let _sentry_guard = if let Ok(sentry_dsn) = env::var("SENTRY_DSN") { - tracing_registry - .with(sentry_tracing::layer().event_filter(|md| { - if md.fields().field("reported_to_sentry").is_some() { - sentry_tracing::EventFilter::Ignore - } else { - sentry_tracing::default_event_filter(md) - } - })) - .init(); + check_tracing_result( + tracing_registry + .with(sentry_tracing::layer().event_filter(|md| { + if md.fields().field("reported_to_sentry").is_some() { + sentry_tracing::EventFilter::Ignore + } else { + sentry_tracing::default_event_filter(md) + } + })) + .try_init(), + ) + .unwrap(); Some(sentry::init(( sentry_dsn, @@ -65,7 +79,8 @@ fn main() { .add_integration(sentry_panic::PanicIntegration::default()), ))) } else { - tracing_registry.init(); + check_tracing_result(tracing_registry.try_init()).unwrap(); + None };