如何对相关请求日志条目进行分组GAE python 3.7 standard env [英] How to group related request log entries GAE python 3.7 standard env

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

问题描述

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

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]/跟踪/[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服务日志网络控制台

The log doesn't appear in the GAE service log web console

推荐答案

您可能想看看我在此处提供的答案.

(此答案解决了如何将日志记录严重性添加到写入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库,以及此 一个 有关一些用例示例.

[...], 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.

请注意,为了让日志位于正确的资源下, 您将需要手动配置它们,请参见此 列表 支持的资源类型.同样,每种资源类型都有 某些必填 标签 需要在日志结构中显示的内容.

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字段更改为与gae_app标签中的相同https://cloud.google.com/logging/docs/api/v2/resource-list#resource-types"rel =" nofollow noreferrer>此处.

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 standard env的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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