設定 Python 適用的 Cloud Logging

您可以使用標準 Python 記錄處理常式,或直接使用 Python 適用的 Cloud Logging API 用戶端程式庫,將記錄從 Python 應用程式寫入至 Cloud Logging。使用標準 Python 記錄處理常式時,必須將 Cloud Logging 處理常式附加至 Python 根處理常式。本文件將說明這項做法。

事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Logging API.

    Enable the API

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

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Logging API.

    Enable the API

  8. 設定適當的 Python 開發環境。

    前往 Python 設定指南

  9. 安裝程式庫

    如要安裝 Python 適用的 Cloud Logging 程式庫,請參閱「安裝 Python 用戶端程式庫」。這個程式庫可讓您將 Cloud Logging 處理常式附加至標準 Python 根處理常式。您也可以使用這個程式庫將 API 要求傳送至 Cloud Logging。

    如要進一步瞭解安裝相關資訊,請參閱 Python 適用的 Cloud Logging 程式庫說明文件。您也可以使用問題追蹤工具回報問題。

    使用標準 Python 記錄處理常式寫入記錄

    如要將所有使用標準 Python 根處理常式寫入的記錄項目傳送至 Cloud Logging,請執行下列步驟:

    1. 呼叫 setup_logging 方法,將 Cloud Logging 處理常式附加至 Python 根記錄器:

      # Imports the Cloud Logging client library
      import google.cloud.logging
      
      # Instantiates a client
      client = google.cloud.logging.Client()
      
      # Retrieves a Cloud Logging handler based on the environment
      # you're running in and integrates the handler with the
      # Python logging module. By default this captures all logs
      # at INFO level and higher
      client.setup_logging()

      如要進一步瞭解如何設定記錄處理常式,請參閱「與記錄標準程式庫整合」。

      如要瞭解 setup_logging 函式的參數,請前往 Cloud Logging 用戶端,搜尋 setup_logging。例如,本頁面說明如何設定記錄層級。

    2. 使用 Python 根目錄記錄器寫入記錄資料:

      # Imports Python standard library logging
      import logging
      
      # The data to log
      text = "Hello, world!"
      
      # Emits the data using the standard logging module
      logging.warning(text)

      根據預設,任何嚴重性等級至少為 INFO 的記錄,只要是應用程式寫入的,就會傳送至 Cloud Logging。

      如果訊息是從 App Engine 或 Google Kubernetes Engine 記錄至 Logging,則處理程序會將訊息傳送至這些環境的相對應資源類型;否則,記錄會列在 Global 資源類型的 python 記錄下方。

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

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

    在 Google Cloud上執行

    應用程式必須具備 Logs Writer (roles/logging.logWriter) IAM 角色,才能使用 Python 適用的 Cloud Logging 程式庫寫入記錄。大多數環境都會自動將這個角色指派給預設服務帳戶。 Google Cloud

    App Engine

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

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

    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」 部分中執行此操作。請使用 Compute Engine 預設服務帳戶或您選擇的其他服務帳戶,並選取「Identity and API access」(身分及 API 存取權) 區段的「Allow full access to all Cloud APIs」(允許所有 Cloud API 的完整存取權)。無論您選取哪個服務帳戶,請確認該帳戶已在Google Cloud 主控台的「IAM & Admin」區段中取得「Logs Writer」角色。

    在本機和其他位置執行

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

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

    查看記錄

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

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

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

    在記錄檔瀏覽器中,您必須指定一或多個資源,但資源的選取可能並不明顯。下列提示可協助您著手操作:

    • 如果您將應用程式部署至 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 與您的設定。例如,在您的應用程式中,您可以指定資源或使用預設資源。

    • 如果您在記錄檔探索工具中看不到任何記錄,請切換至進階查詢模式並使用空白查詢,以便查看所有記錄項目。

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

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

    如需更多資訊,請參閱「使用記錄檔探索工具」。