- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Google App Engine Python 3 标准环境中有一个应用程序。我将其设置为根据他们的请求对日志条目进行分组,如 Writing Application Logs 中所述。 (在“查看相关请求日志条目”部分)。
该页注释:
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 自动创建的请求日志。它始终处于日志级别“任何”。我没有看到如何更改其日志级别/严重性。
这是我现在拥有的代码(的一个分支):
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')
将日志与他们的请求分组非常有效,但是父(请求)日志,例如,
> * 2019-11-19 15:54:56.613 EST GET 200 1.21 KiB 390ms Chrome 78 /
正在显示“任何”日志级别。我希望它是适当的“信息”或“错误”。我怎样才能做到这一点?
最佳答案
我还没有找到修改自动生成日志的方法。
我仍然有兴趣听听是否有更好的方法,所以我不会将此答案标记为已接受。
但我找到了一种非常符合我需求的解决方法,即添加我自己的请求日志并忽略自动生成的请求日志。
对于每个子日志,我使用与问题代码中相同的想法,但更新了 flask 的 g
对象上的一个新属性,以跟踪到目前为止请求中最严重的日志级别。
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
中级别的整数索引)
然后,为了生成新的请求日志,我使用了 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
中添加一些内容即可完成这项工作:
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
这工作得相当好 - 我很想听听任何让它变得更好的想法。
关于python-3.x - 如何手动设置 Google App Engine 请求日志的严重性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58943157/
我正在使用 boost::log 的简单日志记录库,我想根据当前设置的记录器严重性执行一些代码。只有在输出日志消息时才需要构建日志消息。但是我找不到查询严重性的正确方法。代码必须类似于: if (bo
我有一个运行良好的内核模块。我可以在 insmod 阶段传递参数,但我想创建一个“详细”参数。当设置为 true 时,我的 printks 从 KERN_INFO 移动到 KERN_ERR 严重性。
我有以下脚本来连接到我的 Microsoft Azure 服务器。 getMessage() . "\n"; exit; } echo "Passed!"; 上面的脚本在我的旧服务器上传递,但
网络数据包负载中指定的长度与读取的字节数不匹配;连接已关闭。请联系客户端库的供应商。错误:17836,严重性:20,状态:14。我几乎同时收到此错误 5 次,想知道其发生的原因。 最佳答案 查看解决方
这个问题已经有答案了: "Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key",
我正在连接到外部 MSSQL 数据库以从 PHP55/osx 导出,但我遇到了奇怪的问题。 代码: new \PDO("dblib:host={$hostname};dbname={$dbname}"
这是我的代码详细信息 Step 1. /etc/freetds/freettds.conf [mssql] host = 192.168.1.10 port = 1433 tds version =8
我正在尝试从运行 SQL Server 实例的服务器登录该实例,同时以我自己的身份进行远程访问。 我不断收到标准登录失败错误,并且在错误日志中看到“错误:18456,严重性:14,状态:11。” 登录
这个问题在这里已经有了答案: Reference - What does this error mean in PHP? (38 个答案) 关闭 4 年前。 我的 Controller : func
我敢肯定这个问题已经被提出了无数次,但也许,有人仍然可以帮助我。 我将 pymssql v2.1.3 与 Python 2.7.12 一起使用,直到昨天我多次使用将数据写入我的 Azure SQL 数
我刚刚开始使用 codeigniter、PHP 并在尝试通过从数据库获取数据来动态构建 UI 时遇到问题: 要求:我需要通过从数据库读取信息来动态创建父子列表。我有两个表 admin_menu 和 a
我正在尝试运行这个 program conn = pymssql.connect(host='localhost', user='notsa', password='notsayly', datab
我有一个动态选择?它携带另一个 select 中的值。正在充电。但是显示此消息: 严重性:警告消息:为 foreach() 提供的参数无效文件名:helpers/form_helper.php 这是我
严重性:8192 消息:在未来的 PHP 版本中,与类同名的方法将不再是构造函数; CI_Pagination 有一个已弃用的构造函数 文件名:libraries/Pagination.php 行号:
我正在尝试在我的 php 脚本上连接到 Azure Microsoft SQL Server 数据库。我不明白为什么它不起作用。当我运行 db_connection.php 脚本时,出现以下错误: S
我正在尝试在我的 php 脚本上连接到 Azure Microsoft SQL Server 数据库。我不明白为什么它不起作用。当我运行 db_connection.php 脚本时,出现以下错误: S
我正在尝试使用 PHP 的 PDO 连接到 Microsoft SQL Server/Microsoft Azure 数据库: getMessage() . "\n"; exit; } PDO
在 create-react-app 下创建新项目时,您会立即收到有关 postcss 中发现的漏洞的警告。 . npm 报告的问题:https://www.npmjs.com/advisories/
尝试通过命令连接到 remort mssql 服务器时 tsql -S SQLEXPRESS -U sa 密码:sa它抛出错误错误 20009(严重性 9):无法连接:Adaptive Server
在现场收到以下错误 " 遇到 PHP 错误 严重性:核心警告 消息:模块“ffmpeg”已加载 文件名:未知行号:0 回溯:”。 但我没有在本地主机中收到此错误。我正在使用 codeigniter 3
我是一名优秀的程序员,十分优秀!