跳至内容

Laravel Telescope

介绍

Laravel Telescope是您本地 Laravel 开发环境的绝佳伴侣。Telescope 可以洞察进入您应用程序的请求、异常、日志条目、数据库查询、队列作业、邮件、通知、缓存操作、计划任务、变量转储等。

安装

您可以使用 Composer 包管理器将 Telescope 安装到您的 Laravel 项目中:

1composer require laravel/telescope

安装 Telescope 后,使用 Artisan 命令发布其资源和迁移telescope:install。安装 Telescope 后,还应运行以下migrate命令来创建存储 Telescope 数据所需的表:

1php artisan telescope:install
2 
3php artisan migrate

最后,您可以通过路线访问 Telescope 仪表板/telescope

仅限本地安装

如果您只打算使用 Telescope 来协助您的本地开发,您可以使用以下--dev标志安装 Telescope:

1composer require laravel/telescope --dev
2 
3php artisan telescope:install
4 
5php artisan migrate

运行 后,您应该从应用程序的配置文件中telescope:install删除服务提供商注册。而是在类的方法中手动注册 Telescope 的服务提供商。在注册服务提供商之前,我们需要确保当前环境是:TelescopeServiceProviderbootstrap/providers.phpregisterApp\Providers\AppServiceProviderlocal

1/**
2 * Register any application services.
3 */
4public function register(): void
5{
6 if ($this->app->environment('local') && class_exists(\Laravel\Telescope\TelescopeServiceProvider::class)) {
7 $this->app->register(\Laravel\Telescope\TelescopeServiceProvider::class);
8 $this->app->register(TelescopeServiceProvider::class);
9 }
10}

最后,您还应该通过在文件中添加以下内容来防止 Telescope 包被自动发现composer.json

1"extra": {
2 "laravel": {
3 "dont-discover": [
4 "laravel/telescope"
5 ]
6 }
7},

配置

发布 Telescope 的资源后,其主配置文件将位于config/telescope.php。此配置文件允许您配置观察者选项。每个配置选项都包含其用途的描述,因此请务必仔细研究此文件。

如果需要,您可以使用配置选项完全禁用 Telescope 的数据收集enabled

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

数据修剪

如果不进行修剪,telescope_entries表会很快积累记录。为了缓解这种情况,您应该将Artisan命令安排telescope:prune为每天运行:

1use Illuminate\Support\Facades\Schedule;
2 
3Schedule::command('telescope:prune')->daily();

默认情况下,所有超过 24 小时的条目都将被删除。您可以hours在调用命令时使用该选项来确定保留 Telescope 数据的时间。例如,以下命令将删除所有超过 48 小时前创建的记录:

1use Illuminate\Support\Facades\Schedule;
2 
3Schedule::command('telescope:prune --hours=48')->daily();

仪表板授权

可以通过路由访问 Telescope 仪表板/telescope。默认情况下,您只能在local环境中访问此仪表板。在您的app/Providers/TelescopeServiceProvider.php文件中,有一个授权门定义。此授权门控制在非本地环境中对 Telescope 的访问。您可以根据需要随意修改此门,以限制对 Telescope 安装的访问:

1use App\Models\User;
2 
3/**
4 * Register the Telescope gate.
5 *
6 * This gate determines who can access Telescope in non-local environments.
7 */
8protected function gate(): void
9{
10 Gate::define('viewTelescope', function (User $user) {
11 return in_array($user->email, [
12 'taylor@laravel.com',
13 ]);
14 });
15}

你应该确保在生产环境中将APP_ENV环境变量更改为production。否则,你的 Telescope 安装将会被公开。

升级Telescope

当升级到 Telescope 的新主要版本时,仔细阅读升级指南非常重要

此外,升级到任何新的 Telescope 版本时,您应该重新发布 Telescope 的资产:

1php artisan telescope:publish

为了使资产保持最新并避免将来更新时出现问题,您可以将vendor:publish --tag=laravel-assets命令添加到post-update-cmd应用程序文件中的composer.json脚本中:

1{
2 "scripts": {
3 "post-update-cmd": [
4 "@php artisan vendor:publish --tag=laravel-assets --ansi --force"
5 ]
6 }
7}

过滤

条目

filter你可以通过类中定义的闭包来过滤 Telescope 记录的数据App\Providers\TelescopeServiceProvider。默认情况下,此闭包会记录local环境中的所有数据,以及所有其他环境中的异常、失败的作业、计划任务和带有监控标签的数据:

1use Laravel\Telescope\IncomingEntry;
2use Laravel\Telescope\Telescope;
3 
4/**
5 * Register any application services.
6 */
7public function register(): void
8{
9 $this->hideSensitiveRequestDetails();
10 
11 Telescope::filter(function (IncomingEntry $entry) {
12 if ($this->app->environment('local')) {
13 return true;
14 }
15 
16 return $entry->isReportableException() ||
17 $entry->isFailedJob() ||
18 $entry->isScheduledTask() ||
19 $entry->isSlowQuery() ||
20 $entry->hasMonitoredTag();
21 });
22}

批次

虽然filter闭包会过滤单个条目的数据,但你可以使用filterBatch方法来注册一个闭包,用于过滤给定请求或控制台命令的所有数据。如果闭包返回true,则所有条目都会被 Telescope 记录下来:

1use Illuminate\Support\Collection;
2use Laravel\Telescope\IncomingEntry;
3use Laravel\Telescope\Telescope;
4 
5/**
6 * Register any application services.
7 */
8public function register(): void
9{
10 $this->hideSensitiveRequestDetails();
11 
12 Telescope::filterBatch(function (Collection $entries) {
13 if ($this->app->environment('local')) {
14 return true;
15 }
16 
17 return $entries->contains(function (IncomingEntry $entry) {
18 return $entry->isReportableException() ||
19 $entry->isFailedJob() ||
20 $entry->isScheduledTask() ||
21 $entry->isSlowQuery() ||
22 $entry->hasMonitoredTag();
23 });
24 });
25}

标记

Telescope 允许您通过“标签”搜索条目。标签通常是 Eloquent 模型类名或 Telescope 自动添加到条目的已验证用户 ID。有时,您可能希望将自定义标签附加到条目。为此,您可以使用Telescope::tag方法。该tag方法接受一个闭包,该闭包返回一个标签数组。闭包返回的标签将与 Telescope 自动附加到条目的任何标签合并。通常,您应该在类的方法tag中调用该方法registerApp\Providers\TelescopeServiceProvider

1use Laravel\Telescope\IncomingEntry;
2use Laravel\Telescope\Telescope;
3 
4/**
5 * Register any application services.
6 */
7public function register(): void
8{
9 $this->hideSensitiveRequestDetails();
10 
11 Telescope::tag(function (IncomingEntry $entry) {
12 return $entry->type === 'request'
13 ? ['status:'.$entry->content['response_status']]
14 : [];
15 });
16}

可用的观察者

Telescope 的“观察者”会在执行请求或控制台命令时收集应用程序数据。您可以在配置文件中自定义要启用的观察者列表config/telescope.php

1'watchers' => [
2 Watchers\CacheWatcher::class => true,
3 Watchers\CommandWatcher::class => true,
4 // ...
5],

一些观察者还允许您提供额外的自定义选项:

1'watchers' => [
2 Watchers\QueryWatcher::class => [
3 'enabled' => env('TELESCOPE_QUERY_WATCHER', true),
4 'slow' => 100,
5 ],
6 // ...
7],

批次观察器

批处理观察器记录有关排队批处理的信息,包括作业和连接信息。

缓存观察器

缓存观察器会在缓存键被命中、丢失、更新和遗忘时记录数据。

指挥观察员

命令监听器会记录 Artisan 命令执行时的参数、选项、退出代码和输出。如果你想让监听器不记录某些命令,可以在文件ignore中的选项中指定该命令config/telescope.php

1'watchers' => [
2 Watchers\CommandWatcher::class => [
3 'enabled' => env('TELESCOPE_COMMAND_WATCHER', true),
4 'ignore' => ['key:generate'],
5 ],
6 // ...
7],

转储观察员

转储监视程序会在 Telescope 中记录并显示变量转储。使用 Laravel 时,可以使用全局dump函数转储变量。必须在浏览器中打开转储监视程序选项卡才能记录转储,否则,转储将被监视程序忽略。

事件观察者

事件观察器会记录应用程序所调度事件的有效负载、监听器和广播数据。Laravel 框架的内部事件会被事件观察器忽略。

异常观察器

异常观察器记录应用程序抛出的任何可报告异常的数据和堆栈跟踪。

守门人

门控监控器会记录应用程序的门控和策略检查的数据和结果。如果您希望某些权限不被监控器记录,您可以在文件ignore_abilities中的选项中指定这些权限config/telescope.php

1'watchers' => [
2 Watchers\GateWatcher::class => [
3 'enabled' => env('TELESCOPE_GATE_WATCHER', true),
4 'ignore_abilities' => ['viewNova'],
5 ],
6 // ...
7],

HTTP 客户端观察器

HTTP 客户端观察器记录您的应用程序发出的HTTP 客户端请求

工作观察员

作业观察器记录您的应用程序调度的任何作业的数据和状态。

日志观察器

日志观察器记录应用程序写入的任何日志的日志数据。

默认情况下,Telescope 只会记录该error级别及以上的日志。不过,你可以修改level应用程序config/telescope.php配置文件中的选项来改变此行为:

1'watchers' => [
2 Watchers\LogWatcher::class => [
3 'enabled' => env('TELESCOPE_LOG_WATCHER', true),
4 'level' => 'debug',
5 ],
6 
7 // ...
8],

邮件监视程序

邮件监控器允许您在浏览器中预览应用程序发送的电子邮件及其相关数据。您也可以将电子邮件下载为.eml文件。

模型观察者

每当 Eloquent模型事件被调度时,模型观察器都会记录模型的变化。您可以通过观察器的events选项指定要记录哪些模型事件:

1'watchers' => [
2 Watchers\ModelWatcher::class => [
3 'enabled' => env('TELESCOPE_MODEL_WATCHER', true),
4 'events' => ['eloquent.created*', 'eloquent.updated*'],
5 ],
6 // ...
7],

如果您想记录给定请求期间水合的模型数量,请启用以下hydrations选项:

1'watchers' => [
2 Watchers\ModelWatcher::class => [
3 'enabled' => env('TELESCOPE_MODEL_WATCHER', true),
4 'events' => ['eloquent.created*', 'eloquent.updated*'],
5 'hydrations' => true,
6 ],
7 // ...
8],

通知观察者

通知监控器会记录您的应用发送的所有通知。如果通知触发了电子邮件,并且您启用了邮件监控器,则该电子邮件也将在邮件监控器屏幕上可供预览。

查询观察器

查询监控器会记录应用程序执行的所有查询的原始 SQL、绑定和执行时间。监控器还会将任何慢于 100 毫秒的查询标记为slow。您可以使用监控器的slow选项自定义慢查询阈值:

1'watchers' => [
2 Watchers\QueryWatcher::class => [
3 'enabled' => env('TELESCOPE_QUERY_WATCHER', true),
4 'slow' => 50,
5 ],
6 // ...
7],

Redis 观察者

Redis 观察器会记录您的应用程序执行的所有Redis命令。如果您使用 Redis 进行缓存,则 Redis 观察器还会记录缓存命令。

请求观察者

请求监控器会记录应用程序处理的任何请求、标头、会话和响应数据。您可以通过以下size_limit选项(以千字节为单位)限制记录的响应数据:

1'watchers' => [
2 Watchers\RequestWatcher::class => [
3 'enabled' => env('TELESCOPE_REQUEST_WATCHER', true),
4 'size_limit' => env('TELESCOPE_RESPONSE_SIZE_LIMIT', 64),
5 ],
6 // ...
7],

日程观察员

计划观察器记录应用程序运行的任何计划任务的命令和输出。

查看观察者

视图观察器记录视图名称、路径、数据以及渲染视图时使用的“编写器”。

显示用户头像

Telescope 仪表板会显示已通过身份验证的用户在保存指定条目时的用户头像。默认情况下,Telescope 将使用 Gravatar Web 服务获取头像。但是,您可以通过在类中注册回调来自定义头像 URL App\Providers\TelescopeServiceProvider。回调将接收用户的 ID 和电子邮件地址,并返回用户头像图片的 URL:

1use App\Models\User;
2use Laravel\Telescope\Telescope;
3 
4/**
5 * Register any application services.
6 */
7public function register(): void
8{
9 // ...
10 
11 Telescope::avatar(function (?string $id, ?string $email) {
12 return ! is_null($id)
13 ? '/avatars/'.User::find($id)->avatar_path
14 : '/generic-avatar.jpg';
15 });
16}