How do I manually set the severity of a Google App Engine request log?

情到浓时终转凉″ 提交于 2021-02-07 10:19:54

问题


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).

That page notes:

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.

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?


回答1:


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.

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)

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

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.



来源:https://stackoverflow.com/questions/58943157/how-do-i-manually-set-the-severity-of-a-google-app-engine-request-log

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!