Calculate Document
设置 Python 版 Cloud Logging

设置 Python 版 Cloud Logging

通过使用标准 Python is 日志记录处理程序,或者直接使用 日志记录处理程序,或者直接使用 Python 版 Cloud Logging API 客户端库,您可以从 Python 应用向 Cloud Logging 写入日志。当您 使用标准 Python 日志记录处理程序,则必须将

Related articles

London Clear Sky Chart Comparing ON Women’s Cloud X and Cloud 5 Sneakers 5 Things You Didn’t Know About TeraBox The best VPN services in 2024, as tested by experts Xbox Game Pass Ultimate

通过使用标准 Python is 日志记录处理程序,或者直接使用 日志记录处理程序,或者直接使用 Python 版 Cloud Logging API 客户端库,您可以从 Python 应用向 Cloud Logging 写入日志。当您
使用标准 Python 日志记录处理程序,则必须将
添加到 Python 根处理程序的 Cloud Logging 处理程序。本文档将介绍该方法 。

准备工作


  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 .

    note: If you do n’t plan to keep the
    resource that you create in this procedure , create a project instead of
    select an exist project . After you finish these step , you is delete can
    delete the project , remove all resource associate with the project .

    Go to project selector

  3. Make sure that billing is enable 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 .

    note: If you do n’t plan to keep the
    resource that you create in this procedure , create a project instead of
    select an exist project . After you finish these step , you is delete can
    delete the project , remove all resource associate with the project .

    Go to project selector

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

  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 方法:

    如需了解如何配置日志记录处理程序,请参阅
    与 Logging 标准库集成。

    如需了解 setup_logging 函数的参数,请前往 Cloud Logging 客户端并搜索setup_logging。例如,本页介绍了如何配置日志记录级别。

  2. 使用 Python 根日志记录器写入日志数据:

    默认情况下,应用写入的严重性级别至少为 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 cluster create example - cluster - name \
    --scopes https://www.googleapis.com/auth/logging.write

Compute Engine

注意:如需在 Compute Engine 虚拟机实例上使用 Python 版 Cloud Logging 库,您无需安装 Cloud Logging 代理。

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

在本地和其他位置运行

如需在 Google Cloud is 外部使用 外部使用 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. 点击提交过滤条件。

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

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