Files
firezone/rust/logging/src/lib.rs
Thomas Eizinger 857bbf5d98 chore(connlib): introduce custom logging format (#7024)
This PR introduces a custom logging format for all Rust-components. It
is more or less a copy of `tracing_subscriber::fmt::format::Compact`
with the main difference that span-names don't get logged.

Spans are super useful because they allow us to record contextual
values, like the current connection ID, for a certain scope. What is IMO
less useful about them is that in the default formatter configuration,
active spans cause a right-drift of the actual log message.

The actual log message is still what most accurately describes, what
`connlib` is currently doing. Spans only add contextual information that
the reader may use for further understand what is happening. This
optional nature of the utility of spans IMO means that they should come
_after_ the actual log message.

Resolves: #7014.
2024-10-14 18:09:38 +00:00

65 lines
2.3 KiB
Rust

pub mod file;
mod format;
use tracing::subscriber::DefaultGuard;
use tracing_log::LogTracer;
use tracing_subscriber::{
filter::ParseError, fmt, layer::SubscriberExt as _, util::SubscriberInitExt, EnvFilter, Layer,
Registry,
};
pub use format::Format;
/// Registers a global subscriber with stdout logging and `additional_layer`
pub fn setup_global_subscriber<L>(additional_layer: L)
where
L: Layer<Registry> + Send + Sync,
{
let directives = std::env::var("RUST_LOG").unwrap_or_default();
let subscriber = Registry::default()
.with(additional_layer)
.with(fmt::layer().event_format(Format::new()))
.with(filter(&directives));
tracing::subscriber::set_global_default(subscriber).expect("Could not set global default");
LogTracer::init().unwrap();
}
/// Constructs an opinionated [`EnvFilter`] with some crates already silenced.
pub fn filter(directives: &str) -> EnvFilter {
try_filter(directives).unwrap()
}
/// Constructs an opinionated [`EnvFilter`] with some crates already silenced.
pub fn try_filter(directives: &str) -> Result<EnvFilter, ParseError> {
/// A filter directive that silences noisy crates.
///
/// For debugging, it is useful to set a catch-all log like `debug`.
/// This obviously creates a lot of logs from all kinds of crates.
/// For our usecase, logs from `netlink_proto` and other crates are very likely not what you want to see.
///
/// By prepending this directive to the active log filter, a simple directive like `debug` actually produces useful logs.
/// If necessary, you can still activate logs from these crates by restating them in your directive with a lower filter, i.e. `netlink_proto=debug`.
const IRRELEVANT_CRATES: &str = "netlink_proto=warn,os_info=warn,rustls=warn";
EnvFilter::try_new(format!("{IRRELEVANT_CRATES},{directives}"))
}
/// Initialises a logger to be used in tests.
pub fn test(directives: &str) -> DefaultGuard {
tracing_subscriber::fmt()
.with_test_writer()
.with_env_filter(directives)
.set_default()
}
pub fn test_global(directives: &str) {
tracing::subscriber::set_global_default(
tracing_subscriber::fmt()
.with_test_writer()
.with_env_filter(directives)
.finish(),
)
.ok();
}