設定 PHP 適用的 Cloud Logging

您可以直接使用 PHP 適用的 Cloud Logging 程式庫,從 PHP 應用程式將記錄寫入 Cloud Logging。

事前準備

  1. 登入 Google Cloud 帳戶。如果您未曾使用過 Google Cloud,歡迎建立帳戶,親自體驗實際使用我們產品的成效。新客戶還能獲得價值 $300 美元的免費抵免額,用於執行、測試及部署工作負載。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Logging API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Logging API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

安裝 PHP 專用的 Cloud Logging 程式庫

PHP 適用的 Cloud Logging 程式庫為 PHP 網頁框架提供簡單的 PSR-3 記錄器實作。

如要從應用程式寫入記錄,請將 PHP 適用的 Cloud Logging 程式庫新增至 composer.json:

composer require google/cloud-logging

建立 PSR-3 記錄器

請使用下列程式碼來建立 PSR-3 記錄器:

use Google\Cloud\Logging\LoggingClient;
$logging = new LoggingClient([
    'projectId' => $projectId
]);
$logger = $logging->psrLogger('app');

啟用批次處理選項

PSR-3 記錄器會同步傳送記錄。也就是說,每當您發出記錄時,系統就會在使用者要求中加入 RPC 延遲時間。特別是當您在單一要求中發出多個記錄時,增加的延遲時間會很明顯。您可能會想要避免此情況的發生。

以下程式碼建立了一個 PSR-3 記錄器,將多個記錄批次處理到單一遠端程序呼叫 (RPC) 中:

$logger = LoggingClient::psrBatchLogger('app');

根據預設,此記錄器會在單一程序內批次處理記錄。在 App Engine 彈性環境中,您可以設定 PHP 適用的 Cloud Logging 程式庫,進一步提高處理量並降低延遲。詳情請參閱設定記錄 Daemon 的說明。

設定 PHP 適用的 Cloud Logging 程式庫

您可以自訂 PHP 適用的 Cloud Logging 程式庫行為。如需可能的設定選項清單,請參閱設定文件。

使用 PSR-3 記錄器

建立記錄器之後,您可在應用程式中使用記錄器:

$logger->info('Hello World');
$logger->error('Oh no');

如要進一步瞭解安裝作業,請參閱 PHP 適用的 Cloud Logging 程式庫說明文件。您也可以使用 Issue Tracker 回報問題。

使用 Cloud Logging 用戶端程式庫寫入記錄

如要瞭解如何直接使用 PHP 適用的 Cloud Logging 用戶端程式庫,請參閱「Cloud Logging 用戶端程式庫」。

在 Google Cloud執行

如要讓應用程式使用 PHP 適用的 Cloud Logging 程式庫寫入記錄,基礎資源的服務帳戶必須具備記錄檔寫入者 (roles/logging.logWriter) IAM 角色。多數 Google Cloud 環境會自動設定預設服務帳戶,使其具備這個角色。

App Engine

系統會自動為 App Engine 啟用 Cloud Logging,且應用程式的預設服務帳戶預設具備寫入記錄項目的 IAM 權限。

詳情請參閱「寫入及查看記錄」。

但是,我們建議您使用 PSR-3 記錄器,自動將中繼資料新增至您的記錄。這樣一來,您的應用程式記錄就會與要求記錄相互關聯。

設定 App Engine 彈性環境適用的 Daemon

在 App Engine 彈性環境中,您可以設定 PHP 適用的 Cloud Logging 程式庫,使用外部 Daemon 程式,盡可能提高記錄處理量,並盡可能縮短應用程式的延遲時間。如要啟用這項功能,請在 app.yaml 的 runtime_config 區段中新增下列程式碼:

enable_stackdriver_integration: true

Google Kubernetes Engine (GKE)

GKE 會自動將記錄檔寫入者 (roles/logging.logWriter) IAM 角色授予預設服務帳戶。如果您使用 Workload Identity Federation for GKE 搭配這個預設服務帳戶,讓工作負載存取特定Google Cloud API,則不需進行其他設定。不過,如果您使用 Workload Identity Federation for GKE 搭配自訂 IAM 服務帳戶,請務必確認自訂服務帳戶具備記錄檔寫入者角色 (roles/logging.logWriter)。

如有需要,您也可以在建立叢集時,使用下列指令新增 logging.write 存取權範圍:

gcloud container clusters create example-cluster-name \
    --scopes https://www.googleapis.com/auth/logging.write

Compute Engine

使用 Compute Engine VM 執行個體時,請將 cloud-platform 存取權範圍新增至每個執行個體。透過Google Cloud 控制台建立新執行個體時,您可以在「Create Instance」(建立執行個體) 面板的「Identity and API access」(身分和 API 存取權) 區段中執行這項操作。使用 Compute Engine 預設服務帳戶或您選擇的其他服務帳戶,然後在「Identity and API access」(身分和 API 存取權) 區段中選取「Allow full access to all Cloud APIs」(允許所有 Cloud API 的完整存取權)。無論您選取哪個服務帳戶,請務必在Google Cloud 控制台的「IAM & Admin」(IAM 與管理) 專區中,將記錄檔寫入者角色授予該帳戶。

在本機和其他位置執行

如要在 Google Cloud以外的地方使用 PHP 適用的 Cloud Logging 程式庫,包括在自己的工作站、資料中心的電腦或其他雲端服務供應商的 VM 執行個體上執行程式庫,您必須在本機環境中設定應用程式預設憑證 (ADC),向 PHP 適用的 Cloud Logging 程式庫進行驗證。

詳情請參閱「為地端部署或其他雲端服務供應商設定 ADC」。

查看記錄

前往 Google Cloud 控制台的 「Logs Explorer」頁面:

前往「Logs Explorer」(記錄檔探索工具)

如果您是使用搜尋列尋找這個頁面,請選取子標題為「Logging」的結果。

在 Logs Explorer 中,您必須指定一或多項資源,但資源選取可能不明顯。下列提示可協助您著手操作:

  • 如果您將應用程式部署至 App Engine 或使用 App Engine 特定程式庫,請將您的資源設定為 GAE 應用程式。

  • 如果您在 Compute Engine 上部署應用程式,請將資源設定為 GCE VM 執行個體。

  • 如果您在 Google Kubernetes Engine 上部署應用程式,叢集的記錄設定可決定記錄項目的資源類型。如要詳細瞭解舊版 Google Cloud Observability 和 Google Cloud Observability Kubernetes Monitoring 解決方案,以及這些選項如何影響資源類型,請參閱「遷移至 Google Cloud Observability Kubernetes Monitoring」。

  • 如果應用程式直接使用 Cloud Logging API,資源會依附於 API 和您的設定。例如,在您的應用程式中,您可以指定資源或使用預設資源。

  • 如果 Logs Explorer 中未顯示任何記錄,請切換至進階查詢模式,並使用空白查詢,即可查看所有記錄項目。

    1. 如要切換至進階查詢模式,請按一下 Logs Explorer 頂端的「選單」(▾),然後選取「Convert to advanced filter」(轉換為進階篩選器)。
    2. 清除顯示在篩選器方塊中的內容。
    3. 按一下 [Submit Filter] (提交篩選器)。

    您可以檢查個別項目,以識別您的資源。

詳情請參閱「使用記錄檔探索工具」。