介绍

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

安装

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

  1. composer require laravel/telescope

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

  1. php artisan telescope:install
  2. php artisan migrate

最后,你可以通过 php /telescope 路由访问 Telescope 仪表板。

仅本地安装

如果你计划仅在本地开发中使用 Telescope,可以使用 php --dev 标志安装 Telescope:

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

运行完 php telescope:install 后,应该从你的应用程序的 php bootstrap/providers.php 配置文件中移除 php TelescopeServiceProvider 服务提供程序的注册。相反,在 php App\Providers\AppServiceProvider 类的 php register 方法中手动注册 Telescope 的服务提供程序。我们将确保当前环境为 php local,然后再注册这些提供程序:

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

最后,你还应该防止 Telescope 包被 自动发现 ,方法是在你的 php composer.json 文件中添加以下内容:

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

配置

在发布 Telescope 的资产后,其主要配置文件将位于 php config/telescope.php。这个配置文件允许你配置你的观察者选项。每个配置选项都包括其用途的描述,所以一定要彻底探索这个文件。

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

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

数据修剪

如果不进行修剪,php telescope_entries 表会很快积累记录。为了缓解这个问题,你应该 调度 php telescope:prune Artisan 命令以每天运行:

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

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

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

仪表板授权

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

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


[!WARNING]
你应确保在生产环境中将你的 php APP_ENV 环境变量更改为 php production。否则,你的 Telescope 安装将公开可访问。

升级 Telescope

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

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

  1. php artisan telescope:publish

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

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

过滤

条目

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

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

批处理

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

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

标记

Telescope 允许你通过「标记」搜索条目。通常,标记是 Eloquent 模型类名或经过身份验证的用户 ID,Telescope 会自动将其添加到条目中。偶尔,你可能希望将自定义标记附加到条目。为此,你可以使用 php Telescope::tag 方法。php tag 方法接受一个闭包,该闭包应返回一个标记数组。闭包返回的标记将与 Telescope 自动附加到条目的任何标记合并。通常情况下,你应该在 php App\Providers\TelescopeServiceProvider 类的 php register 方法中调用 php tag 方法:

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

可用观察者

Telescope 的「观察者」在执行请求或控制台命令时收集应用程序数据。你可以在 php 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 命令时记录参数、选项、退出代码和输出。如果你希望排除某些命令被观察者记录,你可以在 php config/telescope.php 文件中的 php ignore 选项中指定这些命令:

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

Dump 观察者

Dump 观察者记录并在 Telescope 中显示你的变量转储。在使用 Laravel 时,可以使用全局的 php dump 函数转储变量。要记录转储,浏览器中必须打开 Dump 观察者选项卡,否则观察者将忽略转储。

事件观察者

事件观察者记录应用程序分派的任何事件的负载、侦听器和广播数据。 Laravel 框架内部事件将被事件观察者忽略。

异常观察者

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

Gate 观察者

Gate 观察者记录应用程序进行的门禁和策略检查的数据和结果。如果你希望排除观察者记录的某些权限,则可以在 php config/telescope.php 文件中的 php ignore_abilities 选项中指定这些权限:

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

HTTP 客户端观察者

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

作业观察者

作业观察者记录应用程序分派的任何作业的数据和状态。

日志观察者

日志观察者记录应用程序写入的日志数据。默认情况下,Telescope 仅记录 php error 级别及以上的日志。但是,你可以修改应用程序的 php config/telescope.php 配置文件中的 php level 选项来修改此行为:

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

邮件观察者

邮件观察者允许你在浏览器中预览应用程序发送的邮件,以及它们的相关数据。你还可以将邮件下载为 php .eml 文件。

模型观察者

模型观察者

模型观察者在调度 Eloquent 模型事件 时记录模型更改。你可以通过观察者的 php events 选项指定应记录哪些模型事件:

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

如果你想记录在给定请求期间加载的模型数量,请启用 php 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 毫秒的查询标记为 php slow。你可以使用观察者的 php 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 观察者记录。

请求观察者

请求观察者记录应用程序处理的所有请求的请求、标头、会话和响应数据。你可以通过 php 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 网络服务来获取头像。但是,你可以通过在 php App\Providers\TelescopeServiceProvider 类中注册回调来自定义头像 URL。回调函数将接收用户的 ID 和电子邮件地址,并应返回用户的头像图片 URL:

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