设置 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 根处理程序。您还可以使用此库向 Cloud Logging 发送 API 请求。

    如需详细了解如何安装,请参阅 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上运行

    如需使应用可使用 Python 版 Cloud Logging 库写入日志,底层资源的服务账号必须具有 Logs Writer (roles/logging.logWriter) IAM 角色。大多数 Google Cloud 环境会自动将默认服务账号配置为具有此角色。

    App Engine

    系统会自动为 App Engine 启用 Cloud Logging,并且应用的默认服务账号默认拥有 IAM 权限以写入日志条目。

    如需了解详情,请参阅写入和查看日志

    Google Kubernetes Engine (GKE)

    GKE 会自动向默认服务账号授予 Logs Writer (roles/logging.logWriter) IAM 角色。如果您将 Workload Identity Federation for GKE 与此默认服务账号搭配使用,以允许工作负载访问特定Google Cloud API,则无需进行其他配置。不过,如果您将 Workload Identity Federation for GKE 与自定义 IAM 服务账号搭配使用,请确保该自定义服务账号具有 Logs Writer (roles/logging.logWriter) 角色。

    如果需要,您还可以在创建集群时使用以下命令添加 logging.write 访问权限范围:

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

    Compute Engine

    使用 Compute Engine 虚拟机实例时,请为每个实例添加 cloud-platform 访问权限范围。通过Google Cloud 控制台创建新实例时,您可以在创建实例面板的身份和 API 访问权限部分中执行此操作。请使用 Compute Engine 默认服务账号或您所选的其他服务账号,并在身份和 API 访问权限部分中选择授予对所有 Cloud API 的完整访问权限。无论您选择哪个服务账号,都请确保已在Google Cloud 控制台的 IAM 和管理部分中向该服务账号授予 Logs Writer 角色

    在本地和其他位置运行

    如需在 Google Cloud外部使用 Python 版 Cloud Logging 库(包括在您自己的工作站、数据中心的计算机或其他云服务提供商的虚拟机实例上运行该库),您必须在本地环境中设置应用默认凭证 (ADC),以向 Python 版 Cloud Logging 库进行身份验证。

    如需了解详情,请参阅为本地或其他云服务提供商设置 ADC

    查看日志

    在 Google Cloud 控制台中,转到 Logs Explorer 页面:

    前往 Logs Explorer

    如果您使用搜索栏查找此页面,请选择子标题为 Logging 的结果。

    在日志浏览器中,您必须指定一个或多个资源,但资源的选择并不那么显而易见。以下是帮助您上手的一些提示:

    • 如果您将把应用部署到 App Engine 或者您使用的是 App Engine 专用的库,请将资源设置为 GAE 应用

    • 如果您将在 Compute Engine 上部署应用,请将资源设置为 GCE 虚拟机实例

    • 如果您要在 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 顶部的菜单 (▾),然后选择转换为高级过滤条件
      2. 清除过滤条件框中显示的内容。
      3. 点击提交过滤条件

      您可以检查各个条目以找出您的资源。

    如需了解更多信息,请参阅使用 Logs Explorer