【问题标题】:How to log AppEnginePlatformWarning as warning not as error如何将 AppEnginePlatformWarning 记录为警告而不是错误
【发布时间】:2018-03-18 09:40:19
【问题描述】:

我的项目使用这些 python 库:

requests==2.18.4
requests-toolbelt==0.8.0

并在 main.py 中调用它

requests_toolbelt.adapters.appengine.monkeypatch()

当使用请求库时,应用引擎在谷歌云日志中记录错误消息:

AppEnginePlatformWarning:urllib3 正在 Google 应用上使用 URLFetch 引擎沙箱而不是套接字。直接使用套接字而不是 URLFetch 见 https://urllib3.readthedocs.io/en/latest/reference/urllib3.contrib.html.

AppEnginePlatformWarning:URLFetch 不支持粒度超时 设置,恢复为总或默认 URLFetch 超时。

但这实际上只是警告,我的代码按预期工作。但是这个日志对我来说是有问题的,因为当我看到统计信息时,我不知道日志中是否真的有错误。这就是为什么我必须以某种方式将其记录为警告。

这是一个 stackoverflow 答案。该答案表明,如果在 GAE 标准环境中显示此警告,则代码将毫无问题地工作。所以这对我来说真的是一个警告。如何记录它? AppEnginePlatformWarning: urllib3 is using URLFetch on Google App Engine sandbox instead of sockets

【问题讨论】:

    标签: python google-app-engine python-requests


    【解决方案1】:

    您可以使用logging.captureWarnings 函数来做到这一点。

    来自docs

    此功能用于通过登录开启捕获警告 并关闭。

    如果捕获为真,warnings 模块发出的警告将是 重定向到日志系统。具体来说,将发出警告 使用warnings.formatwarning() 和结果字符串格式化 记录到名为“py.warnings”的记录器,严重性为 WARNING。

    如果 capture 为 False,则将警告重定向到日志系统 将停止,警告将被重定向到原来的 目的地(即在 captureWarnings(True) 之前生效的目的地) 调用)。

    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
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-10
      • 1970-01-01
      • 2011-04-01
      • 1970-01-01
      相关资源
      最近更新 更多