|
| 1 | +//! Platform-agnostic logging configuration and filtering. |
| 2 | +//! |
| 3 | +//! This module provides logging types and filter logic that can be reused |
| 4 | +//! across platforms (WASM, iOS, Android, native). Each platform provides |
| 5 | +//! its own tracing Layer/writer — this module only handles configuration. |
| 6 | +
|
| 7 | +use serde::Deserialize; |
| 8 | +use tracing::{Level, Metadata}; |
| 9 | +use tracing_subscriber::fmt::format::FmtSpan; |
| 10 | + |
| 11 | +/// Logging verbosity level. |
| 12 | +#[derive(Debug, Default, Clone, Copy, Deserialize)] |
| 13 | +pub enum LoggingLevel { |
| 14 | + /// Disable all logging for this target. |
| 15 | + Off, |
| 16 | + /// Most verbose — includes all messages. |
| 17 | + Trace, |
| 18 | + /// Detailed debugging information. |
| 19 | + Debug, |
| 20 | + /// Informational messages (default). |
| 21 | + #[default] |
| 22 | + Info, |
| 23 | + /// Warnings only. |
| 24 | + Warn, |
| 25 | + /// Errors only. |
| 26 | + Error, |
| 27 | +} |
| 28 | + |
| 29 | +impl LoggingLevel { |
| 30 | + /// Returns true if this level disables all logging. |
| 31 | + pub fn is_off(&self) -> bool { |
| 32 | + matches!(self, LoggingLevel::Off) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl From<LoggingLevel> for Level { |
| 37 | + fn from(value: LoggingLevel) -> Self { |
| 38 | + match value { |
| 39 | + // Off maps to ERROR as a fallback, but is_off() should be checked first. |
| 40 | + LoggingLevel::Off => Level::ERROR, |
| 41 | + LoggingLevel::Trace => Level::TRACE, |
| 42 | + LoggingLevel::Debug => Level::DEBUG, |
| 43 | + LoggingLevel::Info => Level::INFO, |
| 44 | + LoggingLevel::Warn => Level::WARN, |
| 45 | + LoggingLevel::Error => Level::ERROR, |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/// Span lifecycle events to log. |
| 51 | +#[derive(Debug, Clone, Copy, Deserialize)] |
| 52 | +pub enum SpanEvent { |
| 53 | + /// Log when a span is created. |
| 54 | + New, |
| 55 | + /// Log when a span is closed. |
| 56 | + Close, |
| 57 | + /// Log when a span becomes active. |
| 58 | + Active, |
| 59 | +} |
| 60 | + |
| 61 | +impl From<SpanEvent> for FmtSpan { |
| 62 | + fn from(value: SpanEvent) -> Self { |
| 63 | + match value { |
| 64 | + SpanEvent::New => FmtSpan::NEW, |
| 65 | + SpanEvent::Close => FmtSpan::CLOSE, |
| 66 | + SpanEvent::Active => FmtSpan::ACTIVE, |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/// Top-level logging configuration. |
| 72 | +#[derive(Debug, Default, Clone, Deserialize)] |
| 73 | +pub struct LoggingConfig { |
| 74 | + /// Global default log level. |
| 75 | + pub level: Option<LoggingLevel>, |
| 76 | + /// Per-crate log level overrides. |
| 77 | + pub crate_filters: Option<Vec<CrateLogFilter>>, |
| 78 | + /// Which span lifecycle events to log. |
| 79 | + pub span_events: Option<Vec<SpanEvent>>, |
| 80 | +} |
| 81 | + |
| 82 | +/// Per-crate log level override. |
| 83 | +#[derive(Debug, Clone, Deserialize)] |
| 84 | +pub struct CrateLogFilter { |
| 85 | + /// Log level for this crate. |
| 86 | + pub level: LoggingLevel, |
| 87 | + /// Crate name to match (case-insensitive). |
| 88 | + pub name: String, |
| 89 | +} |
| 90 | + |
| 91 | +/// Creates a filter function from a [`LoggingConfig`]. |
| 92 | +/// |
| 93 | +/// The returned closure checks each tracing event's target against the |
| 94 | +/// configured crate filters (case-insensitive match on the first path |
| 95 | +/// segment). Events that don't match any filter use the global default level. |
| 96 | +pub fn filter(config: LoggingConfig) -> impl Fn(&Metadata) -> bool { |
| 97 | + let default_level = config.level.unwrap_or(LoggingLevel::Info); |
| 98 | + let crate_filters = config |
| 99 | + .crate_filters |
| 100 | + .unwrap_or_default() |
| 101 | + .into_iter() |
| 102 | + .map(|filter| (filter.name, filter.level)) |
| 103 | + .collect::<Vec<_>>(); |
| 104 | + |
| 105 | + move |meta| { |
| 106 | + let logging_level = if let Some(crate_name) = meta.target().split("::").next() { |
| 107 | + crate_filters |
| 108 | + .iter() |
| 109 | + .find_map(|(filter_name, filter_level)| { |
| 110 | + if crate_name.eq_ignore_ascii_case(filter_name) { |
| 111 | + Some(*filter_level) |
| 112 | + } else { |
| 113 | + None |
| 114 | + } |
| 115 | + }) |
| 116 | + .unwrap_or(default_level) |
| 117 | + } else { |
| 118 | + default_level |
| 119 | + }; |
| 120 | + |
| 121 | + // Off disables all logging for this target. |
| 122 | + if logging_level.is_off() { |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + meta.level() <= &Level::from(logging_level) |
| 127 | + } |
| 128 | +} |
0 commit comments