如何将AppEnginePlatformWarning记录为warning而不是as

2024-06-12 21:53:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我的项目使用以下python库:

requests==2.18.4
requests-toolbelt==0.8.0

打电话到主.py在

^{pr2}$

使用请求库时,App engine正在google云日志中记录错误消息:

AppEnginePlatformWarning: urllib3 is using URLFetch on Google App Engine sandbox instead of sockets. To use sockets directly instead of URLFetch see https://urllib3.readthedocs.io/en/latest/reference/urllib3.contrib.html.

AppEnginePlatformWarning: URLFetch does not support granular timeout settings, reverting to total or default URLFetch timeout.

但这实际上只是警告,我的代码按预期工作。但是这个日志对我来说有问题,因为当我看到统计数据时,我不知道日志中是否真的有错误。这就是为什么我要把它记录为警告。在

这里有一个stackoverflow答案。这个答案说明,如果这个警告显示在GAE标准环境中,那么代码将正常工作。所以这对我来说真的是个警告。如何记录? AppEnginePlatformWarning: urllib3 is using URLFetch on Google App Engine sandbox instead of sockets


Tags: ofapp警告ison错误google记录
1条回答
网友
1楼 · 发布于 2024-06-12 21:53:56

您可以使用logging.captureWarnings函数来执行此操作。在

docs

This function is used to turn the capture of warnings by logging on and off.

If capture is True, warnings issued by the warnings module will be redirected to the logging system. Specifically, a warning will be formatted using warnings.formatwarning() and the resulting string logged to a logger named 'py.warnings' with a severity of WARNING.

If capture is False, the redirection of warnings to the logging system will stop, and warnings will be redirected to their original destinations (i.e. those in effect before captureWarnings(True) was called).

appengine_config.py中执行logging.captureWarnings(True)会导致这些警告被记录为对我的警告。在

另请参阅warnings模块的文档。在

编辑:

This question包含此代码片段,用于完全抑制消息:

# Use the App Engine Requests adapter. This makes sure that Requests uses
# URLFetch.
requests_toolbelt.adapters.appengine.monkeypatch()

# squelch warning
requests.packages.urllib3.disable_warnings(
    requests.packages.urllib3.contrib.appengine.AppEnginePlatformWarning
)

相关问题 更多 >