如何手动设置Google App Engine请求日志的严重性? [英] How do I manually set the severity of a Google App Engine request log?

查看:57
本文介绍了如何手动设置Google App Engine请求日志的严重性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google App Engine Python 3标准环境中有一个应用程序.我已按照

I have an app in the Google App Engine Python 3 Standard Environment. I have it set up to group log entries by their request, as described in Writing Application Logs (in the "Viewing related request log entries" section).

该页面指出:

子级"日志条目中的最高严重性不会自动应用于顶级条目.如果需要这种行为,请在顶级条目中手动设置最高严重性.

The highest severity from the "child" log entries does not automatically apply to the top-level entry. If that behavior is desired, manually set the highest severity in the top-level entry.

有问题的顶级条目是App Engine自动创建的请求日志.它始终处于日志级别任何".我没有看到如何更改其日志级别/严重性.

The top-level entry in question is the request log that App Engine creates automatically. It's always at log level "Any". I'm not seeing how to change its log level/severity.

这是我现在拥有的代码(其中一个分支):

Here's (one branch of) the code I have now:

import os
from flask import request
from google.cloud import logging as gcp_logging
from google.cloud.logging.resource import Resource

client = gcp_logging.Client()
logger = client.logger('my_custom_log_type')
trace_id = (f"projects/{os.environ['GOOGLE_CLOUD_PROJECT']}/traces/"
            f"{request.headers['X-Cloud-Trace-Context'].split('/')[0]}")
res = Resource(type='gae_app',
               labels={'project_id': os.environ['GOOGLE_CLOUD_PROJECT'],
                       'module_id': os.environ['GAE_SERVICE'],
                       'version_id': os.environ['GAE_VERSION']})
logger.log_text('Sample log text', resource=res, trace=trace_id, severity='INFO')

将日志与请求分组在一起是一种完美的方法,但是父级(请求)日志例如

It's working perfectly to group the logs with their requests, but the parent (request) log, e.g.,

> * 2019-11-19 15:54:56.613 EST  GET  200  1.21 KiB  390ms  Chrome 78  /

以任何"日志级别显示.我希望适当地将其设置为信息"或错误".我该如何做到这一点?

is displaying with the "Any" log level. I'd like it to be "Info" or "Error" as appropriate. How can I make this happen?

推荐答案

我还没有找到修改自动生成的日志的方法.

I've not found a way to tinker with the auto-generated log.

我仍然想知道是否有更好的方法,因此我不会将此答案标记为已接受.

I'm still interested in hearing if there's a better way, so I'm not marking this answer as accepted.

但是我发现了一种解决方法,可以通过添加我自己的请求日志并忽略自动生成的请求日志来满足我的需求.

But I have found a workaround that pretty much meets my needs, by adding my own request log and ignoring the automatically generated one.

对于每个子日志,我使用与问题代码相同的思想,但是在flask的 g 对象上更新了一个新属性,以跟踪请求中迄今为止最严格的日志级别.

For each child log, I use the same idea as in the question's code, but update a new attribute on flask's g object to keep track of the most severe log level so far within the request.

from flask import g

LOG_LEVELS = ('DEFAULT', 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL')

previous_level = g.get('log_level', 0)
g.log_level = max(previous_level, new_level)

(其中new_level是 LOG_LEVELS 中级别的整数索引)

(where new_level is the integer index of the level in LOG_LEVELS)

然后,为了生成新的请求日志,我使用了缩小版本的

Then, to generate the new request log, I use a scaled-down version of this solution:

def log_request(response):
    logger = client.logger('my_request_logger')
    severity = LOG_LEVELS[g.get('log_level', 0)]
    request_info = {
        'requestMethod': request.method,
        'requestUrl': request.url,
        'status': response.status_code,
        'userAgent': request.headers.get('USER-AGENT'),
        'responseSize': response.content_length,
        'latency': g.request_duration(),
        'remoteIp': request.remote_addr
    }
    if request.method == 'POST':
        payload = request.get_json() or json.loads(request.data.decode())
    else:
        payload = {}
    logger.log_struct(payload,
                      trace=trace_id,
                      http_request=request_info,
                      severity=severity)

以上内容位于 my_logging 模块中.然后,只需对 main.py 进行一些补充,即可完成这项工作:

The above was in a my_logging module. Then, just a couple of additions to main.py to make this work:

import time
from flask import g
import my_logging

@app.before_request
def setup_timing():
    g.request_start_time = time.time()
    g.request_duration = lambda: f'{(time.time() - g.request_start_time):.5f}s'

@app.after_request
def log_request(response):
    my_logging.log_request(response)
    return response

这工作得相当不错-我很想听听任何改善它的想法.

This is working reasonably well - I'd love to hear any ideas to make it better.

这篇关于如何手动设置Google App Engine请求日志的严重性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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