pub struct Builder { /* private fields */ }Expand description
Builder for configuring and creating the tracing plugin.
Use this builder to customize logging behavior before registering the plugin with your Tauri application.
§Example
use tauri_plugin_tracing::{Builder, LevelFilter};
let plugin = Builder::new()
.with_max_level(LevelFilter::DEBUG)
.with_target("hyper", LevelFilter::WARN) // Reduce noise from hyper
.with_target("my_app", LevelFilter::TRACE) // Verbose logging for your app
.build::<tauri::Wry>();Implementations§
Source§impl Builder
impl Builder
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new builder with default settings.
The default log level is LevelFilter::WARN.
Sourcepub fn with_max_level(self, max_level: LevelFilter) -> Self
pub fn with_max_level(self, max_level: LevelFilter) -> Self
Sets the maximum log level.
Events more verbose than this level will be filtered out.
§Example
Builder::new().with_max_level(LevelFilter::DEBUG);Sourcepub fn with_target(self, target: &str, level: LevelFilter) -> Self
pub fn with_target(self, target: &str, level: LevelFilter) -> Self
Sets the log level for a specific target (module path).
This allows fine-grained control over logging verbosity for different parts of your application or dependencies.
§Example
Builder::new()
.with_max_level(LevelFilter::INFO)
.with_target("my_app::database", LevelFilter::DEBUG)
.with_target("hyper", LevelFilter::WARN);Sourcepub fn filter<F>(self, filter: F) -> Self
pub fn filter<F>(self, filter: F) -> Self
Sets a custom filter function for metadata-based log filtering.
The filter function receives the metadata for each log event and returns
true if the event should be logged. This filter is applied in addition
to the level and target filters configured via with_max_level()
and with_target().
Only applies when using with_default_subscriber().
For custom subscribers, use tracing_subscriber::filter::filter_fn() directly.
§Example
use tauri_plugin_tracing::Builder;
// Filter out logs from a specific module
Builder::new()
.filter(|metadata| {
metadata.target() != "noisy_crate::spammy_module"
})
.with_default_subscriber()
.build::<tauri::Wry>();
// Only log events (not spans)
Builder::new()
.filter(|metadata| metadata.is_event())
.with_default_subscriber()
.build::<tauri::Wry>();Sourcepub fn with_layer(self, layer: BoxedLayer) -> Self
pub fn with_layer(self, layer: BoxedLayer) -> Self
Adds a custom tracing layer to the subscriber.
Use this to integrate additional tracing functionality (e.g., OpenTelemetry, Sentry, custom metrics) with the plugin-managed subscriber.
Only applies when using with_default_subscriber().
Note: Only one custom layer is supported. Calling this multiple times will
replace the previous layer. To use multiple custom layers, compose them
with tracing_subscriber::layer::Layered before passing to this method.
§Example
use tauri_plugin_tracing::Builder;
use tracing_subscriber::Layer;
// Add a custom layer (e.g., a secondary fmt layer or OpenTelemetry)
let custom_layer = tracing_subscriber::fmt::layer().boxed();
Builder::new()
.with_layer(custom_layer)
.with_default_subscriber()
.build::<tauri::Wry>();Sourcepub fn with_file_logging(self) -> Self
pub fn with_file_logging(self) -> Self
Enables file logging to the platform-standard log directory.
Log files rotate daily with the naming pattern app.YYYY-MM-DD.log.
Platform log directories:
- macOS:
~/Library/Logs/{bundle_identifier} - Linux:
~/.local/share/{bundle_identifier}/logs - Windows:
%LOCALAPPDATA%/{bundle_identifier}/logs
This is a convenience method equivalent to calling
.target(Target::LogDir { file_name: None }).
§Example
Builder::new()
.with_max_level(LevelFilter::DEBUG)
.with_file_logging()
.build::<tauri::Wry>();Sourcepub fn with_rotation(self, rotation: Rotation) -> Self
pub fn with_rotation(self, rotation: Rotation) -> Self
Sets the rotation period for log files.
This controls how often new log files are created. Only applies when file logging is enabled.
§Example
use tauri_plugin_tracing::{Builder, Rotation};
Builder::new()
.with_file_logging()
.with_rotation(Rotation::Hourly) // Rotate every hour
.build::<tauri::Wry>();Sourcepub fn with_rotation_strategy(self, strategy: RotationStrategy) -> Self
pub fn with_rotation_strategy(self, strategy: RotationStrategy) -> Self
Sets the retention strategy for rotated log files.
This controls how many old log files are kept. Cleanup happens when the application starts.
§Example
use tauri_plugin_tracing::{Builder, RotationStrategy};
Builder::new()
.with_file_logging()
.with_rotation_strategy(RotationStrategy::KeepSome(7)) // Keep 7 files
.build::<tauri::Wry>();Sourcepub fn with_max_file_size(self, size: MaxFileSize) -> Self
pub fn with_max_file_size(self, size: MaxFileSize) -> Self
Sets the maximum file size before rotating.
When set, log files will rotate when they reach this size, in addition
to any time-based rotation configured via with_rotation().
Use MaxFileSize for convenient size specification:
MaxFileSize::kb(100)- 100 kilobytesMaxFileSize::mb(10)- 10 megabytesMaxFileSize::gb(1)- 1 gigabyte
§Example
use tauri_plugin_tracing::{Builder, MaxFileSize};
// Rotate when file reaches 10 MB
Builder::new()
.with_file_logging()
.with_max_file_size(MaxFileSize::mb(10))
.build::<tauri::Wry>();Sourcepub fn with_timezone_strategy(self, strategy: TimezoneStrategy) -> Self
pub fn with_timezone_strategy(self, strategy: TimezoneStrategy) -> Self
Sets the timezone strategy for log timestamps.
Controls whether timestamps are displayed in UTC or local time.
The default is TimezoneStrategy::Utc.
§Example
use tauri_plugin_tracing::{Builder, TimezoneStrategy};
// Use local time for timestamps
Builder::new()
.with_timezone_strategy(TimezoneStrategy::Local)
.build::<tauri::Wry>();Sourcepub fn with_format(self, format: LogFormat) -> Self
pub fn with_format(self, format: LogFormat) -> Self
Sets the log output format style.
Controls the overall structure of log output. The default is LogFormat::Full.
Only applies when using with_default_subscriber().
§Example
use tauri_plugin_tracing::{Builder, LogFormat};
// Use compact format for shorter lines
Builder::new()
.with_format(LogFormat::Compact)
.with_default_subscriber()
.build::<tauri::Wry>();Sourcepub fn with_file(self, show: bool) -> Self
pub fn with_file(self, show: bool) -> Self
Sets whether to include the source file path in log output.
When enabled, logs will show which file the log event originated from.
Default is false.
Only applies when using with_default_subscriber().
§Example
Builder::new()
.with_file(true)
.with_default_subscriber()
.build::<tauri::Wry>();Sourcepub fn with_line_number(self, show: bool) -> Self
pub fn with_line_number(self, show: bool) -> Self
Sets whether to include the source line number in log output.
When enabled, logs will show which line number the log event originated from.
Default is false.
Only applies when using with_default_subscriber().
§Example
Builder::new()
.with_line_number(true)
.with_default_subscriber()
.build::<tauri::Wry>();Sourcepub fn with_thread_ids(self, show: bool) -> Self
pub fn with_thread_ids(self, show: bool) -> Self
Sets whether to include the current thread ID in log output.
When enabled, logs will show the ID of the thread that emitted the event.
Default is false.
Only applies when using with_default_subscriber().
§Example
Builder::new()
.with_thread_ids(true)
.with_default_subscriber()
.build::<tauri::Wry>();Sourcepub fn with_thread_names(self, show: bool) -> Self
pub fn with_thread_names(self, show: bool) -> Self
Sets whether to include the current thread name in log output.
When enabled, logs will show the name of the thread that emitted the event.
Default is false.
Only applies when using with_default_subscriber().
§Example
Builder::new()
.with_thread_names(true)
.with_default_subscriber()
.build::<tauri::Wry>();Sourcepub fn with_target_display(self, show: bool) -> Self
pub fn with_target_display(self, show: bool) -> Self
Sets whether to include the log target (module path) in log output.
When enabled, logs will show which module/target emitted the event.
Default is true.
Only applies when using with_default_subscriber().
§Example
// Disable target display for cleaner output
Builder::new()
.with_target_display(false)
.with_default_subscriber()
.build::<tauri::Wry>();Sourcepub fn with_level(self, show: bool) -> Self
pub fn with_level(self, show: bool) -> Self
Sets whether to include the log level in log output.
When enabled, logs will show the severity level (TRACE, DEBUG, INFO, etc.).
Default is true.
Only applies when using with_default_subscriber().
§Example
// Disable level display
Builder::new()
.with_level(false)
.with_default_subscriber()
.build::<tauri::Wry>();Sourcepub fn target(self, target: Target) -> Self
pub fn target(self, target: Target) -> Self
Adds a log output target.
By default, logs are sent to Target::Stdout and Target::Webview.
Use this method to add additional targets.
§Example
use tauri_plugin_tracing::{Builder, Target};
// Add file logging to the default targets
Builder::new()
.target(Target::LogDir { file_name: None })
.build::<tauri::Wry>();Sourcepub fn targets(self, targets: impl IntoIterator<Item = Target>) -> Self
pub fn targets(self, targets: impl IntoIterator<Item = Target>) -> Self
Sets the log output targets, replacing any previously configured targets.
By default, logs are sent to Target::Stdout and Target::Webview.
Use this method to completely replace the default targets.
§Example
use tauri_plugin_tracing::{Builder, Target};
// Log only to file and webview (no stdout)
Builder::new()
.targets([
Target::LogDir { file_name: None },
Target::Webview,
])
.build::<tauri::Wry>();
// Log only to stderr
Builder::new()
.targets([Target::Stderr])
.build::<tauri::Wry>();Sourcepub fn clear_targets(self) -> Self
pub fn clear_targets(self) -> Self
Sourcepub fn with_default_subscriber(self) -> Self
pub fn with_default_subscriber(self) -> Self
Enables the plugin to set up and register the global tracing subscriber.
By default, this plugin does not call tracing::subscriber::set_global_default(),
following the convention that libraries should not set globals. This allows your
application to compose its own subscriber with layers from multiple crates.
Call this method if you want the plugin to handle all tracing setup for you, using the configuration from this builder (log levels, targets, file logging, etc.).
§Example
// Let the plugin set up everything
tauri::Builder::default()
.plugin(
Builder::new()
.with_max_level(LevelFilter::DEBUG)
.with_file_logging()
.with_default_subscriber() // Opt-in to global subscriber
.build()
);
// .run(tauri::generate_context!("examples/default-subscriber/src-tauri/tauri.conf.json"))Sourcepub fn configured_targets(&self) -> &[Target]
pub fn configured_targets(&self) -> &[Target]
Returns the configured log output targets.
Use this when setting up your own subscriber to determine which layers to include based on the configured targets.
§Example
use tauri_plugin_tracing::{Builder, Target};
let builder = Builder::new()
.target(Target::LogDir { file_name: None });
for target in builder.configured_targets() {
match target {
Target::Stdout => { /* add stdout layer */ }
Target::Stderr => { /* add stderr layer */ }
Target::Webview => { /* add WebviewLayer */ }
Target::LogDir { .. } | Target::Folder { .. } => { /* add file layer */ }
}
}Sourcepub fn configured_rotation(&self) -> Rotation
pub fn configured_rotation(&self) -> Rotation
Returns the configured rotation period for file logging.
Sourcepub fn configured_rotation_strategy(&self) -> RotationStrategy
pub fn configured_rotation_strategy(&self) -> RotationStrategy
Returns the configured rotation strategy for file logging.
Sourcepub fn configured_max_file_size(&self) -> Option<MaxFileSize>
pub fn configured_max_file_size(&self) -> Option<MaxFileSize>
Returns the configured maximum file size for rotation, if set.
Sourcepub fn configured_timezone_strategy(&self) -> TimezoneStrategy
pub fn configured_timezone_strategy(&self) -> TimezoneStrategy
Returns the configured timezone strategy for timestamps.
Sourcepub fn configured_format(&self) -> LogFormat
pub fn configured_format(&self) -> LogFormat
Returns the configured log format style.
Sourcepub fn configured_format_options(&self) -> FormatOptions
pub fn configured_format_options(&self) -> FormatOptions
Returns the configured format options.
Sourcepub fn build_filter(&self) -> Targets
pub fn build_filter(&self) -> Targets
Returns the configured filter based on log level and per-target settings.
Use this when setting up your own subscriber to apply the same filtering
configured via with_max_level() and
with_target().
§Example
let builder = Builder::new()
.with_max_level(LevelFilter::DEBUG)
.with_target("hyper", LevelFilter::WARN);
let filter = builder.build_filter();
tauri::Builder::default()
.plugin(builder.build())
.setup(move |app| {
Registry::default()
.with(fmt::layer())
.with(WebviewLayer::new(app.handle().clone()))
.with(filter)
.init();
Ok(())
});
// .run(tauri::generate_context!("examples/default-subscriber/src-tauri/tauri.conf.json"))Sourcepub fn build<R: Runtime>(self) -> TauriPlugin<R>
pub fn build<R: Runtime>(self) -> TauriPlugin<R>
Builds and returns the configured Tauri plugin.
This consumes the builder and returns a TauriPlugin that can be
registered with your Tauri application.
§Example
tauri::Builder::default()
.plugin(Builder::new().build());
// .run(tauri::generate_context!("examples/default-subscriber/src-tauri/tauri.conf.json"))