如何将相关的请求日志条目分组 GAE python 3.7 标准环境 [英] How to group related request log entries GAE python 3.7 standard env

查看:17
本文介绍了如何将相关的请求日志条目分组 GAE python 3.7 标准环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Google App Engine python 3.7 标准,并且正在尝试对相关的请求日志条目进行分组.根据 Writing Application Logs 文档,我应该:

I'm using Google App Engine python 3.7 standard and i'm trying to group related request log entries. According to the Writing Application Logs documentation, I should:

在应用日志的 LogEntry 跟踪字段中设置跟踪标识符条目.预期的格式是项目/[PROJECT_ID]/traces/[TRACE_ID]

Set the trace identifier in the LogEntry trace field of your app log entries. The expected format is projects/[PROJECT_ID]/traces/[TRACE_ID]

应该在哪里/如何使用 LogEntry?

Where/How should use LogEntry?

Stackdriver Logging 文档 没有显示它是如何可能的.我错过了什么吗?

The Stackdriver Logging documentation doesn't show how it's possible. Am I missing something?

代码示例将不胜感激.

[更新]按照 Duck Hunt Duo 的建议,我尝试了以下操作,但没有成功:

[UPDATE] Following Duck Hunt Duo advice, I tried the following, without any success:

    trace_id = request.headers.get('X-Cloud-Trace-Context', 'no_trace_id').split('/')[0]
    client = logging.Client()
    logger = client.logger('appengine.googleapis.com%2Fstdout')  # Not shown
    # logger = client.logger('projects/{}/logs/stdout'.format(GOOGLE_CLOUD_PROJECT)) # error
    # logger = client.logger('projects/{}/logs/appengine.googleapis.com%2Fstdout'.format(GOOGLE_CLOUD_PROJECT)) # error

    logger.log_text('log_message', trace=trace_id)

日志未出现在 GAE 服务日志网络控制台

推荐答案

您可能想看看我在这里提供的答案.

(此答案解决了如何为写入 Stackdriver 的 Cloud Functions 日志添加日志严重性,但基本工作流程相同)

(This answer addresses how to add logging severity to Cloud Functions logs written into Stackdriver, but the basic workflow is the same)

引用:

[...],您仍然可以通过使用Stackdriver Logging Client图书馆.检查 这个文档参考 Python 库,以及 this一个一些用例示例.

[...], you can still create logs with certain severity by using the Stackdriver Logging Client Libraries. Check this documentation in reference to the Python libraries, and this one for some usage-case examples.

注意,为了让日志在正确的资源下,您必须手动配置它们,请参阅 this清单对于支持的资源类型.同样,每种资源类型都有一些 需要标签需要出现在日志结构中.

Notice that in order to let the logs be under the correct resource, you will have to manually configure them, see this list for the supported resource types. As well, each resource type has some required labels that need to be present in the log structure.

使用 App Engine 示例更新上一个答案:

Updating the previous answer with an example for App Engine:

from google.cloud import logging
from google.cloud.logging.resource import Resource
from flask import Flask

app = Flask(__name__)

@app.route('/')
def logger():
    log_client = logging.Client()
    log_name = 'appengine.googleapis.com%2Fstdout'

    res = Resource( type='gae_app',
                    labels={
                        "project_id": "MY-PROJECT-ID",
                        "module_id": "MY-SERVICE-NAME"
                       })

    logger = log_client.logger(log_name)

    logger.log_struct({"message": "message string to log"}, resource=res, severity='ERROR') # As an example log message with a ERROR warning level

    return 'Wrote logs to {}.'.format(logger.name)

以此代码为例,将日志的资源类型更改为 appengine.googleapis.com%2Fstdout 应该可以工作,并将 Resource 字段更改为与 此处.

By using this code as example, and changing the resource type of the log to appengine.googleapis.com%2Fstdout should work, and change the Resource fields to be the same as in the gae_app labels described in here.

这篇关于如何将相关的请求日志条目分组 GAE python 3.7 标准环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆