|
| 1 | +--- |
| 2 | +title: Logging(ログ記録) |
| 3 | +description: 設定可能なログ記録。 |
| 4 | +plugin: log |
| 5 | +i18nReady: true |
| 6 | +--- |
| 7 | + |
| 8 | +import PluginLinks from '@components/PluginLinks.astro'; |
| 9 | +import Compatibility from '@components/plugins/Compatibility.astro'; |
| 10 | + |
| 11 | +import { Tabs, TabItem, Steps } from '@astrojs/starlight/components'; |
| 12 | +import CommandTabs from '@components/CommandTabs.astro'; |
| 13 | +import PluginPermissions from '@components/PluginPermissions.astro'; |
| 14 | +import TranslationNote from '@components/i18n/TranslationNote.astro'; |
| 15 | + |
| 16 | +<TranslationNote lang="ja"> |
| 17 | + |
| 18 | +**Plugin 説明内容の英語表記部分について** Plugin の各章は、原文データからページ内容の一部が自動生成されているため、英語表記のままの部分があります。 |
| 19 | + |
| 20 | +</TranslationNote> |
| 21 | + |
| 22 | +<PluginLinks plugin={frontmatter.plugin} /> |
| 23 | + |
| 24 | +Tauri アプリ用の設定可能なログ記録。 |
| 25 | + |
| 26 | +## 対応プラットフォーム |
| 27 | + |
| 28 | +<Compatibility plugin={frontmatter.plugin} /> |
| 29 | + |
| 30 | +## セットアップ |
| 31 | + |
| 32 | +はじめに、「log」プラグインをインストールしてください。 |
| 33 | + |
| 34 | +<Tabs> |
| 35 | + <TabItem label="自動で設定" > |
| 36 | + |
| 37 | + 自分のプロジェクトのパッケージ・マネージャーを使用して依存関係を追加します: |
| 38 | + |
| 39 | + { ' ' } |
| 40 | + |
| 41 | + <CommandTabs |
| 42 | + npm="npm run tauri add log" |
| 43 | + yarn="yarn run tauri add log" |
| 44 | + pnpm="pnpm tauri add log" |
| 45 | + deno="deno task tauri add log" |
| 46 | + bun="bun tauri add log" |
| 47 | + cargo="cargo tauri add log" |
| 48 | + /> |
| 49 | + |
| 50 | + </TabItem> |
| 51 | + <TabItem label = "手動で設定"> |
| 52 | + <Steps> |
| 53 | + |
| 54 | + 1. `src-tauri` フォルダで次のコマンドを実行して、このプラグインを `Cargo.toml` 内のプロジェクトの依存関係に追加します: |
| 55 | + |
| 56 | + ```sh frame=none |
| 57 | + cargo add tauri-plugin-log |
| 58 | + ``` |
| 59 | + |
| 60 | + 2. 追加したプラグインを初期化するために `lib.rs` を修正します: |
| 61 | + |
| 62 | + ```rust title="src-tauri/src/lib.rs" ins={4} |
| 63 | + #[cfg_attr(mobile, tauri::mobile_entry_point)] |
| 64 | + pub fn run() { |
| 65 | + tauri::Builder::default() |
| 66 | + .plugin(tauri_plugin_log::Builder::new().build()) |
| 67 | + .run(tauri::generate_context!()) |
| 68 | + .expect("error while running tauri application"); |
| 69 | + } |
| 70 | + ``` |
| 71 | + |
| 72 | + 3. お好みの JavaScript パッケージ・マネージャーを使用して、「JavaScript Guest」バインディングをインストールします: |
| 73 | + |
| 74 | + <CommandTabs |
| 75 | + npm = "npm install @tauri-apps/plugin-log" |
| 76 | + yarn = "yarn add @tauri-apps/plugin-log" |
| 77 | + pnpm = "pnpm add @tauri-apps/plugin-log" |
| 78 | + deno = "deno add npm:@tauri-apps/plugin-log" |
| 79 | + bun = "bun add @tauri-apps/plugin-log" |
| 80 | + /> |
| 81 | + |
| 82 | + </Steps> |
| 83 | + |
| 84 | + </TabItem> |
| 85 | +</Tabs> |
| 86 | + |
| 87 | +## 使用法 |
| 88 | + |
| 89 | + <Steps> |
| 90 | + |
| 91 | +1. まず、プラグインを Tauri に登録する必要があります。 |
| 92 | + |
| 93 | + ```rust title="src-tauri/src/lib.rs" {1} {6-14} |
| 94 | + use tauri_plugin_log::{Target, TargetKind}; |
| 95 | + |
| 96 | + #[cfg_attr(mobile, tauri::mobile_entry_point)] |
| 97 | + pub fn run() { |
| 98 | + tauri::Builder::default() |
| 99 | + .plugin(tauri_plugin_log::Builder::new().build()) |
| 100 | + .run(tauri::generate_context!()) |
| 101 | + .expect("error while running tauri application"); |
| 102 | + } |
| 103 | + ``` |
| 104 | + |
| 105 | +2. すると、プラグインのすべての API は JavaScript ゲスト・バインディングを通じて利用できるようになります: |
| 106 | + |
| 107 | + ```javascript |
| 108 | + import { |
| 109 | + warn, |
| 110 | + debug, |
| 111 | + trace, |
| 112 | + info, |
| 113 | + error, |
| 114 | + attachConsole, |
| 115 | + attachLogger, |
| 116 | + } from '@tauri-apps/plugin-log'; |
| 117 | + // `"withGlobalTauri": true` を使用する場合は、 |
| 118 | + // const { warn, debug, trace, info, error, attachConsole, attachLogger } = window.__TAURI__.log; を使用できます |
| 119 | + ``` |
| 120 | + |
| 121 | + </Steps> |
| 122 | + |
| 123 | +## ログの記録 |
| 124 | + |
| 125 | +<Tabs syncKey="lang"> |
| 126 | + <TabItem label="JavaScript"> |
| 127 | +プラグインの `warn`、`debug`、`trace`、`info`、`error` API のいずれかを使用して、JavaScript コードから「ログ記録」を生成します。 |
| 128 | + |
| 129 | +```js |
| 130 | +import { warn, debug, trace, info, error } from '@tauri-apps/plugin-log'; |
| 131 | + |
| 132 | +trace('Trace'); |
| 133 | +info('Info'); |
| 134 | +error('Error'); |
| 135 | +``` |
| 136 | + |
| 137 | +すべての `console` メッセージを「log」プラグインに自動転送するには、次のように書き換えます: |
| 138 | + |
| 139 | +```ts |
| 140 | +import { warn, debug, trace, info, error } from '@tauri-apps/plugin-log'; |
| 141 | + |
| 142 | +function forwardConsole( |
| 143 | + fnName: 'log' | 'debug' | 'info' | 'warn' | 'error', |
| 144 | + logger: (message: string) => Promise<void> |
| 145 | +) { |
| 146 | + const original = console[fnName]; |
| 147 | + console[fnName] = (message) => { |
| 148 | + original(message); |
| 149 | + logger(message); |
| 150 | + }; |
| 151 | +} |
| 152 | + |
| 153 | +forwardConsole('log', trace); |
| 154 | +forwardConsole('debug', debug); |
| 155 | +forwardConsole('info', info); |
| 156 | +forwardConsole('warn', warn); |
| 157 | +forwardConsole('error', error); |
| 158 | +``` |
| 159 | + |
| 160 | +</TabItem> |
| 161 | + |
| 162 | +<TabItem label="Rust"> |
| 163 | +Rust 側で自分のログを作成するには、[`log` crate] を使用します: |
| 164 | + |
| 165 | +```rust |
| 166 | +log::error!("something bad happened!"); |
| 167 | +log::info!("Tauri is awesome!"); |
| 168 | +``` |
| 169 | + |
| 170 | +[`log` crate] を `Cargo.toml` ファイルに追加する必要があることに注意してください。 |
| 171 | + |
| 172 | +```toml |
| 173 | +[dependencies] |
| 174 | +log = "0.4" |
| 175 | +``` |
| 176 | + |
| 177 | +</TabItem> |
| 178 | +</Tabs> |
| 179 | + |
| 180 | +## ログの対象(ターゲット) |
| 181 | + |
| 182 | +「log」プラグイン・ビルダーには、すべてのアプリケーション・ログの共通の送り先を設定できる `targets` 関数があります。 |
| 183 | + |
| 184 | +:::note |
| 185 | +デフォルトでは、このプラグインは「stdout」(標準出力)と「アプリケーション・ログ・ディレクトリ」内のファイルにログを記録します。 |
| 186 | +自分のログ・ターゲットだけを使用するには、`clear_targets` を呼び出してください。 |
| 187 | + |
| 188 | +```rust |
| 189 | +tauri_plugin_log::Builder::new() |
| 190 | +.clear_targets() |
| 191 | +.build() |
| 192 | +``` |
| 193 | + |
| 194 | +::: |
| 195 | + |
| 196 | +### ログのターミナル出力 |
| 197 | + |
| 198 | +すべてのログをターミナルに転送するには、`Stdout` または `Stderr` をターゲットとして有効化します。 |
| 199 | + |
| 200 | +```rust |
| 201 | +tauri_plugin_log::Builder::new() |
| 202 | + .target(tauri_plugin_log::Target::new( |
| 203 | + tauri_plugin_log::TargetKind::Stdout, |
| 204 | + )) |
| 205 | + .build() |
| 206 | +``` |
| 207 | + |
| 208 | +このターゲットはデフォルトで有効化されています。 |
| 209 | + |
| 210 | +### Webview コンソールへのログ出力 |
| 211 | + |
| 212 | +Webview コンソールにすべての Rust ログを表示するには、`Webview` ターゲットを有効にし、フロントエンドで `attachConsole` を実行します: |
| 213 | + |
| 214 | +```rust |
| 215 | +tauri_plugin_log::Builder::new() |
| 216 | + .target(tauri_plugin_log::Target::new( |
| 217 | + tauri_plugin_log::TargetKind::Webview, |
| 218 | + )) |
| 219 | + .build() |
| 220 | +``` |
| 221 | + |
| 222 | +```js |
| 223 | +import { attachConsole } from '@tauri-apps/plugin-log'; |
| 224 | +const detach = await attachConsole(); |
| 225 | +// コンソールへのログ出力が不要になった場合には、detach() を呼び出してください。 |
| 226 | +``` |
| 227 | + |
| 228 | +### 永続ログ |
| 229 | + |
| 230 | +すべてのログをファイルに書き込むには、`LogDir` または `Folder` ターゲットのいずれかを使用します。 |
| 231 | + |
| 232 | +<TranslationNote lang="ja"> |
| 233 | + |
| 234 | +**永続ログ** persisting log: (通常 persistent log) システムやアプリケーションが終了・再起動してもログ・データの履歴を保持する仕組み。 |
| 235 | + |
| 236 | +</TranslationNote> |
| 237 | + |
| 238 | +- `LogDir`: |
| 239 | + |
| 240 | +```rust |
| 241 | +tauri_plugin_log::Builder::new() |
| 242 | + .target(tauri_plugin_log::Target::new( |
| 243 | + tauri_plugin_log::TargetKind::LogDir { |
| 244 | + file_name: Some("logs".to_string()), |
| 245 | + }, |
| 246 | + )) |
| 247 | + .build() |
| 248 | +``` |
| 249 | + |
| 250 | +`LogDir` ターゲットを使用すると、すべてのログは推奨されるログ・ディレクトリに保存されます。 |
| 251 | +次の表は、プラットフォームごとのログの場所を示しています: |
| 252 | + |
| 253 | +| Platform | Value | 設定例 | |
| 254 | +| -------- | ---------------------------------------------------------------------------------------- | ------------------------------------------------- | |
| 255 | +| Linux | `$XDG_DATA_HOME/{bundleIdentifier}/logs` or `$HOME/.local/share/{bundleIdentifier}/logs` | `/home/alice/.local/share/com.tauri.dev/logs` | |
| 256 | +| macOS | `{homeDir}/Library/Logs/{bundleIdentifier}` | `/Users/Alice/Library/Logs/com.tauri.dev` | |
| 257 | +| Windows | `{FOLDERID_LocalAppData}/{bundleIdentifier}/logs` | `C:\Users\Alice\AppData\Local\com.tauri.dev\logs` | |
| 258 | + |
| 259 | +- `Folder`: |
| 260 | + |
| 261 | +`Folder` ターゲットを使用すると、ファイル・システム内のカスタムな場所にログを書き込むことができます。 |
| 262 | + |
| 263 | +```rust |
| 264 | +tauri_plugin_log::Builder::new() |
| 265 | + .target(tauri_plugin_log::Target::new( |
| 266 | + tauri_plugin_log::TargetKind::Folder { |
| 267 | + path: std::path::PathBuf::from("/path/to/logs"), |
| 268 | + file_name: None, |
| 269 | + }, |
| 270 | + )) |
| 271 | + .build() |
| 272 | +``` |
| 273 | + |
| 274 | +デフォルトの `file_name` は「アプリケーション名」です。 |
| 275 | + |
| 276 | +#### ログ・ファイルの動作設定 |
| 277 | + |
| 278 | +デフォルトでは、ログ・ファイルは最大サイズに到達すると破棄されます。 |
| 279 | +最大ファイル・サイズは、ビルダーの `max_file_size` 関数を使用して設定できます: |
| 280 | + |
| 281 | +```rust |
| 282 | +tauri_plugin_log::Builder::new() |
| 283 | + .max_file_size(50_000 /* bytes */) |
| 284 | + .build() |
| 285 | +``` |
| 286 | + |
| 287 | +Tauri は、ログ・ファイルがサイズ上限に達したときに、以前のファイルを破棄するのではなく、自動的にログ・ファイルを入れ替えます。 |
| 288 | +この動作は `rotation_strategy`(入れ替え方式)を使用して設定できます: |
| 289 | + |
| 290 | +```rust |
| 291 | +tauri_plugin_log::Builder::new() |
| 292 | + .rotation_strategy(tauri_plugin_log::RotationStrategy::KeepAll) |
| 293 | + .build() |
| 294 | +``` |
| 295 | + |
| 296 | +### ログ・フィルター |
| 297 | + |
| 298 | +デフォルトでは**すべての**ログが処理されます。ログの量を減らし、関連する情報のみをフィルタリングする方法がいくつかあります。 |
| 299 | + |
| 300 | +### 最大ログ・レベル |
| 301 | + |
| 302 | +最大ログ・レベルを設定するには、`level` 関数を使用します: |
| 303 | + |
| 304 | +```rust |
| 305 | +tauri_plugin_log::Builder::new() |
| 306 | + .level(log::LevelFilter::Info) |
| 307 | + .build() |
| 308 | +``` |
| 309 | + |
| 310 | +上記の設定例では、「デバッグ・ログ」と「トレース・ログ」は _info(情報)_ よりもレベルが低いため破棄されます。 |
| 311 | + |
| 312 | +個々のモジュールごとにそれぞれ最大レベルを定義することもできます: |
| 313 | + |
| 314 | +```rust |
| 315 | +tauri_plugin_log::Builder::new() |
| 316 | + .level(log::LevelFilter::Info) |
| 317 | + // コマンド・モジュールのみ詳細ログ |
| 318 | + .level_for("my_crate_name::commands", log::LevelFilter::Trace) |
| 319 | + .build() |
| 320 | +``` |
| 321 | + |
| 322 | +これらの API は [`log` crate] を使用するため、`Cargo.toml` ファイルに追加する必要があることに注意してください: |
| 323 | + |
| 324 | +```toml |
| 325 | +[dependencies] |
| 326 | +log = "0.4" |
| 327 | +``` |
| 328 | + |
| 329 | +### ターゲット・フィルター |
| 330 | + |
| 331 | +メタデータをチェックして不要なログを破棄するように `filter` 関数を定義できます: |
| 332 | + |
| 333 | +```rust |
| 334 | +tauri_plugin_log::Builder::new() |
| 335 | + // ターゲット "hyper" のログを除外します |
| 336 | + .filter(|metadata| metadata.target() != "hyper") |
| 337 | + .build() |
| 338 | +``` |
| 339 | + |
| 340 | +### 書式設定(フォーマット) |
| 341 | + |
| 342 | +「log」プラグインは、各ログ・レコードを `DATE[TARGET][LEVEL] MESSAGE` のようなフォーマットに書式設定します。 |
| 343 | +カスタム・フォーマットの機能は `format` で提供されます。 |
| 344 | + |
| 345 | +```rust |
| 346 | +tauri_plugin_log::Builder::new() |
| 347 | + .format(|out, message, record| { |
| 348 | + out.finish(format_args!( |
| 349 | + "[{} {}] {}", |
| 350 | + record.level(), |
| 351 | + record.target(), |
| 352 | + message |
| 353 | + )) |
| 354 | + }) |
| 355 | + .build() |
| 356 | +``` |
| 357 | + |
| 358 | +#### ログの日付 |
| 359 | + |
| 360 | +デフォルトでは、「log」プラグインは UTC タイムゾーンを使用して日付を指定します。 |
| 361 | +ただし、`timezone_strategy` を使用してローカル・タイムゾーンを使用するように設定できます。 |
| 362 | + |
| 363 | +<TranslationNote lang="ja"> |
| 364 | + |
| 365 | +**UTC**: 協定世界時(coordinated universal time)。所謂「国際標準時」。日本標準時は、「UTC+9 時間」となります。《[wikipedia](https://ja.wikipedia.org/wiki/協定世界時)》 |
| 366 | + |
| 367 | +</TranslationNote> |
| 368 | + |
| 369 | +```rust |
| 370 | +tauri_plugin_log::Builder::new() |
| 371 | + .timezone_strategy(tauri_plugin_log::TimezoneStrategy::UseLocal) |
| 372 | + .build() |
| 373 | +``` |
| 374 | + |
| 375 | +## アクセス権限 Permissions |
| 376 | + |
| 377 | +デフォルトでは、潜在的に危険なプラグイン・コマンドとそのスコープ(有効範囲)はすべてブロックされており、アクセスできません。これらを有効にするには、`capabilities` 設定でアクセス権限を変更する必要があります。 |
| 378 | + |
| 379 | +詳細については「[セキュリティ・レベル Capabilities](/ja/security/capabilities/)」の章を参照してください。また、プラグインのアクセス権限を設定するには「[プライグン・アクセス権の使用](/ja/learn/security/using-plugin-permissions/)」の章のステップ・バイ・ステップ・ガイドを参照してください。 |
| 380 | + |
| 381 | +```json title="src-tauri/capabilities/default.json" ins={6} |
| 382 | +{ |
| 383 | + "$schema": "../gen/schemas/desktop-schema.json", |
| 384 | + "identifier": "main-capability", |
| 385 | + "description": "Capability for the main window", |
| 386 | + "windows": ["main"], |
| 387 | + "permissions": ["log:default"] |
| 388 | +} |
| 389 | +``` |
| 390 | + |
| 391 | +<PluginPermissions plugin={frontmatter.plugin} /> |
| 392 | + |
| 393 | +[`log` crate]: https://crates.io/crates/log |
| 394 | + |
| 395 | +<div style="text-align: right;"> |
| 396 | + 【※ この日本語版は、「Jul 3, 2025 英語版」に基づいています】 |
| 397 | +</div> |
0 commit comments