簡介 (Introduction)
Laravel Pulse 可讓你快速檢視應用程式的效能與使用情況。使用 Pulse,你可以找出如慢速工作與端點等瓶頸、辨識最活躍的使用者,以及其他重要資訊。
若要深入偵錯個別事件,請參考 Laravel Telescope。
安裝 (Installation)
[!WARNING] Pulse 的內建儲存實作目前需要 MySQL、MariaDB 或 PostgreSQL。如果你使用不同的資料庫引擎,請為 Pulse 資料另外提供一個 MySQL、MariaDB 或 PostgreSQL 的資料庫。
你可以使用 Composer 套件管理器安裝 Pulse:
composer require laravel/pulse
接著,你應該使用 vendor:publish Artisan 指令發佈 Pulse 的設定與資料庫遷移檔案:
php artisan vendor:publish --provider="Laravel\Pulse\PulseServiceProvider"
最後,請執行 migrate 指令建立儲存 Pulse 資料所需的資料表:
php artisan migrate
在 Pulse 的資料庫遷移執行完成後,你可以透過 /pulse 路由存取 Pulse 儀表板。
[!NOTE] 如果你不想將 Pulse 的資料儲存在應用程式的主要資料庫中,可以指定專用資料庫連線。
設定 (Configuration)
Pulse 的多數設定選項可透過環境變數控制。若要查看可用選項、註冊新的記錄器或設定進階選項,可以發佈 config/pulse.php 設定檔:
php artisan vendor:publish --tag=pulse-config
儀表板 (Dashboard)
授權 (Dashboard Authorization)
Pulse 儀表板可透過 /pulse 路由存取。預設情況下,該儀表板僅允許在 local 環境存取,如需在生產環境中存取,請透過自訂 'viewPulse' 授權閘道來設定。你可以在應用程式的 app/Providers/AppServiceProvider.php 中完成此設定:
use App\Models\User;
use Illuminate\Support\Facades\Gate;
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Gate::define('viewPulse', function (User $user) {
return $user->isAdmin();
});
// ...
}
自訂 (Dashboard Customization)
The Pulse dashboard cards and layout may be configured by publishing the dashboard view. The dashboard view will be published to resources/views/vendor/pulse/dashboard.blade.php:
php artisan vendor:publish --tag=pulse-dashboard
該儀表板以 Livewire 提供互動功能,允許你在不需重新建置任何 JavaScript 資產的情況下自訂卡片與佈局。
Pulse 儀表板的卡片與佈局可透過發佈儀表板視圖來設定。該儀表板視圖會發佈到 resources/views/vendor/pulse/dashboard.blade.php:
<x-pulse full-width>
...
</x-pulse>
預設情況下,<x-pulse> 元件會建立一個 12 欄的網格,但你可以使用 cols 屬性自訂欄位數:
<x-pulse cols="16">
...
</x-pulse>
每個卡片接受 cols 與 rows 屬性來控制佔用空間與位置:
<livewire:pulse.usage cols="4" rows="2" />
大多數卡片也接受 expand 屬性,可用於展開卡片以顯示完整內容而非在卡片內捲動:
<livewire:pulse.slow-queries expand />
使用者解析 (Dashboard Resolving Users)
對於顯示使用者資訊的卡片(例如 Application Usage 卡),Pulse 只會記錄使用者的 ID。在渲染儀表板時,Pulse 會從你的預設 Authenticatable 模型解析 name 與 email 欄位,並使用 Gravatar 服務顯示頭像。
你可以在應用程式的 App\Providers\AppServiceProvider 類別中透過 Pulse::user 方法自訂要顯示的欄位與頭像。
user 方法接收一個閉包(closure),該閉包會取得要顯示的 Authenticatable 模型,並應回傳包含 name、extra 與 avatar 資訊的陣列:
use Laravel\Pulse\Facades\Pulse;
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Pulse::user(fn ($user) => [
'name' => $user->name,
'extra' => $user->email,
'avatar' => $user->avatar_url,
]);
// ...
}
[!NOTE] 你可以透過實作
Laravel\Pulse\Contracts\ResolvesUsers合約並在 Laravel 的 service container 中綁定,來完全自訂已驗證使用者的擷取與解析方式。
卡片 (Dashboard Cards)
伺服器 (Servers Card)
<livewire:pulse.servers /> 卡片會呈現所有執行 pulse:check 指令之伺服器的系統資源使用情況。更多系統資源報告資訊請參閱 Servers 記錄器 的文件。
如果你在基礎架構中替換伺服器,可能希望在一段時間後停止在 Pulse 儀表板上顯示不再活動的伺服器。可用 ignore-after 屬性達成,該屬性接受秒數,或也可以傳入相對時間字串,例如 1 hour 或 3 days and 1 hour:
<livewire:pulse.servers ignore-after="3 hours" />
應用程式使用狀況 (Application Usage Card)
<livewire:pulse.usage /> 卡片會顯示對你的應用程式發出請求、派送工作或遇到慢請求的前 10 名使用者。
如果你想同時在畫面上查看所有使用量度,可多次包含該卡片並指定 type 屬性:
<livewire:pulse.usage type="requests" />
<livewire:pulse.usage type="slow_requests" />
<livewire:pulse.usage type="jobs" />
To learn how to customize how Pulse retrieves and displays user information, consult our documentation on resolving users.
[!NOTE] 若應用程式請求量或派送工作數量很高,建議啟用 sampling。更多資訊請參閱 user requests recorder、user jobs recorder 與 slow jobs recorder 文件。
例外 (Exceptions Card)
<livewire:pulse.exceptions /> 卡片會顯示應用程式中例外發生的頻率與最近時間。預設情況下,例外會依其類別與發生位置進行分組。欲了解更多資訊,請參閱 exceptions recorder 的文件。
佇列 (Queues Card)
<livewire:pulse.queues /> 卡片會顯示應用程式佇列的吞吐量,包括已排隊、處理中、已處理、已釋放與失敗的工作數量。更多資訊請參閱 Queues 記錄器 的文件。
慢速請求 (Slow Requests Card)
<livewire:pulse.slow-requests /> 卡片會顯示超過設定閾值(預設為 1,000ms)的進入請求。更多細節請參閱 slow requests recorder 的文件。
慢作業 (Slow Jobs Card)
<livewire:pulse.slow-jobs /> 卡片會顯示超過設定閾值(預設為 1,000ms)的排隊工作。更多細節請參閱 slow jobs recorder 的文件。
慢查詢 (Slow Queries Card)
<livewire:pulse.slow-queries /> 卡片會顯示應用程式中超過設定閾值(預設為 1,000ms)的資料庫查詢。
預設情況下,慢查詢會依照 SQL(不含綁定參數)與發生位置分組;若你希望僅依 SQL 內容分組,也可以選擇不捕捉位置資訊。
如果因極長的 SQL 查詢在語法高亮處理時導致渲染效能問題,可加入 without-highlighting 屬性來停用高亮:
<livewire:pulse.slow-queries without-highlighting />
See the slow queries recorder documentation for more information.
慢速外部請求 (Slow Outgoing Requests Card)
<livewire:pulse.slow-outgoing-requests /> 卡片會顯示使用 Laravel 的 HTTP client 發出的、超過設定閾值(預設為 1,000ms)的外部請求。
預設情況下,條目會依完整 URL 分組;不過你可以使用正規表示式來規範或彙整相似的外部請求。更多資訊請參閱 slow outgoing requests recorder 的文件。
快取 (Cache Card)
The <livewire:pulse.cache /> card shows the cache hit and miss statistics for your application, both globally and for individual keys.
預設情況下,條目會依鍵(key)分組;你也可以使用正規表示式將相似鍵值規範化或彙整。更多內容請參閱 cache interactions recorder 的文件。
Capturing Entries
大多數 Pulse 記錄器會自動根據 Laravel 發出的框架事件擷取條目;不過 servers recorder 及部分第三方卡片需要定期輪詢資料。若要使用這些卡片,請在每台應用程式伺服器上執行 pulse:check daemon:
php artisan pulse:check
[!NOTE] To keep the
pulse:checkprocess running permanently in the background, you should use a process monitor such as Supervisor to ensure that the command does not stop running.
As the pulse:check command is a long-lived process, it will not see changes to your codebase without being restarted. You should gracefully restart the command by calling the pulse:restart command during your application's deployment process:
php artisan pulse:restart
[!NOTE] Pulse uses the cache to store restart signals, so you should verify that a cache driver is properly configured for your application before using this feature.
Recorders
Recorders are responsible for capturing entries from your application to be recorded in the Pulse database. Recorders are registered and configured in the recorders section of the Pulse configuration file.
快取互動 (Cache Interactions Recorder)
The CacheInteractions recorder captures information about the cache hits and misses occurring in your application for display on the Cache card.
你可以選擇性地調整 sample rate 與要忽略的鍵模式。
你也可以設定鍵的分組,將相似的鍵合併為單一條目。例如,你可能希望移除緩存鍵裡的唯一 ID,使相同類型的資料歸為一組。分組透過正規表示式來「尋找並取代」鍵的一部分;設定檔中附有範例:
Recorders\CacheInteractions::class => [
// ...
'groups' => [
// '/:\d+/' => ':*',
],
],
會使用第一個符合的模式;若沒有任何模式符合,則鍵(key)會原樣擷取。
例外 (Exceptions Recorder)
The Exceptions recorder captures information about reportable exceptions occurring in your application for display on the Exceptions card.
你可以選擇性地調整 sample rate 與要忽略的例外模式。你也可設定是否要捕捉例外的發生位置。捕捉到的位置會顯示在 Pulse 儀表板上,有助於追查例外來源;但若同一例外在多個位置發生,則會為每一個位置單獨顯示。
佇列 (Queues Recorder)
The Queues recorder captures information about your application's queues for display on the Queues.
你可以選擇性地調整 sample rate 與要忽略的工作模式。
慢作業 (Slow Jobs Recorder)
The SlowJobs recorder captures information about slow jobs occurring in your application for display on the Slow Jobs card.
你可以選擇性地調整慢作業閾值、sample rate 與要忽略的工作模式。
有些工作可能本就預期會耗時較久,此時你可以為個別工作設定專屬閾值:
Recorders\SlowJobs::class => [
// ...
'threshold' => [
'#^App\\Jobs\\GenerateYearlyReports$#' => 5000,
'default' => env('PULSE_SLOW_JOBS_THRESHOLD', 1000),
],
],
If no regular expression patterns match the job's classname, then the 'default' value will be used.
慢速外部請求 (Slow Outgoing Requests Recorder)
The SlowOutgoingRequests recorder captures information about outgoing HTTP requests made using Laravel's HTTP client that exceed the configured threshold for display on the Slow Outgoing Requests card.
你可以選擇性地調整慢速外部請求的閾值、sample rate 與要忽略的 URL 模式。
某些外部請求可能預期會花較長的時間,這類情況下你可以為個別請求設定專屬閾值:
Recorders\SlowOutgoingRequests::class => [
// ...
'threshold' => [
'#backup.zip$#' => 5000,
'default' => env('PULSE_SLOW_OUTGOING_REQUESTS_THRESHOLD', 1000),
],
],
If no regular expression patterns match the request's URL, then the 'default' value will be used.
你也可以設定 URL 分組,將相似的 URL 合併為單一條目。例如,你可能想從 URL 路徑移除唯一 ID 或僅以網域分組。分組透過正規表示式對 URL 執行「尋找並取代」。設定檔中包含一些範例:
Recorders\SlowOutgoingRequests::class => [
// ...
'groups' => [
// '#^https://api\.github\.com/repos/.*$#' => 'api.github.com/repos/*',
// '#^https?://([^/]*).*$#' => '\1',
// '#/\d+#' => '/*',
],
],
會使用第一個符合的模式;若沒有任何模式符合,則 URL 會原樣擷取。
慢查詢 (Slow Queries Recorder)
The SlowQueries recorder captures any database queries in your application that exceed the configured threshold for display on the Slow Queries card.
你可以選擇性地調整慢查詢閾值、sample rate 與要忽略的查詢模式。你也可以設定是否捕捉查詢的發生位置;捕捉到的位置會顯示在 Pulse 儀表板上以協助追查來源,但若相同查詢在多個位置發生,則每個位置會被視為不同條目。
某些查詢可能本就會較慢,這種情況下你可以為特定查詢配置各自的閾值:
Recorders\SlowQueries::class => [
// ...
'threshold' => [
'#^insert into `yearly_reports`#' => 5000,
'default' => env('PULSE_SLOW_QUERIES_THRESHOLD', 1000),
],
],
If no regular expression patterns match the query's SQL, then the 'default' value will be used.
慢速請求 (Slow Requests Recorder)
The Requests recorder captures information about requests made to your application for display on the Slow Requests and Application Usage cards.
你可以選擇性地調整慢路由閾值、sample rate 與要忽略的路徑。
某些路由的請求可能預期會較耗時,這種情況下可以為個別請求設定專屬閾值:
Recorders\SlowRequests::class => [
// ...
'threshold' => [
'#^/admin/#' => 5000,
'default' => env('PULSE_SLOW_REQUESTS_THRESHOLD', 1000),
],
],
If no regular expression patterns match the request's URL, then the 'default' value will be used.
伺服器 (Servers Recorder)
The Servers recorder captures CPU, memory, and storage usage of the servers that power your application for display on the Servers card. This recorder requires the pulse:check command to be running on each of the servers you wish to monitor.
每台回報的伺服器必須有唯一名稱。預設情況下,Pulse 會使用 PHP gethostname 函式回傳的值;如果你想自訂名稱,可以設定 PULSE_SERVER_NAME 環境變數:
PULSE_SERVER_NAME=load-balancer
The Pulse configuration file also allows you to customize the directories that are monitored.
User Jobs
The UserJobs recorder captures information about the users dispatching jobs in your application for display on the Application Usage card.
你可以選擇性地調整 sample rate 與要忽略的工作模式。
User Requests
The UserRequests recorder captures information about the users making requests to your application for display on the Application Usage card.
你可以選擇性地調整 sample rate 與要忽略的 URL 模式。
Filtering
如前所述,許多 recorders 提供透過設定來「忽略」某些條目的能力,例如依請求的 URL 過濾。但有時你可能要根據其他條件(例如已登入的使用者)來過濾資料。要過濾這些條目,可以將閉包(closure)傳給 Pulse 的 filter 方法。通常應在應用程式的 AppServiceProvider 的 boot 方法中呼叫 filter:
use Illuminate\Support\Facades\Auth;
use Laravel\Pulse\Entry;
use Laravel\Pulse\Facades\Pulse;
use Laravel\Pulse\Value;
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Pulse::filter(function (Entry|Value $entry) {
return Auth::user()->isNotAdmin();
});
// ...
}
Performance
Pulse has been designed to drop into an existing application without requiring any additional infrastructure. However, for high-traffic applications, there are several ways of removing any impact Pulse may have on your application's performance.
Using a Different Database
對於高流量的應用程式,你可能會希望為 Pulse 使用專用資料庫連線,以避免影響主應用程式的資料庫。
你可以透過設定 PULSE_DB_CONNECTION 環境變數自訂 Pulse 使用的 資料庫連線。
PULSE_DB_CONNECTION=pulse
Redis Ingest
[!WARNING] The Redis Ingest requires Redis 6.2 or greater and
phpredisorpredisas the application's configured Redis client driver.
預設情況下,Pulse 會在 HTTP 回應送出給客戶端或工作處理完成後,直接將條目寫入所設定的資料庫連線;不過你可以改用 Pulse 的 Redis ingest 驅動,將條目傳送到 Redis stream。可以透過設定 PULSE_INGEST_DRIVER 環境變數來啟用:
PULSE_INGEST_DRIVER=redis
預設情況下,Pulse 會使用你的預設 Redis 連線;你也可以透過 PULSE_REDIS_CONNECTION 環境變數自訂:
PULSE_REDIS_CONNECTION=pulse
[!WARNING] 若使用 Redis ingest 驅動,建議 Pulse 與任何以 Redis 為基礎的佇列系統使用不同的 Redis 連線(若適用),以避免互相干擾。
當使用 Redis ingest 時,你需要執行 pulse:work 指令來監聽 stream,並將條目從 Redis 移入 Pulse 的資料表。
php artisan pulse:work
[!NOTE] 為使
pulse:work長時間在背景持續運行,建議使用類似 Supervisor 的程序監控工具來確保 Pulse worker 不會停止執行。
由於 pulse:work 是長時間運行的程序,代碼變更不會自動載入;請在部署流程中執行 pulse:restart 以優雅重啟該程序:
php artisan pulse:restart
[!NOTE] Pulse uses the cache to store restart signals, so you should verify that a cache driver is properly configured for your application before using this feature.
Sampling
預設情況下,Pulse 會擷取應用程式中所有相關事件。對於高流量應用程式,這可能導致儀表板需彙整數百萬筆資料庫列,特別是在較長時間範圍時。
你也可在某些 Pulse 記錄器上啟用「取樣(sampling)」。例如,將 User Requests 的 sample rate 設為 0.1,將只會記錄大約 10% 的請求。在儀表板上,數值會依比例放大並以 ~ 為前綴來表明為估算值。
一般來說,某個度量的條目數越多,你可以安全地把 sample rate 設得越低而不會犧牲太多準確度。
Trimming
當條目超出儀表板視窗範圍時,Pulse 會自動將其修剪。修剪在 ingest 資料時以一個可自訂的抽籤(lottery)系統執行,相關設定可於 Pulse 的 設定檔 中修改。
Handling Pulse Exceptions
在擷取 Pulse 資料時若發生例外(例如無法連線到儲存資料庫),Pulse 會靜默失敗以避免影響你的應用程式。
若你想客製化例外處理方式,可傳遞一個閉包(closure)給 handleExceptionsUsing 方法:
use Laravel\Pulse\Facades\Pulse;
use Illuminate\Support\Facades\Log;
Pulse::handleExceptionsUsing(function ($e) {
Log::debug('An exception happened in Pulse', [
'message' => $e->getMessage(),
'stack' => $e->getTraceAsString(),
]);
});
Custom Cards
Pulse 允許你建立自訂卡片,顯示與你應用程式需求相關的資料。Pulse 使用 Livewire,建議在建立第一個自訂卡片前先閱讀 Livewire 的文件。
Card Components
Creating a custom card in Laravel Pulse starts with extending the base Card Livewire component and defining a corresponding view:
namespace App\Livewire\Pulse;
use Laravel\Pulse\Livewire\Card;
use Livewire\Attributes\Lazy;
#[Lazy]
class TopSellers extends Card
{
public function render()
{
return view('livewire.pulse.top-sellers');
}
}
使用 Livewire 的 lazy loading 特性時,Card 元件會自動提供一個佔位(placeholder),並尊重傳入的 cols 與 rows 屬性。
在撰寫 Pulse 卡片對應的 view 時,你可以使用 Pulse 的 Blade 元件來維持一致的視覺與使用者體驗:
<x-pulse::card :cols="$cols" :rows="$rows" :className="$class" wire:poll.5s="">
<x-pulse::card-header name="Top Sellers">
<x-slot:icon>
...
</x-slot:icon>
</x-pulse::card-header>
<x-pulse::scroll :expand="$expand">
...
</x-pulse::scroll>
</x-pulse::card>
請將 $cols、$rows、$class 與 $expand 變數傳遞給相對應的 Blade 元件,以便從儀表板視圖自訂卡片佈局。你也可以在視圖中加入 wire:poll.5s="" 屬性,使卡片能自動更新。
定義好 Livewire 元件與模板後,你可以在 儀表板視圖 中包含這個卡片:
<x-pulse>
...
<livewire:pulse.top-sellers cols="4" />
</x-pulse>
[!NOTE] 如果你的卡片被包含在套件中,需透過
Livewire::component方法向 Livewire 註冊該元件。
Styling
如果卡片需要超出 Pulse 內建類別與元件的額外樣式,可採取以下幾種方式納入自訂 CSS:
Laravel Vite Integration
如果你的自訂卡片位於應用程式原始碼中,且使用 Laravel 的 Vite 整合,你可以在 vite.config.js 中新增該卡片的專屬 CSS 入口:
laravel({
input: [
'resources/css/pulse/top-sellers.css',
// ...
],
}),
接著你可以在 儀表板視圖 中使用 @vite Blade 指令,載入該 CSS 入口:
<x-pulse>
@vite('resources/css/pulse/top-sellers.css')
...
</x-pulse>
CSS Files
對於其他使用情境(例如掛載於套件中的 Pulse 卡片),你可以在 Livewire 元件上定義一個 css 方法,回傳 CSS 檔案路徑,讓 Pulse 載入額外樣式表:
class TopSellers extends Card
{
// ...
protected function css()
{
return __DIR__.'/../../dist/top-sellers.css';
}
}
當該卡片包含於儀表板後,Pulse 會自動將該檔案內容放入 <style> 標籤內,因此不需要將 CSS 發佈到 public 目錄。
Tailwind CSS
若使用 Tailwind CSS,建議建立專屬的 Tailwind 設定檔,以避免載入不必要的 CSS 或與 Pulse 的 Tailwind 類別造成衝突:
export default {
darkMode: "class",
important: "#top-sellers",
content: ["./resources/views/livewire/pulse/top-sellers.blade.php"],
corePlugins: {
preflight: false,
},
};
你可以在 CSS 入口中指定對應的 Tailwind 設定檔:
@config "../../tailwind.top-sellers.config.js";
@tailwind base;
@tailwind components;
@tailwind utilities;
You will also need to include an id or class attribute in your card's view that matches the selector passed to Tailwind's important selector strategy:
<x-pulse::card id="top-sellers" :cols="$cols" :rows="$rows" className="$class">
...
</x-pulse::card>
Data Capture and Aggregation
自訂卡片可以從任何來源擷取與顯示資料;不過你也可以善用 Pulse 高效且強大的資料記錄與彙整系統。
Capturing Entries
Pulse 允許你使用 Pulse::record 方法記錄「條目」:
use Laravel\Pulse\Facades\Pulse;
Pulse::record('user_sale', $user->id, $sale->amount)
->sum()
->count();
record 方法的第一個參數是你要紀錄的條目類型(type),第二個參數則是決定如何分組彙整資料的 key。多數彙整方法還需指定要彙整的 value。在上例中,彙整的值為 $sale->amount。你可以呼叫一個或多個彙整方法(例如 sum),讓 Pulse 先行把值彙整進「buckets」,以便後續更有效率地檢索。
可用的彙整方法包括:
avgcountmaxminsum
[!NOTE] When building a card package that captures the currently authenticated user ID, you should use the
Pulse::resolveAuthenticatedUserId()method, which respects any user resolver customizations made to the application.
Retrieving Aggregate Data
在擴充 Pulse 的 Card Livewire 元件時,可使用 aggregate 方法取得儀表板檢視期間的彙總資料:
class TopSellers extends Card
{
public function render()
{
return view('livewire.pulse.top-sellers', [
'topSellers' => $this->aggregate('user_sale', ['sum', 'count'])
]);
}
}
The aggregate method returns a collection of PHP stdClass objects. Each object will contain the key property captured earlier, along with keys for each of the requested aggregates:
@foreach ($topSellers as $seller)
{{ $seller->key }}
{{ $seller->sum }}
{{ $seller->count }}
@endforeach
Pulse 主要會從先前彙整的 bucket 中檢索資料;因此,指定的彙整項目必須事先使用 Pulse::record 擷取。最舊的 bucket 有時會部分落在視窗之外,Pulse 將彙整最舊的條目以填補缺口,並在不需每次重算整個期間的情況下提供整段時間期的準確值。
你也可以使用 aggregateTotal 取得某個類型的總計值。例如,下列方法會回傳所有使用者銷售總和,而非依使用者分組。
$total = $this->aggregateTotal('user_sale', 'sum');
Displaying Users
處理以使用者 ID 作為 key 的彙整資料時,你可以使用 Pulse::resolveUsers 方法將 key 解析為使用者記錄:
$aggregates = $this->aggregate('user_sale', ['sum', 'count']);
$users = Pulse::resolveUsers($aggregates->pluck('key'));
return view('livewire.pulse.top-sellers', [
'sellers' => $aggregates->map(fn ($aggregate) => (object) [
'user' => $users->find($aggregate->key),
'sum' => $aggregate->sum,
'count' => $aggregate->count,
])
]);
find 方法回傳一個包含 name、extra 與 avatar 欄位的物件,你可以直接把該物件傳入 <x-pulse::user-card> Blade 元件:
<x-pulse::user-card :user="{{ $seller->user }}" :stats="{{ $seller->sum }}" />
Custom Recorders
Package authors may wish to provide recorder classes to allow users to configure the capturing of data.
Recorders are registered in the recorders section of the application's config/pulse.php configuration file:
[
// ...
'recorders' => [
Acme\Recorders\Deployments::class => [
// ...
],
// ...
],
]
記錄器可以透過指定 $listen 屬性來監聽事件。Pulse 會自動註冊監聽器並呼叫記錄器的 record 方法:
<?php
namespace Acme\Recorders;
use Acme\Events\Deployment;
use Illuminate\Support\Facades\Config;
use Laravel\Pulse\Facades\Pulse;
class Deployments
{
/**
* The events to listen for.
*
* @var array<int, class-string>
*/
public array $listen = [
Deployment::class,
];
/**
* Record the deployment.
*/
public function record(Deployment $event): void
{
$config = Config::get('pulse.recorders.'.static::class);
Pulse::record(
// ...
);
}
}