Laravel Telescope 调试助手完整指南:安装、配置、Watcher 机制与运维实战

原创2026-10-14 12:10:541,701 阅读
文章标签:开发工具调试器可观测性后端日志分析

Laravel Telescope 调试助手完整指南:安装、配置、Watcher 机制与运维实战

导读:Laravel Telescope 是 Laravel 官方推出的优雅调试助手,能够洞察进入应用的请求、异常、日志、数据库查询、队列任务、邮件、通知、缓存操作、定时任务与变量 Dump 等几乎全部运行时行为。本文以当前仓库(laravel/telescope)源码为据,完整讲解它的安装发布、配置项语义、Watcher 机制、存储与清理运维,帮助读者在本地开发环境中快速上手并把 Telescope 用透。

一、Telescope 是什么:一次看清应用的“五脏六腑”

Laravel Telescope 是专为 Laravel 框架设计的调试伙伴。它不只是一块看板,而是一套完整的“应用内窥镜”:当请求进入应用、任务被执时,Telescope 通过一组名为 Watcher(观察器) 的组件采集应用运行数据,并以优雅的 Web 界面呈现。根据当前仓库 composer.json 的声明,它的官方定位正是 "An elegant debug assistant for the Laravel framework"。

它能记录的数据范围极其广泛,对应 src/EntryType.php 中定义的 18 种条目类型:

条目类型 常量 含义
batch EntryType::BATCH 队列批次(Queue Batch)
cache EntryType::CACHE 缓存读写操作
command EntryType::COMMAND Artisan 命令执行
dump EntryType::DUMP 变量 Dump(dump() / dd())
event EntryType::EVENT 事件分发
exception EntryType::EXCEPTION 异常记录
job EntryType::JOB 队列任务
log EntryType::LOG 日志条目
mail EntryType::MAIL 邮件发送
model EntryType::MODEL Eloquent 模型事件
notification EntryType::NOTIFICATION 通知发送
query EntryType::QUERY 数据库查询
redis EntryType::REDIS Redis 命令
request EntryType::REQUEST HTTP 请求
schedule EntryType::SCHEDULED_TASK 定时任务
gate EntryType::GATE 授权 Gate 检查
view EntryType::VIEW 视图渲染
client_request EntryType::CLIENT_REQUEST 应用发出的外部 HTTP 请求

技术前提(以当前仓库 composer.json 为准):Telescope 要求 PHP ^8.0,支持 Laravel Framework ^8.37 / ^9.0 / ^10.0 / ^11.0,依赖 symfony/console 与 symfony/var-dumper。也就是说它服务于 PHP 8 时代的现代 Laravel 项目。

典型应用场景:本地开发环境排障——接口返回异常时回看请求头、Payload 与响应;数据库慢查询定位;队列任务失败原因复盘;邮件/通知内容核对;甚至查看某次请求中所有 Gate 授权检查的通过情况。Telescope 是本地开发环境的最佳伴侣(README 原话为 "Telescope makes a wonderful companion to your local Laravel development environment"),而仓库默认配置也正是为本地开发量体裁衣:非 local 环境仅记录可上报异常、失败请求、失败任务、定时任务与受监控标签(详见下文"自定义过滤器")。

二、安装与发布:三步把 Telescope 装进项目

2.1 安装依赖

在 Laravel 项目根目录执行:

composer require laravel/telescope

2.2 安装脚手架

执行安装命令,它会自动完成四件事——发布服务提供者、前端资源、配置文件与数据库迁移,并把 TelescopeServiceProvider 注册进应用:

php artisan telescope:install

从 src/Console/InstallCommand.php 的实现可以看出,telescope:install 实际是四个 vendor:publish 的组合调用:

$this->callSilent('vendor:publish', ['--tag' => 'telescope-provider']);
$this->callSilent('vendor:publish', ['--tag' => 'telescope-assets']);
$this->callSilent('vendor:publish', ['--tag' => 'telescope-config']);
$this->callSilent('vendor:publish', ['--tag' => 'telescope-migrations']);

随后它会通过 registerTelescopeServiceProvider() 把 App\Providers\TelescopeServiceProvider 写入 config/app.php 的 providers 数组(Laravel 11 及更高版本则直接写入 bootstrap/providers.php)。

各发布标签对应的资源,见 src/TelescopeServiceProvider.php:

标签 发布内容 目标位置
telescope-migrations 数据库迁移 database/migrations
telescope-assets 前端静态资源 等 public/vendor/telescope
telescope-config 配置文件 config/telescope.php
telescope-provider TelescopeServiceProvider 模板 app/Providers/TelescopeServiceProvider.php

2.3 运行迁移并进入控制台

php artisan migrate

Telescope 使用 3 张数据表(详见 迁移文件):

  • telescope_entries:条目主表,按 uuid 唯一索引,batch_id、family_hash、created_at、(type, should_display_on_index) 均有索引,content 以 longText 存储 JSON;
  • telescope_entries_tags:条目标签表,主键 (entry_uuid, tag),外键级联删除;
  • telescope_monitoring:被监控标签表,tag 为主键。

之后启动本地服务(php artisan serve),访问 http://localhost:8000/telescope 即可看到控制台首页。若需要更换默认路径(默认 telescope),可修改配置项 telescope.path(见下文)。

生产环境提示:README 与 stubs/TelescopeServiceProvider.stub 一致表明 Telescope 定位是本地开发调试工具。生产环境使用必须显式配置授权 Gate(viewTelescope),并且推荐用 Telescope::filter() 只记录异常、失败请求、失败任务、定时任务等关键信息,避免全量记录拖垮数据库。

三、配置文件全景:每个参数的语义与默认值

安装后生成的 config/telescope.php(仓库内的原始版本见 config/telescope.php)是 Telescope 所有行为的开关总控。下面按配置块逐一说明。

3.1 总开关(Master Switch)

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

telescope.enabled 是 Telescope 的“总闸”。设 false 时,所有 Watcher 全部停用、数据不再存储。在 src/TelescopeServiceProvider.php 与 src/Telescope.php 中,start() 会在 enabled 为 false 时直接 return,boot() 也会跳过路由注册与 Watcher 启动。日常可配合环境变量一键开关:.env 中设置 TELESCOPE_ENABLED=false。

3.2 域名与路径

'domain' => env('TELESCOPE_DOMAIN'),
'path'   => env('TELESCOPE_PATH', 'telescope'),
  • domain:Telescope 使用的子域名,为 null 时与应用同域名;设置后(如 telescope.example.com)控制台只在子域名下可访问。路由注册时通过 Route::group 的 domain 参数 生效。
  • path:控制台 URI 路径,默认 /telescope。注意 配置注释 明确说明:该路径不影响内部 API(/telescope-api/*)地址。

3.3 存储驱动(Storage Driver)

'driver' => env('TELESCOPE_DRIVER', 'database'),

'storage' => [
    'database' => [
        'connection' => env('DB_CONNECTION', 'mysql'),
        'chunk' => 1000,
    ],
],
  • driver:存储驱动,默认 database。服务提供者通过 registerStorageDriver()(src/TelescopeServiceProvider.php)动态调用 register{Driver}Driver() 方法完成依赖绑定——即新增自定义驱动时,只需在服务提供者中添加同名注册方法。
  • storage.database.connection:Telescope 数据使用的数据库连接名,默认跟随应用主连接(DB_CONNECTION)。迁移文件的 getConnection() 与 EntryModel::getConnectionName() 均读取该配置,因此改连接名后需重新执行迁移。
  • storage.database.chunk:批量写入/删除的每批条数,默认 1000。仓库实现 DatabaseEntriesRepository 在 store()、prune()、clear() 中均以 chunk() 分批执行,避免一次性大事务压垮数据库。

3.4 队列配置

'queue' => [
    'connection' => env('TELESCOPE_QUEUE_CONNECTION', null),
    'queue' => env('TELESCOPE_QUEUE', null),
],

部分条目(如任务状态更新)需要在条目写入后异步补写。Telescope 会派发 ProcessPendingUpdates 队列任务,connection 与 queue 决定该任务落到哪个连接/队列;留空则使用应用默认队列。从 Telescope::store() 可以看到,派发时默认 delay(now()->addSeconds(10)) 延迟 10 秒执行,这是为了给“先记录、后补状态”的异步更新留出窗口;若更新仍失败,ProcessPendingUpdates 最多重试 3 次($attempt < 3),每次同样延迟 10 秒。另外在 Vapor 环境(存在 $_ENV['VAPOR_SSM_PATH'])会跳过该队列任务。

3.5 路由中间件

'middleware' => [
    'web',
    Authorize::class,
],

所有 Telescope 路由都会挂上这两个中间件。Authorize 内部调用 Telescope::check($request),失败即 abort(403)。你可以追加自己的中间件(如 IP 白名单)来加强访问控制。

3.6 路径与命令的监控白名单/黑名单

'only_paths' => [
    // 'api/*'
],

'ignore_paths' => [
    'livewire*',
    'nova-api*',
    'pulse*',
],

'ignore_commands' => [
    //
],
  • only_paths:只记录匹配这些 URI 模式的请求;为空数组表示不限制。
  • ignore_paths:不记录的 URI 模式(支持 * 通配),默认忽略 livewire*、nova-api*、pulse*。
  • ignore_commands:不记录的 Artisan 命令。

在 Telescope::requestIsToApprovedUri() 中,判定逻辑是:若 only_paths 非空则只放行匹配项;否则排除 telescope-api*、vendor/telescope*、Horizon 路径、ignore_paths 以及 Telescope 自身 path*。而在 runningApprovedArtisanCommand() 中,migrate:rollback、migrate:fresh、migrate:reset、migrate:install、package:discover、queue:listen、queue:work、horizon* 等命令被硬编码排除,同时合并了 ignore_commands 配置。注意:代码中同时兼容新旧两种配置键 ignoreCommands 与 ignore_commands。

3.7 Watcher 列表:18 类观察器的开关与专属参数

watchers 数组的键是 Watcher 类名,值可以是布尔值(纯开关)或数组(含 enabled 与专属选项)。注册逻辑见 src/RegistersWatchers.php:false 或 <a href="https://link.gitcode.com/i/b820e79503b64e3aec2b288b137faae2" target="_blank">'enabled' => false] 的 Watcher 会被跳过,其余通过容器实例化并调用 register($app) 挂载对应的事件监听。Watcher 基类 [src/Watchers/Watcher.php 只做两件事:接收构造参数 options 并声明抽象的 register()。

完整默认配置如下(可直接复制为自定义基线):

'watchers' => [
    Watchers\BatchWatcher::class => env('TELESCOPE_BATCH_WATCHER', true),

    Watchers\CacheWatcher::class => [
        'enabled' => env('TELESCOPE_CACHE_WATCHER', true),
        'hidden' => [],
    ],

    Watchers\ClientRequestWatcher::class => env('TELESCOPE_CLIENT_REQUEST_WATCHER', true),

    Watchers\CommandWatcher::class => [
        'enabled' => env('TELESCOPE_COMMAND_WATCHER', true),
        'ignore' => [],
    ],

    Watchers\DumpWatcher::class => [
        'enabled' => env('TELESCOPE_DUMP_WATCHER', true),
        'always' => env('TELESCOPE_DUMP_WATCHER_ALWAYS', false),
    ],

    Watchers\EventWatcher::class => [
        'enabled' => env('TELESCOPE_EVENT_WATCHER', true),
        'ignore' => [],
    ],

    Watchers\ExceptionWatcher::class => env('TELESCOPE_EXCEPTION_WATCHER', true),

    Watchers\GateWatcher::class => [
        'enabled' => env('TELESCOPE_GATE_WATCHER', true),
        'ignore_abilities' => [],
        'ignore_packages' => true,
        'ignore_paths' => [],
    ],

    Watchers\JobWatcher::class => env('TELESCOPE_JOB_WATCHER', true),

    Watchers\LogWatcher::class => [
        'enabled' => env('TELESCOPE_LOG_WATCHER', true),
        'level' => 'error',
    ],

    Watchers\MailWatcher::class => env('TELESCOPE_MAIL_WATCHER', true),

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

    Watchers\NotificationWatcher::class => env('TELESCOPE_NOTIFICATION_WATCHER', true),

    Watchers\QueryWatcher::class => [
        'enabled' => env('TELESCOPE_QUERY_WATCHER', true),
        'ignore_packages' => true,
        'ignore_paths' => [],
        'slow' => 100,
    ],

    Watchers\RedisWatcher::class => env('TELESCOPE_REDIS_WATCHER', true),

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

    Watchers\ScheduleWatcher::class => env('TELESCOPE_SCHEDULE_WATCHER', true),
    Watchers\ViewWatcher::class => env('TELESCOPE_VIEW_WATCHER', true),
],

各 Watcher 专属参数详解

  • BatchWatcher / ExceptionWatcher / JobWatcher / MailWatcher / NotificationWatcher / RedisWatcher / ScheduleWatcher / ViewWatcher:纯开关型,无专属参数,布尔值控制。
  • CacheWatcher:hidden 可列出不想记录的缓存键。
  • CommandWatcher:ignore 列出要忽略的 Artisan 命令(注意与全局 ignore_commands 的区别:这里是该 Watcher 的局部忽略列表)。
  • DumpWatcher:always 为 true 时总是记录 dump() 输出;为 false 时仅在页面打开了 Dump 面板(前端会写入 telescope:dump-watcher 缓存)才记录。src/Watchers/DumpWatcher.php 中可以看到这一缓存判断逻辑。
  • EventWatcher:ignore 列出要忽略的事件。
  • GateWatcher:ignore_abilities 忽略指定授权能力;ignore_packages(默认 true)忽略框架包自身触发的授权检查;ignore_paths 忽略指定 URI 路径下的授权检查。
  • LogWatcher:level 为最低记录级别,默认 error(即只记录 error 及以上)。实现见 src/Watchers/LogWatcher.php 的优先级表:debug=100、info=200、notice=250、warning=300、error=400、critical=500、alert=550、emergency=600,事件级别优先级低于该值则被忽略;同时,携带 exception 上下文的日志由 ExceptionWatcher 负责,LogWatcher 会跳过。
  • ModelWatcher:events 默认监听 eloquent.* 全量模型事件;hydrations(默认 true)表示是否记录模型从数据库 hydration(实例化填充)事件。
  • QueryWatcher:slow 为慢查询阈值(毫秒),默认 100ms,超过阈值的查询会被标记 slow 标签并置 slow: true(见 src/Watchers/QueryWatcher.php);ignore_packages 默认 true 忽略框架自身发起的查询;ignore_paths 忽略指定路径下的查询。
  • RequestWatcher:size_limit 为响应内容大小限制(单位 KB),默认 64,超限的 JSON/text 响应会被替换为 'Purged By Telescope'(见 src/Watchers/RequestWatcher.php 的 intdiv(mb_strlen($content), 1000) <= $limit 判断);ignore_http_methods 忽略指定 HTTP 方法;ignore_status_codes 忽略指定响应状态码的请求。

四、授权控制:谁可以打开 Telescope

4.1 默认行为:仅本地环境

安装生成的 TelescopeServiceProvider 继承自 TelescopeApplicationServiceProvider。该基类的 authorization() 方法注册了授权逻辑:

Telescope::auth(function ($request) {
    return app()->environment('local') ||
           Gate::check('viewTelescope', [$request->user()]);
});

也就是说:local 环境默认放行,非 local 环境必须通过 viewTelescope Gate。而 Authorize 中间件的 Telescope::check() 默认(未注册 auth 回调时)也等价于“仅 local 环境可访问”。

4.2 在生产/预发环境放行指定用户

在 app/Providers/TelescopeServiceProvider.php 中重写 gate() 方法,把允许访问的用户邮箱加入白名单:

protected function gate(): void
{
    Gate::define('viewTelescope', function ($user) {
        return in_array($user->email, [
            'admin@example.com',
            'dev@example.com',
        ]);
    });
}

viewTelescope Gate 接收当前登录用户,按需放行。非 local 环境务必完成此步,否则访问 /telescope 将收到 403。

五、进阶定制:过滤器、隐藏敏感信息与主题

Telescope 提供了丰富的编程式定制入口(均在应用自身的 TelescopeServiceProvider::register() 中调用,示例模板见 stubs/TelescopeServiceProvider.stub)。

5.1 条目过滤器(filter)

只保留需要关注的数据,是生产环境控制存储量的关键:

Telescope::filter(function (IncomingEntry $entry) {
    return $entry->isReportableException() ||
           $entry->isFailedRequest() ||
           $entry->isFailedJob() ||
           $entry->isScheduledTask() ||
           $entry->hasMonitoredTag();
});

安装模板默认的过滤器逻辑是:local 环境全量记录;非 local 环境只保留可上报异常(isReportableException())、失败请求(isFailedRequest(),即响应状态码 ≥ 500,见 src/IncomingEntry.php)、失败任务(isFailedJob())、定时任务与带受监控标签的条目。判定方法定义在 src/IncomingEntry.php。

从 Telescope::record() 的实现可见,过滤器在 withoutRecording() 包裹下执行(避免过滤回调自身被 Telescope 记录造成递归),全部过滤器通过后条目才进入 $entriesQueue。

5.2 按标签监控(Monitored Tags)

hasMonitoredTag() 依赖标签监控机制:在 Telescope 界面中可以添加监控标签(如 Auth:1 或 slow),此后即使非 local 环境,带有这些标签的条目也会被记录。底层由 DatabaseEntriesRepository 的 monitor() / stopMonitoring() / isMonitoring() 维护 telescope_monitoring 表,loadMonitoredTags() 在启动记录时一次性载入内存。前端对应接口见 routes/web.php 的 /telescope-api/monitored-tags。

5.3 隐藏敏感请求信息

Telescope 内置了敏感信息隐藏机制(默认值见 src/Telescope.php):请求头默认隐藏 authorization、php-auth-pw;请求参数默认隐藏 password、password_confirmation。你可以继续追加:

Telescope::hideRequestParameters(['_token']);

Telescope::hideRequestHeaders(['cookie', 'x-csrf-token', 'x-xsrf-token']);

Telescope::hideResponseParameters(['secret']);

对应方法为 hideRequestParameters()、hideRequestHeaders()、hideResponseParameters()。隐藏逻辑在 RequestWatcher 中执行:命中隐藏名单的字段值被替换为 ********。安装模板默认在非 local 环境追加隐藏 _token、cookie、x-csrf-token、x-xsrf-token(见 stubs/TelescopeServiceProvider.stub)。如果只希望展示给本地开发者看,可以直接调用 Telescope::withoutRecording(callable) 包裹某段代码,让其不被记录(src/Telescope.php)。

5.4 深色主题与自定义头像

  • Telescope::night():启用深色主题(对应 src/Telescope.php 与 public/app-dark.css)。
  • Telescope::avatar(callback):注册用户头像回调(src/Telescope.php,实现在 src/Avatar.php),可按用户信息返回自定义头像 URL。
  • Telescope::recordFrameworkEvents():让 Telescope 记录 Laravel 框架自身触发的事件(默认 $ignoreFrameworkEvents = true 忽略,见 src/Telescope.php)。
  • Telescope::tag(Closure):为条目附加自定义标签(src/Telescope.php),标签可用于界面筛选与监控。

六、数据记录与存储的底层原理

理解 Telescope 的存储机制,有助于判断它对性能的影响以及如何调优。

6.1 从采集到入库:批处理(Batch)模型

Telescope 采用“请求期入队、终止前批量落库”的模型。核心链路如下:

  1. 各 Watcher 监听 Laravel 事件(如 RequestHandled、QueryExecuted、MessageLogged、JobProcessed 等),调用 Telescope::recordXxx() 生成 IncomingEntry;
  2. 条目进入内存队列 $entriesQueue / $updatesQueue(src/Telescope.php);
  3. 在 ListensForStorageOpportunities 注册的时机集中写入:HTTP 请求与 Artisan 命令在应用 terminating 回调中触发 store();非 sync 队列任务则在每个任务处理完的 JobProcessed / JobFailed 事件后触发(src/ListensForStorageOpportunities.php);Octane 环境下则在 RequestTerminated 时停止记录并落库(src/ListensForStorageOpportunities.php)。

每次 store() 会为这一批条目生成一个有序 UUID 作为 batch_id(src/Telescope.php),同一次请求产生的所有条目(请求、查询、日志、模型事件等)共享同一个批次号,在界面上可以整批回看。Dump 条目还会通过 assignEntryPointFromBatch() 回填“由哪个请求触发”的入口信息。

6.2 存储层:DatabaseEntriesRepository

默认驱动 database 由 DatabaseEntriesRepository 实现,它同时实现 EntriesRepository、ClearableRepository、PrunableRepository、TerminableRepository 四个契约(见 src/Contracts 目录)。写入时:

6.3 状态回写:EntryUpdate 与延迟队列

部分条目(任务、请求)的生命周期是“先记录、后补状态”。例如队列任务先记录 processing 状态,再在任务完成后回写 finished 状态。这些回写以 EntryUpdate 形式进入 $updatesQueue,随 store() 一起落库;对当时尚未存在的条目(如任务完成时条目还未写入),更新会失败并进入 pendingUpdates,交由 ProcessPendingUpdates 队列任务延迟 10 秒重试,最多 3 次。

6.4 请求条目内容构成

RequestWatcher::recordRequest() 采集的字段包括:ip_address、uri、method、controller_action(控制器方法)、middleware(路由中间件列表)、headers、payload(请求输入与上传文件信息)、session、response_headers、response_status、response(按 Content-Type 分别处理:JSON 解析为数组、text/plain 保留原文、重定向记录目标地址、视图渲染记录视图路径与数据、空响应标记 Empty Response,其余标记 HTML Response)、duration(毫秒级耗时)与 memory(峰值内存,单位 MB)。这些字段构成了请求详情的完整画像。

七、日常运维:清空、裁剪、暂停与升级

Telescope 提供 4 个运维命令,全部注册于 src/TelescopeServiceProvider.php。

7.1 清空全部数据

php artisan telescope:clear

对应 src/Console/ClearCommand.php。底层 clear() 会按 chunk 分批删除 telescope_entries 与 telescope_monitoring 中全部数据。测试覆盖见 tests/Console/ClearCommandTest.php。

7.2 裁剪过期数据(推荐生产环境定期执行)

php artisan telescope:prune --hours=48
php artisan telescope:prune --keep-exceptions
  • --hours:保留最近多少小时的数据,默认 24;
  • --keep-exceptions:保留异常数据不裁剪。

对应 src/Console/PruneCommand.php。底层 prune() 删除 created_at 早于 now()->subHours($hours) 的条目,若指定 keep-exceptions 则排除 exception 类型;同样按 chunk 分批删除并返回删除条数。可以在 routes/console.php 中注册为每日调度任务实现自动清理。测试覆盖见 tests/Console/PruneCommandTest.php。

7.3 暂停 / 恢复录制

php artisan telescope:pause
php artisan telescope:resume

对应 src/Console/PauseCommand.php 与 src/Console/ResumeCommand.php。两者通过缓存键 telescope:pause-recording 控制(暂停写入 30 天有效期的缓存标记,恢复时删除)。Telescope::startRecording() 在每次启动记录时读取该缓存判断是否真的开始采集。界面右上角也提供同样的暂停/恢复按钮(对应 routes/web.php 的 /telescope-api/toggle-recording)。

7.4 版本升级

升级 Telescope 版本后,执行(详见 UPGRADE.md):

php artisan telescope:publish
php artisan view:clear

特别提醒:5.0 起 Telescope 不再自动加载自身迁移目录,升级到 5.x 时必须先发布迁移:

php artisan vendor:publish --tag=telescope-migrations
php artisan migrate

从 UPGRADE.md 可知,4.0 起要求 PHP 7.3+ / Laravel 8.0+,而当前仓库(5.x)进一步要求 PHP ^8.0 与 Laravel ^8.37 起。

7.5 重新发布资源

php artisan telescope:publish --force

对应 src/Console/PublishCommand.php:发布配置文件(--force 决定是否覆盖已有文件)与前端资源(始终强制覆盖)。日常升级或资源更新后使用。

八、路由与内部 API

所有 Telescope 路由定义在 routes/web.php,由服务提供者以 telescope 中间件组、配置的 path 前缀统一注册(src/TelescopeServiceProvider.php)。前端控制台通过内部 JSON API 取数,例如:

  • POST /telescope-api/mail、POST /telescope-api/exceptions、POST /telescope-api/queries 等:按类型分页拉取条目列表;
  • GET /telescope-api/mail/{id}/preview、/download:邮件 HTML 预览与 .eml 下载;
  • PUT /telescope-api/exceptions/{id}:标记异常已解决;
  • GET /telescope-api/monitored-tags + POST /telescope-api/monitored-tags/:管理监控标签;
  • POST /telescope-api/toggle-recording:暂停/恢复录制;
  • DELETE /telescope-api/entries:清空条目。

tests/Http/RouteTest.php 通过数据提供器逐一验证了 Mail、Exceptions、Dumps、Logs、Notifications、Jobs、Events、Cache、Queries、Models、Request、Commands、Schedule、Redis、Client Requests 等 15 类内部 API 路由均返回 200 且包含 entries 结构,可作为集成回归的参考。

九、结语:把 Telescope 变成开发习惯

Telescope 的价值不在于装好即完事,而在于形成“请求-查询-异常-任务”四位一体的排障工作流。结合本文:

  • 本地环境用默认配置即可全量洞察;
  • 生产/预发按需开启并配合 Telescope::filter()、gate() 与定期 telescope:prune 控制风险;
  • 需要深挖某条链路时,善用 batch_id 批次关联与标签监控功能。

最后,本仓库还附带完整的 Watcher 单元测试(如 tests/Watchers 下的 QueryWatcherTest、RequestWatchersTest、MailWatcherTest 等)与 ProcessPendingUpdatesTest,深入阅读这些测试能帮你更快理解每个 Watcher 的确切行为边界。

登录后查看全文
telescope