Telescope 應(yīng)用調(diào)試工具
Telescope 應(yīng)用調(diào)試工具
Laravel Telescope
簡(jiǎn)介
Larave Telescope 是 Laravel 框架的優(yōu)雅調(diào)試助手。Telescope 可深入了解進(jìn)入應(yīng)用程序的請(qǐng)求、異常、日志條目、數(shù)據(jù)庫(kù)查詢、排隊(duì)作業(yè)、郵件、通知、緩存操作、計(jì)劃任務(wù)、變量轉(zhuǎn)儲(chǔ)等。Telescope 是您本地 Laravel 開發(fā)環(huán)境的絕佳伴侶。
安裝
你可以使用 Composer 在 Laravel 項(xiàng)目中安裝 Telescope 擴(kuò)展:
composer require laravel/telescope
安裝 Telescope 后,可以在 Artisan 使用 telescope:install
命令來配置擴(kuò)展實(shí)例。安裝 Telescope 后,還應(yīng)運(yùn)行 migrate
命令:
php artisan telescope:install php artisan migrate
更新 Telescope
更新 Telescope 時(shí),您應(yīng)該重新配置加載 Telescope 實(shí)例:
php artisan telescope:publish
僅在特定環(huán)境中安裝
如果您打算僅使用 Telescope 來協(xié)助您的本地開發(fā)。可以使用 --dev
標(biāo)志安裝 Telescope:
composer require laravel/telescope --dev
運(yùn)行 telescope:install
后,您應(yīng)該從 app
配置文件中刪除 TelescopeServiceProvider
服務(wù)提供注冊(cè)。相反,在 AppServiceProvider
的 register
方法中手動(dòng)注冊(cè)服務(wù):
use Laravel\Telescope\TelescopeServiceProvider; /** * 注冊(cè)應(yīng)用服務(wù)。 * * @return void */public function register(){ if ($this->app->isLocal()) { $this->app->register(TelescopeServiceProvider::class); } }
定制數(shù)據(jù)遷移
如果您不打算使用 Telescope 的默認(rèn)遷移,則應(yīng)該在 AppServiceProvider
的 register
方法中調(diào)用 Telescope::ignoreMigrations
方法。您可以使用 php artisan vendor:publish --tag=telescope-migrations
命令導(dǎo)出默認(rèn)遷移。
配置
使用 Telescope,其主要配置文件將位于 config/telescope.php
。此配置文件允許您配置監(jiān)聽程序選項(xiàng),每個(gè)配置選項(xiàng)都包含其用途說明,因此請(qǐng)務(wù)必徹底瀏覽此文件。
如果需要,您可以使用 enabled
配置選項(xiàng)完全禁用 Telescope 的數(shù)據(jù)收集:
'enabled' => env('TELESCOPE_ENABLED', true),
數(shù)據(jù)修改
有了數(shù)據(jù)修改,telescope_entries
表可以非??焖俚乩鄯e記錄。為了緩解這個(gè)問題,你應(yīng)該使用 Artisan 每天運(yùn)行 telescope:prune
命令:
$schedule->command('telescope:prune')->daily();
默認(rèn)情況下,將獲取超過 24 小時(shí)的所有數(shù)據(jù)。在調(diào)用命令時(shí)可以使用 hours
選項(xiàng)來確定保留 Telescope 數(shù)據(jù)的時(shí)間。例如,以下命令將刪除 48 小時(shí)前創(chuàng)建的所有記錄:
$schedule->command('telescope:prune --hours=48')->daily();
儀表板授權(quán)
Telescope 在 /telescope
處顯示儀表板。默認(rèn)情況下,您只能在 本地
環(huán)境中訪問此儀表板。在你的 app/Providers/TelescopeServiceProvider.php
文件中,有一個(gè) gate
方法。此授權(quán)能控制在 非本地 環(huán)境中對(duì) Telescope 的訪問。您可以根據(jù)需要隨意修改此權(quán)限限制以對(duì) Telescope 安裝和訪問:
/** * 註冊(cè) Telescope Gate。 * * 使用 Gate 決定誰(shuí)可以在非本地環(huán)境中訪問 Telescope。 * * @return void */ protected function gate(){ Gate::define('viewTelescope', function ($user) { return in_array($user->email, [ 'taylor@laravel.com', ]); }); }
過濾
單項(xiàng)過濾
您可以通過在 TelescopeServiceProvider
中注冊(cè)的 filter
回調(diào)來過濾 Telescope 記錄的數(shù)據(jù)。默認(rèn)情況下,此回調(diào)會(huì)記錄 本地
環(huán)境中的所有數(shù)據(jù)以及所有其他環(huán)境中的異常、進(jìn)程中斷、計(jì)劃任務(wù)和帶有受監(jiān)控標(biāo)記的數(shù)據(jù):
/** * 注冊(cè)應(yīng)用服務(wù)。 * * @return void */ public function register(){ $this->hideSensitiveRequestDetails(); Telescope::filter(function (IncomingEntry $entry) { if ($this->app->isLocal()) { return true; } return $entry->isReportableException() || $entry->isFailedJob() || $entry->isScheduledTask() || $entry->hasMonitoredTag(); }); }
批量過濾
雖然 filter
回調(diào)過濾單個(gè)條目的數(shù)據(jù),但您可以使用 filterBatch
方法注冊(cè)一個(gè)回調(diào),該回調(diào)過濾給定請(qǐng)求或控制臺(tái)命令的所有數(shù)據(jù)。如果回調(diào)返回 true
,則所有數(shù)據(jù)都由 Telescope 記錄:
use Illuminate\Support\Collection; /** * 注冊(cè)應(yīng)用服務(wù)。 * * @return void */ public function register(){ $this->hideSensitiveRequestDetails(); Telescope::filterBatch(function (Collection $entries) { if ($this->app->isLocal()) { return true; } return $entries->contains(function ($entry) { return $entry->isReportableException() || $entry->isFailedJob() || $entry->isScheduledTask() || $entry->hasMonitoredTag(); }); }); }
可用的監(jiān)聽
當(dāng)在控制臺(tái)執(zhí)行命令或處理請(qǐng)求時(shí),Telescope 監(jiān)聽器會(huì)收集應(yīng)用程序數(shù)據(jù)。您可以在 config/telescope.php
配置文件中自定義要啟用監(jiān)聽項(xiàng)的列表:
'watchers' => [ Watchers\CacheWatcher::class => true, Watchers\CommandWatcher::class => true, ... ],
一些監(jiān)聽器還允許您提供其他自定義選項(xiàng):
'watchers' => [ Watchers\QueryWatcher::class => [ 'enabled' => env('TELESCOPE_QUERY_WATCHER', true), 'slow' => 100, ], ... ],
緩存監(jiān)聽
當(dāng)緩存鍵被命中、遺漏、更新和遺忘時(shí),緩存監(jiān)聽器會(huì)記錄數(shù)據(jù)。
命令監(jiān)聽
只要執(zhí)行 Artisan 命令,命令監(jiān)聽器就會(huì)記錄參數(shù)、選項(xiàng)、退出代碼和輸出。如果您想排除監(jiān)聽器記錄的某些命令,您可以在 config/telescope.php
文件的 ignore
選項(xiàng)中指定命令:
'watchers' => [ Watchers\CommandWatcher::class => [ 'enabled' => env('TELESCOPE_COMMAND_WATCHER', true), 'ignore' => ['key:generate'], ], ... ],
數(shù)據(jù)監(jiān)聽
數(shù)據(jù)監(jiān)聽器在 Telescope 中記錄并顯示您的數(shù)據(jù)變量。使用 Laravel 時(shí),可以使用全局 dump
函數(shù)輸出變量。必須在瀏覽器中打開數(shù)據(jù)監(jiān)聽器選項(xiàng)卡,才能進(jìn)行輸出變量,否則監(jiān)聽器將忽略此次輸出。
事件監(jiān)聽
事件監(jiān)聽器記錄應(yīng)用程序調(diào)度的任何事件的有效負(fù)載、監(jiān)聽器和廣播數(shù)據(jù)。事件監(jiān)聽器忽略了 Laravel 框架的內(nèi)部事件。
異常監(jiān)聽
異常監(jiān)聽器記錄應(yīng)用程序引發(fā)的任何可報(bào)告異常的數(shù)據(jù)和堆棧跟蹤。
Gate 監(jiān)聽
Gate 監(jiān)聽器記錄您的應(yīng)用程序的 Gate 和策略檢查的數(shù)據(jù)和結(jié)果。如果您希望將某些能力排除在監(jiān)聽器的記錄之外,您可以在 config/telescope.php
文件的 ignore_abilities
選項(xiàng)中指定它們:
'watchers' => [ Watchers\GateWatcher::class => [ 'enabled' => env('TELESCOPE_GATE_WATCHER', true), 'ignore_abilities' => ['viewNova'], ], ... ],
進(jìn)程監(jiān)聽
進(jìn)程監(jiān)聽器記錄應(yīng)用程序分派的任何作業(yè)的數(shù)據(jù)和狀態(tài)。
日志監(jiān)聽
日志監(jiān)視器記錄應(yīng)用程序?qū)懭氲娜魏稳罩镜娜罩緮?shù)據(jù)。
郵件監(jiān)聽
郵件監(jiān)視器允許您查看電子郵件的瀏覽器內(nèi)預(yù)覽及其相關(guān)數(shù)據(jù)。您也可以將該電子郵件下載為 .eml
文件。
模型監(jiān)聽
只要調(diào)度了模型的 create
、updated
、restored
或 deleted
事件,模型觀察器就會(huì)記錄模型更改。您可以通過監(jiān)聽器的 events
選項(xiàng)指定應(yīng)記錄哪些模型事件:
'watchers' => [ Watchers\ModelWatcher::class => [ 'enabled' => env('TELESCOPE_MODEL_WATCHER', true), 'events' => ['eloquent.created*', 'eloquent.updated*'], ], ... ],
消息通知監(jiān)聽
消息通知監(jiān)聽器記錄您的應(yīng)用程序發(fā)送的所有通知。如果通知觸發(fā)了電子郵件并且您啟用了郵件監(jiān)聽器,則電子郵件也可以在郵件監(jiān)視器屏幕上進(jìn)行預(yù)覽。
數(shù)據(jù)查詢監(jiān)聽
數(shù)據(jù)查詢監(jiān)聽器記錄應(yīng)用程序執(zhí)行的所有查詢的原始 SQL、綁定和執(zhí)行時(shí)間。觀察者還將任何慢于 100 毫秒的查詢標(biāo)記為 slow
。您可以使用觀察者的 slow
選項(xiàng)自定義慢查詢閾值:
'watchers' => [ Watchers\QueryWatcher::class => [ 'enabled' => env('TELESCOPE_QUERY_WATCHER', true), 'slow' => 50, ], ... ],
Redis 監(jiān)聽
必須啟用 Redis 事件才能使 Redis 監(jiān)聽器正常運(yùn)行。您可以通過在
app/Providers/AppServiceProvider.php
文件的boot
方法中調(diào)用Redis::enableEvents()
來啟用 Redis 事件。
Redis 監(jiān)聽器記錄您的應(yīng)用程序執(zhí)行的所有 Redis 命令。如果您使用 Redis 進(jìn)行緩存,Redis 監(jiān)聽器也會(huì)記錄緩存命令。
請(qǐng)求監(jiān)聽
請(qǐng)求監(jiān)聽器記錄與應(yīng)用程序處理的任何請(qǐng)求相關(guān)聯(lián)的請(qǐng)求、標(biāo)頭、會(huì)話和響應(yīng)數(shù)據(jù)。您可以通過 size_limit
(以 KB 為單位)選項(xiàng)限制響應(yīng)數(shù)據(jù):
'watchers' => [ Watchers\RequestWatcher::class => [ 'enabled' => env('TELESCOPE_REQUEST_WATCHER', true), 'size_limit' => env('TELESCOPE_RESPONSE_SIZE_LIMIT', 64), ], ... ],
Schedule 監(jiān)聽
Schedule 監(jiān)聽器記錄應(yīng)用程序運(yùn)行的任何計(jì)劃任務(wù)的命令和輸出。