Laravel Telescope
導入
Laravel Telescope は、ローカルの Laravel 開発環境に最適な補助ツールです。Telescope は、アプリケーションに寄せられるリクエスト、例外、ログエントリ、データベースクエリ、キューイングされたジョブ、メール、通知、キャッシュ操作、スケジュールされたタスク、変数のダンプなどに対する洞察を提供します。

インストール
Composer パッケージマネージャを使用して、Telescope を Laravel プロジェクトにインストールできます:
composer require laravel/telescope
Telescope をインストールした後は、telescope:install
Artisan コマンドを使用して、そのアセットとマイグレーションを公開してください。Telescope をインストールした後は、Telescope のデータを保存するために必要なテーブルを作成するために migrate
コマンドも実行する必要があります。
php artisan telescope:install
php artisan migrate
最終的には、/telescope
ルート経由で Telescope ダッシュボードにアクセスできます。
ローカル専用インストール
Telescope をローカル開発の補助としてのみ使用する場合は、--dev
フラグを使用して Telescope をインストールできます:
composer require laravel/telescope --dev
php artisan telescope:install
php artisan migrate
telescope:install
を実行した後、アプリケーションの bootstrap/providers.php
構成ファイルから TelescopeServiceProvider
サービスプロバイダーの登録を削除する必要があります。代わりに、Telescope のサービスプロバイダーを、App\Providers\AppServiceProvider
クラスの register
メソッドで手動で登録してください。プロバイダーを登録する前に、現在の環境が local
であることを確認します:
/**
* Register any application services.
*/
public function register(): void
{
if ($this->app->environment('local')) {
$this->app->register(\Laravel\Telescope\TelescopeServiceProvider::class);
$this->app->register(TelescopeServiceProvider::class);
}
}
最後に、Telescope パッケージが 自動検出 されないようにするために、composer.json
ファイルに以下を追加する必要があります:
"extra": {
"laravel": {
"dont-discover": [
"laravel/telescope"
]
}
},
設定
Telescope のアセットを公開した後、その主要な設定ファイルは config/telescope.php
にあります。この設定ファイルを使用して、ウォッチャーオプション を構成できます。各構成オプションにはその目的の説明が含まれているため、このファイルを十分に調査してください。
必要に応じて、enabled
設定オプションを使用して Telescope のデータ収集を完全に無効にすることができます:
'enabled' => env('TELESCOPE_ENABLED', true),
データの削除
剪定を行わないと、telescope_entries
テーブルには非常に速くレコードが蓄積されます。これを緩和するために、telescope:prune
Artisan コマンドを毎日実行するように スケジュール する必要があります:
use Illuminate\Support\Facades\Schedule;
Schedule::command('telescope:prune')->daily();
デフォルトでは、24 時間より古いすべてのエントリが剪定されます。Telescope データを保持する期間を決定するために、コマンドを呼び出す際に hours
オプションを使用できます。たとえば、以下のコマンドは 48 時間前に作成されたすべてのレコードを削除します:
use Illuminate\Support\Facades\Schedule;
Schedule::command('telescope:prune --hours=48')->daily();
ダッシュボードの認証
ダッシュボード には /telescope
ルート経由でアクセスできます。デフォルトでは、このダッシュボードには local
環境でのみアクセスできます。app/Providers/TelescopeServiceProvider.php
ファイル内には、認可ゲート の定義があります。この認可ゲートは、非ローカル環境での Telescope へのアクセスを制御します。必要に応じてこのゲートを変更して、Telescope のインストールへのアクセスを制限することができます:
use App\Models\User;
/**
* Register the Telescope gate.
*
* This gate determines who can access Telescope in non-local environments.
*/
protected function gate(): void
{
Gate::define('viewTelescope', function (User $user) {
return in_array($user->email, [
'taylor@laravel.com',
]);
});
}
本番環境では APP_ENV
環境変数を production
に変更することを確認してください。そうしないと、Telescope のインストールが一般公開される可能性があります。
Telescope のアップグレード
新しい Telescope のメジャーバージョンにアップグレードする際には、アップグレードガイド を注意深く確認することが重要です。
また、新しい Telescope バージョンにアップグレードする際には、Telescope のアセットを再公開する必要があります:
php artisan telescope:publish
アセットを最新の状態に保ち、将来のアップデートで問題が発生しないようにするために、アプリケーションの composer.json
ファイルの post-update-cmd
スクリプトに vendor:publish --tag=laravel-assets
コマンドを追加することができます:
{
"scripts": {
"post-update-cmd": [
"@php artisan vendor:publish --tag=laravel-assets --ansi --force"
]
}
}
フィルタリング
エントリ
App\Providers\TelescopeServiceProvider
クラスで定義された filter
クロージャを使用して、Telescope によって記録されるデータをフィルタリングすることができます。デフォルトでは、このクロージャは local
環境と例外、失敗したジョブ、スケジュールされたタスク、および他のすべての環境で監視されたタグを持つデータを記録します:
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\Telescope;
/**
* Register any application services.
*/
public function register(): void
{
$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();
});
}
バッチ
filter
クロージャは個々のエントリのデータをフィルタリングしますが、filterBatch
メソッドを使用して、特定のリクエストやコンソールコマンドのすべてのデータをフィルタリングするクロージャを登録することができます。クロージャが true
を返すと、Telescope によってすべてのエントリが記録されます。
use Illuminate\Support\Collection;
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\Telescope;
/**
* Register any application services.
*/
public function register(): void
{
$this->hideSensitiveRequestDetails();
Telescope::filterBatch(function (Collection $entries) {
if ($this->app->environment('local')) {
return true;
}
return $entries->contains(function (IncomingEntry $entry) {
return $entry->isReportableException() ||
$entry->isFailedJob() ||
$entry->isScheduledTask() ||
$entry->isSlowQuery() ||
$entry->hasMonitoredTag();
});
});
}
タギング
Telescopeを使用すると、「タグ」でエントリを検索できます。タグは、Eloquentモデルクラス名や認証済みユーザーIDなどがよく使用され、Telescopeがエントリに自動的に追加します。時折、独自のカスタムタグをエントリに添付したい場合があります。これを実現するには、Telescope::tag
メソッドを使用できます。tag
メソッドは、タグの配列を返すべきクロージャを受け入れます。クロージャによって返されたタグは、エントリに自動的に添付されるタグとマージされます。通常、App\Providers\TelescopeServiceProvider
クラスのregister
メソッド内でtag
メソッドを呼び出すべきです:
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\Telescope;
/**
* Register any application services.
*/
public function register(): void
{
$this->hideSensitiveRequestDetails();
Telescope::tag(function (IncomingEntry $entry) {
return $entry->type === 'request'
? ['status:'.$entry->content['response_status']]
: [];
});
}