# Laravel Telescope

- [Introduction](#introduction)
- [Installation](#installation)
    - [Local Only Installation](#local-only-installation)
    - [Configuration](#configuration)
    - [Data Pruning](#data-pruning)
    - [Dashboard Authorization](#dashboard-authorization)
- [Upgrading Telescope](#upgrading-telescope)
- [Filtering](#filtering)
    - [Entries](#filtering-entries)
    - [Batches](#filtering-batches)
- [Tagging](#tagging)
- [Available Watchers](#available-watchers)
    - [Batch Watcher](#batch-watcher)
    - [Cache Watcher](#cache-watcher)
    - [Command Watcher](#command-watcher)
    - [Dump Watcher](#dump-watcher)
    - [Event Watcher](#event-watcher)
    - [Exception Watcher](#exception-watcher)
    - [Gate Watcher](#gate-watcher)
    - [HTTP Client Watcher](#http-client-watcher)
    - [Job Watcher](#job-watcher)
    - [Log Watcher](#log-watcher)
    - [Mail Watcher](#mail-watcher)
    - [Model Watcher](#model-watcher)
    - [Notification Watcher](#notification-watcher)
    - [Query Watcher](#query-watcher)
    - [Redis Watcher](#redis-watcher)
    - [Request Watcher](#request-watcher)
    - [Schedule Watcher](#schedule-watcher)
    - [View Watcher](#view-watcher)
- [Displaying User Avatars](#displaying-user-avatars)

<a name="introduction"></a>

## Introduction


[Laravel Telescope](https://github.com/laravel/telescope) は、ローカルの Laravel 開発環境の素晴らしいパートナーになります。 Telescope は、アプリケーションに送られるリクエスト、例外、ログ エントリ、データベース クエリ、キューに入れられたジョブ、メール、通知、キャッシュ操作、スケジュールされたタスク、変数ダンプなどに関する洞察を提供します。


<img src="https://laravel.com/img/docs/telescope-example.png"/>

<a name="installation"></a>

## Installation


Composer パッケージ マネージャーを使用して、Telescope を Laravel プロジェクトにインストールできます。

```shell
composer require laravel/telescope
```


Telescope をインストールした後、`telescope:install` Artisan コマンドを使用してそのアセットを公開します。 Telescope をインストールした後、Telescope のデータを保存するために必要なテーブルを作成するために、`migrate` コマンドも実行する必要があります。

```shell
php artisan telescope:install

php artisan migrate
```

<a name="migration-customization"></a>

#### Migration Customization


Telescope のデフォルトの移行を使用しない場合は、アプリケーションの `App\Providers\AppServiceProvider` クラスの `register` メソッドで `Telescope::ignoreMigrations` メソッドを呼び出す必要があります。次のコマンドを使用してデフォルトの移行をエクスポートできます: `php artisan vendor:publish --tag=telescope-migrations`

<a name="local-only-installation"></a>

### Local Only Installation


ローカル開発を支援するためにのみ Telescope を使用する予定の場合は、`--dev` フラグを使用して Telescope をインストールできます。

```shell
composer require laravel/telescope --dev

php artisan telescope:install

php artisan migrate
```


`telescope:install` を実行した後、アプリケーションの `config/app.php` 構成ファイルから `TelescopeServiceProvider` サービスプロバイダの登録を削除する必要があります。代わりに、`App\Providers\AppServiceProvider` クラスの `register` メソッドで Telescope のサービスプロバイダを手動で登録します。プロバイダを登録する前に、現在の環境が `local` であることを確認します。

```
/**
 * Register any application services.
 *
 * @return void
 */
public function register()
{
    if ($this->app->environment('local')) {
        $this->app->register(\Laravel\Telescope\TelescopeServiceProvider::class);
        $this->app->register(TelescopeServiceProvider::class);
    }
}
```


最後に、`composer.json` ファイルに以下を追加して、Telescope パッケージが [auto-discovered](/ja/php/docs/9.x/packages/#package-discovery) になるのを防ぐ必要もあります。

```json
"extra": {
    "laravel": {
        "dont-discover": [
            "laravel/telescope"
        ]
    }
},
```

<a name="configuration"></a>

### Configuration


Telescope のアセットを公開すると、そのプライマリ構成ファイルは `config/telescope.php` に配置されます。この設定ファイルを使用すると、[watcher options](#available-watchers) を設定できます。各構成オプションにはその目的の説明が含まれているため、このファイルをよく調べてください。


必要に応じて、`enabled` 構成オプションを使用して Telescope のデータ収集を完全に無効にすることができます。

```
'enabled' => env('TELESCOPE_ENABLED', true),
```

<a name="data-pruning"></a>

### Data Pruning


プルーニングを行わない場合、`telescope_entries` テーブルは非常に迅速にレコードを蓄積できます。これを軽減するには、[schedule](/ja/php/docs/9.x/scheduling/) `telescope:prune` Artisan コマンドを毎日実行する必要があります。

```
$schedule->command('telescope:prune')->daily();
```


デフォルトでは、24 時間より古いエントリはすべて削除されます。コマンドを呼び出すときに `hours` オプションを使用して、Telescope データを保持する期間を決定できます。たとえば、次のコマンドは 48 時間以上前に作成されたすべてのレコードを削除します。

```
$schedule->command('telescope:prune --hours=48')->daily();
```

<a name="dashboard-authorization"></a>

### Dashboard Authorization


Telescope ダッシュボードには、`/telescope` ルートでアクセスできます。デフォルトでは、`local` 環境でのみこのダッシュボードにアクセスできます。 `app/Providers/TelescopeServiceProvider.php` ファイル内には、[authorization gate](/ja/php/docs/9.x/authorization/#gates) 定義があります。この認可ゲートは、**非ローカル**環境での Telescope へのアクセスを制御します。必要に応じてこのゲートを自由に変更して、Telescope インストールへのアクセスを制限できます。

```
/**
 * Register the Telescope gate.
 *
 * This gate determines who can access Telescope in non-local environments.
 *
 * @return void
 */
protected function gate()
{
    Gate::define('viewTelescope', function ($user) {
        return in_array($user->email, [
            'taylor@laravel.com',
        ]);
    });
}
```

> [!WARNING]
> 運用環境では、`APP_ENV` 環境変数を `production` に必ず変更する必要があります。そうしないと、Telescope のインストールが公開されてしまいます。

<a name="upgrading-telescope"></a>

## Upgrading Telescope


Telescope の新しいメジャー バージョンにアップグレードする場合は、[the upgrade guide](https://github.com/laravel/telescope/blob/master/UPGRADE.md) を注意深く確認することが重要です。


さらに、新しい Telescope バージョンにアップグレードする場合は、Telescope のアセットを再公開する必要があります。

```shell
php artisan telescope:publish
```


アセットを最新の状態に保ち、今後の更新での問題を回避するには、アプリケーションの `composer.json` ファイル内の `post-update-cmd` スクリプトに `vendor:publish --tag=laravel-assets` コマンドを追加します。

```json
{
    "scripts": {
        "post-update-cmd": [
            "@php artisan vendor:publish --tag=laravel-assets --ansi --force"
        ]
    }
}
```

<a name="filtering"></a>

## Filtering

<a name="filtering-entries"></a>

### Entries


Telescope によって記録されたデータは、`App\Providers\TelescopeServiceProvider` クラスで定義されている `filter` クロージャを介してフィルタリングできます。デフォルトでは、このクロージャは、`local` 環境内のすべてのデータと、他のすべての環境内の例外、失敗したジョブ、スケジュールされたタスク、および監視対象のタグを持つデータを記録します。

```
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\Telescope;

/**
 * Register any application services.
 *
 * @return void
 */
public function register()
{
    $this->hideSensitiveRequestDetails();

    Telescope::filter(function (IncomingEntry $entry) {
        if ($this->app->environment('local')) {
            return true;
        }

        return $entry->isReportableException() ||
            $entry->isFailedJob() ||
            $entry->isScheduledTask() ||
            $entry->isSlowQuery() ||
            $entry->hasMonitoredTag();
    });
}
```

<a name="filtering-batches"></a>

### Batches


`filter` クロージャは個々のエントリのデータをフィルタリングしますが、`filterBatch` メソッドを使用して、特定のリクエストまたはコンソール コマンドのすべてのデータをフィルタリングするクロージャを登録できます。クロージャが `true` を返す場合、すべてのエントリが Telescope によって記録されます。

```
use Illuminate\Support\Collection;
use Laravel\Telescope\Telescope;

/**
 * Register any application services.
 *
 * @return void
 */
public function register()
{
    $this->hideSensitiveRequestDetails();

    Telescope::filterBatch(function (Collection $entries) {
        if ($this->app->environment('local')) {
            return true;
        }

        return $entries->contains(function ($entry) {
            return $entry->isReportableException() ||
                $entry->isFailedJob() ||
                $entry->isScheduledTask() ||
                $entry->isSlowQuery() ||
                $entry->hasMonitoredTag();
            });
    });
}
```

<a name="tagging"></a>

## Tagging


Telescope では、「タグ」によるエントリの検索が可能です。多くの場合、タグは Eloquent モデルのクラス名または認証されたユーザー ID であり、Telescope が自動的にエントリに追加します。場合によっては、エントリに独自のカスタム タグを添付したい場合があります。これを実現するには、`Telescope::tag` メソッドを使用できます。 `tag` メソッドは、タグの配列を返すクロージャを受け入れます。クロージャによって返されたタグは、Telescope が自動的にエントリに付加す​​るタグとマージされます。通常、`App\Providers\TelescopeServiceProvider` クラスの `register` メソッド内で `tag` メソッドを呼び出す必要があります。

```
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\Telescope;

/**
 * Register any application services.
 *
 * @return void
 */
public function register()
{
    $this->hideSensitiveRequestDetails();

    Telescope::tag(function (IncomingEntry $entry) {
        return $entry->type === 'request'
                    ? ['status:'.$entry->content['response_status']]
                    : [];
    });
 }
```

<a name="available-watchers"></a>

## Available Watchers


Telescopeの「ウォッチャー」は、リクエストまたはコンソール コマンドが実行されるときにアプリケーション データを収集します。 `config/telescope.php` 構成ファイル内で有効にするウォッチャーのリストをカスタマイズできます。

```
'watchers' => [
    Watchers\CacheWatcher::class => true,
    Watchers\CommandWatcher::class => true,
    ...
],
```


一部のウォッチャーでは、追加のカスタマイズ オプションを提供することもできます。

```
'watchers' => [
    Watchers\QueryWatcher::class => [
        'enabled' => env('TELESCOPE_QUERY_WATCHER', true),
        'slow' => 100,
    ],
    ...
],
```

<a name="batch-watcher"></a>

### Batch Watcher


バッチ ウォッチャーは、ジョブや接続情報など、キューに入れられた [batches](/ja/php/docs/9.x/queues/#job-batching) に関する情報を記録します。

<a name="cache-watcher"></a>

### Cache Watcher


キャッシュ ウォッチャーは、キャッシュ キーがヒットしたとき、ミスしたとき、更新されたとき、忘れられたときにデータを記録します。

<a name="command-watcher"></a>

### Command Watcher


コマンド ウォッチャーは、Artisan コマンドが実行されるたびに、引数、オプション、終了コード、および出力を記録します。ウォッチャーによる記録から特定のコマンドを除外したい場合は、`config/telescope.php` ファイル内の `ignore` オプションでコマンドを指定できます。

```
'watchers' => [
    Watchers\CommandWatcher::class => [
        'enabled' => env('TELESCOPE_COMMAND_WATCHER', true),
        'ignore' => ['key:generate'],
    ],
    ...
],
```

<a name="dump-watcher"></a>

### Dump Watcher


ダンプ ウォッチャーは、変数ダンプを記録し、Telescope に表示します。 Laravel を使用する場合、グローバル `dump` 関数を使用して変数をダンプすることができます。ダンプを記録するには、ブラウザでダンプ ウォッチャー タブが開かれている必要があります。そうしないと、ダンプはウォッチャーによって無視されます。

<a name="event-watcher"></a>

### Event Watcher


イベント ウォッチャーは、アプリケーションによってディスパッチされた [events](/ja/php/docs/9.x/events/) のペイロード、リスナ、およびブロードキャスト データを記録します。 Laravel フレームワークの内部イベントは、イベント ウォッチャーによって無視されます。

<a name="exception-watcher"></a>

### Exception Watcher


例外ウォッチャーは、アプリケーションによってスローされた報告可能な例外のデータとスタック トレースを記録します。

<a name="gate-watcher"></a>

### Gate Watcher


ゲート ウォッチャーは、アプリケーションによる [gate and policy](/ja/php/docs/9.x/authorization/) チェックのデータと結果を記録します。ウォッチャーによる記録から特定の能力を除外したい場合は、`config/telescope.php` ファイルの `ignore_abilities` オプションでそれらを指定できます。

```
'watchers' => [
    Watchers\GateWatcher::class => [
        'enabled' => env('TELESCOPE_GATE_WATCHER', true),
        'ignore_abilities' => ['viewNova'],
    ],
    ...
],
```

<a name="http-client-watcher"></a>

### HTTP Client Watcher


HTTP クライアント ウォッチャーは、アプリケーションによって作成された送信 [HTTP client requests](/ja/php/docs/9.x/http-client/) を記録します。

<a name="job-watcher"></a>

### Job Watcher


ジョブ ウォッチャーは、アプリケーションによってディスパッチされた [jobs](/ja/php/docs/9.x/queues/) のデータとステータスを記録します。

<a name="log-watcher"></a>

### Log Watcher


ログ ウォッチャーは、アプリケーションによって書き込まれたログの [log data](/ja/php/docs/9.x/logging/) を記録します。

<a name="mail-watcher"></a>

### Mail Watcher


メール ウォッチャーを使用すると、アプリケーションによって送信された [emails](/ja/php/docs/9.x/mail/) とその関連データのブラウザー内プレビューを表示できます。電子メールを `.eml` ファイルとしてダウンロードすることもできます。

<a name="model-watcher"></a>

### Model Watcher


モデル ウォッチャーは、Eloquent [model event](/ja/php/docs/9.x/eloquent/#events) がディスパッチされるたびに、モデルの変更を記録します。ウォッチャーの `events` オプションを使用して、どのモデル イベントを記録するかを指定できます。

```
'watchers' => [
    Watchers\ModelWatcher::class => [
        'enabled' => env('TELESCOPE_MODEL_WATCHER', true),
        'events' => ['eloquent.created*', 'eloquent.updated*'],
    ],
    ...
],
```


特定のリクエスト中にハイドレートされたモデルの数を記録したい場合は、`hydrations` オプションを有効にします。

```
'watchers' => [
    Watchers\ModelWatcher::class => [
        'enabled' => env('TELESCOPE_MODEL_WATCHER', true),
        'events' => ['eloquent.created*', 'eloquent.updated*'],
        'hydrations' => true,
    ],
    ...
],
```

<a name="notification-watcher"></a>

### Notification Watcher


通知ウォッチャーは、アプリケーションによって送信されたすべての [notifications](/ja/php/docs/9.x/notifications/) を記録します。通知によって電子メールが送信され、メール ウォッチャーが有効になっている場合、その電子メールはメール ウォッチャー画面でプレビューすることもできます。

<a name="query-watcher"></a>

### Query Watcher


クエリ ウォッチャーは、アプリケーションによって実行されるすべてのクエリの生の SQL、バインディング、および実行時間を記録します。また、ウォッチャーは、100 ミリ秒より遅いクエリに `slow` としてタグ付けします。ウォッチャーの `slow` オプションを使用して、低速クエリのしきい値をカスタマイズできます。

```
'watchers' => [
    Watchers\QueryWatcher::class => [
        'enabled' => env('TELESCOPE_QUERY_WATCHER', true),
        'slow' => 50,
    ],
    ...
],
```

<a name="redis-watcher"></a>

### Redis Watcher


Redis ウォッチャーは、アプリケーションによって実行されたすべての [Redis](/ja/php/docs/9.x/redis/) コマンドを記録します。キャッシュに Redis を使用している場合、キャッシュ コマンドも Redis ウォッチャーによって記録されます。

<a name="request-watcher"></a>

### Request Watcher


リクエスト ウォッチャーは、アプリケーションによって処理されるリクエストに関連付けられたリクエスト、ヘッダー、セッション、および応答データを記録します。 `size_limit` (キロバイト単位) オプションを使用して、記録された応答データを制限できます。

```
'watchers' => [
    Watchers\RequestWatcher::class => [
        'enabled' => env('TELESCOPE_REQUEST_WATCHER', true),
        'size_limit' => env('TELESCOPE_RESPONSE_SIZE_LIMIT', 64),
    ],
    ...
],
```

<a name="schedule-watcher"></a>

### Schedule Watcher


スケジュール ウォッチャーは、アプリケーションによって実行される [scheduled tasks](/ja/php/docs/9.x/scheduling/) のコマンドと出力を記録します。

<a name="view-watcher"></a>

### View Watcher


ビュー ウォッチャーは、ビューのレンダリング時に使用される [view](/ja/php/docs/9.x/views/) 名、パス、データ、および「コンポーザー」を記録します。

<a name="displaying-user-avatars"></a>

## Displaying User Avatars


Telescope ダッシュボードには、特定のエントリが保存されたときに認証されたユーザーのユーザー アバターが表示されます。デフォルトでは、Telescope は Gravatar Web サービスを使用してアバターを取得します。ただし、`App\Providers\TelescopeServiceProvider` クラスにコールバックを登録することで、アバター URL をカスタマイズできます。コールバックはユーザーの ID と電子メール アドレスを受け取り、ユーザーのアバター画像 URL を返す必要があります。

```
use App\Models\User;
use Laravel\Telescope\Telescope;

/**
 * Register any application services.
 *
 * @return void
 */
public function register()
{
    // ...

    Telescope::avatar(function ($id, $email) {
        return '/avatars/'.User::find($id)->avatar_path;
    });
}
```