Builder

Struct Builder 

Source
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

Source

pub fn new() -> Self

Creates a new builder with default settings.

The default log level is LevelFilter::WARN.

Source

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);
Source

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);
Source

pub fn filter<F>(self, filter: F) -> Self
where F: Fn(&Metadata<'_>) -> bool + Send + Sync + 'static,

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>();
Source

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>();
Source

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>();
Source

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>();
Source

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>();
Source

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 kilobytes
  • MaxFileSize::mb(10) - 10 megabytes
  • MaxFileSize::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>();
Source

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>();
Source

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>();
Source

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>();
Source

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>();
Source

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>();
Source

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>();
Source

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>();
Source

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>();
Source

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>();
Source

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>();
Source

pub fn clear_targets(self) -> Self

Removes all configured log targets.

Use this followed by target() to build a custom set of targets from scratch.

§Example
use tauri_plugin_tracing::{Builder, Target};

// Start fresh and only log to webview
Builder::new()
    .clear_targets()
    .target(Target::Webview)
    .build::<tauri::Wry>();
Source

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"))
Source

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 */ }
    }
}
Source

pub fn configured_rotation(&self) -> Rotation

Returns the configured rotation period for file logging.

Source

pub fn configured_rotation_strategy(&self) -> RotationStrategy

Returns the configured rotation strategy for file logging.

Source

pub fn configured_max_file_size(&self) -> Option<MaxFileSize>

Returns the configured maximum file size for rotation, if set.

Source

pub fn configured_timezone_strategy(&self) -> TimezoneStrategy

Returns the configured timezone strategy for timestamps.

Source

pub fn configured_format(&self) -> LogFormat

Returns the configured log format style.

Source

pub fn configured_format_options(&self) -> FormatOptions

Returns the configured format options.

Source

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"))
Source

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"))

Trait Implementations§

Source§

impl Default for Builder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more