设置 Python 版 Cloud Logging

通过使用标准 Python 日志记录处理程序,或者直接使用 Python 版 Cloud Logging API 客户端库,您可以从 Python 应用向 Cloud Logging 写入日志。当您 使用标准 Python 日志记录处理程序,则必须将 添加到 Python 根处理程序的 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.

    Go to project selector

  3. 确保您的 Google Cloud 项目已启用结算功能

  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. 确保您的 Google Cloud 项目已启用结算功能

  7. Enable the Cloud Logging API.

    Enable the API

  8. 准备 Python 开发环境。

    转到 Python 设置指南

安装库

如需安装 Python 版 Cloud Logging 库,请参阅 安装 Python 版客户端库。 借助此库,您可以将 Cloud Logging 处理程序附加到标准 Python 根处理程序。您还可以使用此库将 API 向 Cloud Logging 发送多个请求。

如需详细了解如何安装,请参阅 Python 版 Cloud Logging 库的文档。您还可以使用问题跟踪器来报告问题。

使用标准 Python 日志记录处理程序写入日志

如需将使用标准 Python 根处理程序写入的所有日志条目发送到 Cloud Logging,请执行以下操作:

  1. 通过调用以下命令,将 Cloud Logging 处理程序附加到 Python 根日志记录器 setup_logging 方法:

    # 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()

    如需了解如何配置日志记录处理程序,请参阅 与 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 或 然后处理程序会将请求发送到相应 资源类型;否则,日志会列在python日志 Global 资源类型。

使用 Cloud Logging 客户端库写入日志

如需了解如何使用 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 角色。 如果您将适用于 GKE 的工作负载身份联合与此默认服务账号搭配使用,以允许工作负载访问特定 Google Cloud API,则无需进行额外配置。但是,如果您将工作负载身份联合用于 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 APIs 的全面访问权限。无论您选择哪个服务账号,都请确保已在 Google Cloud 控制台的 IAM 和管理部分向该服务账号授予 Logs Writer 角色

在本地和其他位置运行

如需在 Google Cloud 外部使用 Python 版 Cloud Logging 库, 包括在您自己的工作站、数据中心的 或其他云服务提供商的虚拟机实例上,则必须 提供您的 Google Cloud 项目 ID 和相应的 服务账号 登录到 Python 版 Cloud Logging 库。

对于现有服务账号,请执行以下操作:

  1. 向服务账号授予 IAM Logs Writer (roles/logging.logWriter) IAM 角色。 如需详细了解 IAM 请参阅访问权限控制

  2. 设置应用默认凭据

如果您没有服务账号,请创建一个。有关 此过程,请参阅创建服务账号

如需了解可用于身份验证的方法的一般信息,请参阅术语:服务账号

查看日志

在 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 和您的配置。例如,在您的应用中,您可以指定资源或使用默认资源。

  • 如果您在日志浏览器中没有看到任何日志,若要查看所有日志条目,请切换到高级查询模式并使用空查询。

    1. 如需切换到高级查询模式,请点击日志浏览器顶部的菜单 (▾),然后选择转换为高级过滤条件
    2. 清除过滤条件框中显示的内容。
    3. 点击提交过滤条件

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

如需了解详情,请参阅使用日志浏览器