Skip to content

Commit

Permalink
Improve formatting of rustwide log messages
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
Nemo157 committed Nov 24, 2023
1 parent d7bfe85 commit bc63255
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ] }
Expand Down
39 changes: 27 additions & 12 deletions src/bin/cratesfyi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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
};

Expand Down

0 comments on commit bc63255

Please sign in to comment.