Mountain/Binary/Build/
LoggingPlugin.rs

1//! # Logging Plugin Module
2//!
3//! Configures and creates the Tauri logging plugin with appropriate targets and
4//! filters.
5
6use log::LevelFilter;
7use tauri::plugin::TauriPlugin;
8use tauri_plugin_log::{RotationStrategy, Target, TargetKind, TimezoneStrategy};
9
10/// Creates and configures the logging plugin with multi-target output and level
11/// filtering.
12///
13/// # Arguments
14///
15/// * `LogLevel` - The desired log level (Trace, Debug, Info, Warn, Error)
16///
17/// # Returns
18///
19/// A configured `tauri_plugin_log::TauriPlugin` instance.
20///
21/// # Logging Strategy
22///
23/// - Release default: Info (low noise) unless RUST_LOG overrides
24/// - Debug default: Debug (high fidelity) unless RUST_LOG overrides
25/// - Very noisy dependencies are capped using level_for(...) and filter(...)
26///
27/// # Targets
28///
29/// - Stdout: Console output for development/terminal viewing
30/// - LogDir: Persistent log file (Mountain.log) in the app's log directory
31/// - Webview: Logs sent to the webview console for frontend debugging
32///
33/// # Noise Filtering
34///
35/// The following noisy dependencies are capped at Info level regardless of
36/// RUST_LOG:
37/// - hyper: HTTP library verbose logs
38/// - mio: Async I/O polling logs
39/// - tao: Windowing system logs
40/// - tracing: Structured logging internal logs
41///
42/// Additionally, the following targets are filtered out entirely:
43/// - polling: File watcher events (very noisy)
44/// - tokio_reactor: Async reactor events
45/// - want: Connection readiness logs
46pub fn LoggingPlugin<R:tauri::Runtime>(LogLevel:LevelFilter) -> TauriPlugin<R> {
47	tauri_plugin_log::Builder::new()
48		// Configure output targets
49		.targets([
50			Target::new(TargetKind::Stdout),
51			Target::new(TargetKind::LogDir {
52				file_name: Some("Mountain.log".into()),
53			}),
54			Target::new(TargetKind::Webview),
55		])
56		// Configure file rotation and timezone
57		.timezone_strategy(TimezoneStrategy::UseLocal)
58		.rotation_strategy(RotationStrategy::KeepAll)
59		// Set base log level
60		.level(LogLevel)
61		// Cap very noisy dependencies at Info level
62		.level_for("hyper", LevelFilter::Info)
63		.level_for("mio", LevelFilter::Info)
64		.level_for("tao", LevelFilter::Info)
65		.level_for("tracing", LevelFilter::Info)
66		// Filter out extremely noisy targets
67		.filter(|Metadata| {
68			!Metadata.target().starts_with("polling")
69				&& !Metadata.target().starts_with("tokio_reactor")
70				&& !Metadata.target().starts_with("want")
71		})
72		// Format logs with category-like structure: [LEVEL] [TARGET] message
73		.format(|out, message, record| {
74			out.finish(format_args!(
75				"[{:<5}] [{}] {}",
76				record.level(),
77				record.target(),
78				message
79			))
80		})
81		.build()
82}