Google App Engine 请求日志,包括严重性

Google App Engine request log including severity

我正在尝试实施 this Whosebug 答案中的方法,让我的日志按请求分组,并显示请求中子日志的最高严重级别。

这是我目前得到的:

custom_logger.py:

import inspect
import json
import os

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

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

resource = Resource(type='gae_app',
                    labels={'project_id': os.environ['GOOGLE_CLOUD_PROJECT'],
                            'module_id': os.environ['GAE_SERVICE'],
                            'version_id': os.environ['GAE_VERSION']})

client = gcp_logging.Client()
custom_logger = client.logger('custom_logger')
request_logger = client.logger('request_logger')


def log_request(response):
    trace_id = get_trace_id()
    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.json() or json.loads(request.data.decode())
    else:
        payload = {}
    request_logger.log_struct(payload,
                              trace=trace_id,
                              http_request=request_info,
                              severity=severity)


def default(text):
    _log(text)


def log(text):
    return default(text)


def debug(text, *args):
    _log(text, *args)


def info(text, *args):
    _log(text, *args)


def warning(text, *args):
    _log(text, *args)


def warn(text, *args):
    return warning(text, *args)


def error(text, *args):
    _log(text, *args)


def critical(text, *args):
    _log(text, *args)


def _log(text, *args):
    trace_id = get_trace_id()
    severity = inspect.stack()[1][3].upper()
    new_level = LOG_LEVELS.index(severity)
    previous_level = g.get('log_level', 0)
    g.log_level = max(previous_level, new_level)
    message = text % args
    custom_logger.log_text(message, resource=resource,
                           severity=severity, trace=trace_id)


def get_trace_id():
    return (f"projects/{os.environ['GOOGLE_CLOUD_PROJECT']}/traces/"
            f"{request.headers['X-Cloud-Trace-Context'].split('/')[0]}")

main.py:

import json
import time

from flask import Flask, g, request, make_response
from flask_cors import CORS

import custom_logger as logging

app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'


@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):
    logging.log_request(response)
    return response

@app.route('/', methods=['GET', 'OPTIONS'])
def _test():
    logger.debug('DEBUG %s', 'TEST')
    logger.info('INFO %s', 'TEST')
    logger.warning('WARNING %s', 'TEST')
    logger.error('ERROR %s', 'TEST')
    logger.critical('CRITICAL %s', 'TEST')
    return 'Success'

request_logger.log_struct 函数似乎没有将任何条目添加到日志中。如果我在 request_logger.log_struct 函数之前添加一个 request_logger.log_text 函数,那么这最终会出现在日志中。为什么我在日志中看不到 request_logger.log_struct 的结果?

将“资源”属性 添加到 log_request 函数中的 log_struct 调用后,我可以在“日志查看器”中看到请求。现在对日志进行分组,并将最高严重性级别添加到父日志条目。

request_logger.log_struct(payload,
                          resource=resource,
                          trace=trace_id,
                          http_request=request_info,
                          severity=severity)