Logging
Configurable logging for your Tauri app.
Supported Platforms
- Windows
- Linux
- macOS
- iOS
Setup
This plugin requires a Rust version of at least 1.75
Install the log plugin to get started.
Use your project’s package manager to add the dependency:
npm run tauri add log
yarn run tauri add log
pnpm tauri add log
cargo tauri add log
-
Install the log plugin by adding the following to your
Cargo.toml
file:src-tauri/Cargo.toml [dependencies]tauri-plugin-log = "2.0.0-beta"# alternatively with Git:tauri-plugin-log = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" } -
Modify
lib.rs
to initialize the plugin:src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]fn run() {tauri::Builder::default().plugin(tauri_plugin_log::Builder::new().build()).run(tauri::generate_context!()).expect("error while running tauri application");} -
Install the JavaScript Guest bindings using your preferred JavaScript package manager:
npm install @tauri-apps/plugin-logyarn add @tauri-apps/plugin-logpnpm add @tauri-apps/plugin-log
Usage
-
First, you need to register the plugin with Tauri.
src-tauri/src/lib.rs use tauri_plugin_log::{Target, TargetKind};#[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().plugin(tauri_plugin_log::Builder::new().targets([Target::new(TargetKind::Stdout),Target::new(TargetKind::LogDir { file_name: None }),Target::new(TargetKind::Webview),]).build(),).run(tauri::generate_context!()).expect("error while running tauri application");} -
Afterwards, all the plugin’s APIs are available through the JavaScript guest bindings:
import { trace, info, error, attachConsole } from '@tauri-apps/plugin-log';// with TargetKind::Webview enabled, this function will print logs to the browser consoleconst detach = await attachConsole();trace('Trace');info('Info');error('Error');// detach the browser console from the log streamdetach();
Permissions
By default, all plugin commands are blocked and cannot be accessed. You must define a list of permissions in your capabilities
configuration.
See Permissions Overview for more information.
{ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "main-capability", "description": "Capability for the main window", "windows": ["main"], "permissions": ["log:default"]}
Permission | Description |
---|---|
log:default | Allows the log command. |
log:allow-log | Enables the log command without any pre-configured scope. |
log:deny-log | Denies the log command without any pre-configured scope. |
© 2024 Tauri Contributors. CC-BY / MIT