Logging
Configurable logging for your Tauri app.
Supported Platforms
This plugin requires a Rust version of at least 1.77.2
Platform | Level | Notes |
---|---|---|
windows | ||
linux | ||
macos | ||
android | ||
ios |
Setup
Install the log plugin to get started.
Use your project’s package manager to add the dependency:
-
Run the following command in the
src-tauri
folder to add the plugin to the project’s dependencies inCargo.toml
: -
Modify
lib.rs
to initialize the plugin: -
Install the JavaScript Guest bindings using your preferred JavaScript package manager:
Usage
-
First, you need to register the plugin with Tauri.
-
Afterwards, all the plugin’s APIs are available through the JavaScript guest bindings:
Logging
Use one of the plugin’s warn
, debug
, trace
, info
or error
APIs to produce a log record from JavaScript code:
To automatically forward all console
messages to the log plugin you can rewrite them:
To create your own logs on the Rust side you can use the [log
crate]:
Note that the [log
crate] must be added to your Cargo.toml
file:
Log targets
The log plugin builder has a targets
function that lets you configure common destination of all your application logs.
Printing logs to the terminal
To forward all your logs to the terminal, enable the Stdout
or Stderr
targets:
This target is enabled by default.
Logging to the webview console
To view all your Rust logs in the webview console, enable the Webview
target and run attachConsole
in your frontend:
Persisting logs
To write all logs to a file, you can use either the LogDir
or the Folder
targets.
LogDir
:
When using the LogDir target, all logs are stored in the recommended log directory. The following table describes the location of the logs per platform:
Platform | Value | Example |
---|---|---|
Linux | {configDir}/{bundleIdentifier} | /home/alice/.config/com.tauri.dev |
macOS | {homeDir}/Library/Logs/{bundleIdentifier} | /Users/Alice/Library/Logs/com.tauri.dev |
Windows | {configDir}/{bundleIdentifier} | C:\Users\Alice\AppData\Roaming\com.tauri.dev |
Folder
:
The Folder target lets you write logs to a custom location in the filesystem.
The default file_name
is the application name.
Configuring log file behavior
By default the log file gets discarded when it reaches the maximum size.
The maximum file size can be configured via the builder’s max_file_size
function:
Tauri can automatically rotate your log file when it reaches the size limit instead of discarding the previous file.
This behavior can be configured using rotation_strategy
:
Filtering
By default all logs are processed. There are some mechanisms to reduce the amount of logs and filter only relevant information.
Maximum log level
To set a maximum log level, use the level
function:
In this example, debug and trace logs are discarded as they have a lower level than info.
It is also possible to define separate maximum levels for individual modules:
Note that these APIs use the [log
crate], which must be added to your Cargo.toml
file:
Target filter
A filter
function can be defined to discard unwanted logs by checking their metadata:
Formatting
The log plugin formats each log record as DATE[TARGET][LEVEL] MESSAGE
.
A custom format function can be provided with format
:
Log dates
By default the log plugin uses the UTC timezone to format dates
but you can configure it to use the local timezone with timezone_strategy
:
Permissions
By default, all plugin commands are blocked and cannot be accessed.
You must define a list of permissions in your capabilities
configuration.
See the Capabilities Overview for more information and the step by step guide to use plugin permissions.
Default Permission
Allows the log command
allow-log
Permission Table
Identifier | Description |
---|---|
|
Enables the log command without any pre-configured scope. |
|
Denies the log command without any pre-configured scope. |
© 2024 Tauri Contributors. CC-BY / MIT