makkasi
makkasi

Reputation: 7328

How to log AppEnginePlatformWarning as warning not as error

My project uses these python libraries:

requests==2.18.4
requests-toolbelt==0.8.0

and calls this at the main.py

requests_toolbelt.adapters.appengine.monkeypatch()

App engine is logging Error message inside google cloud logs when using the requests library:

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.

but this is actually just Warning and my code works as expected. But this log is problematic for me because I cannot know if there is really error or not in the logs when I see the statistics. That's why I have to log it as warning somehow.

Here is one stackoverflow answer. This answer states that if this warning is shown on GAE standard environment then the code will work without problem. So it's really warning for me. How to log it as such? AppEnginePlatformWarning: urllib3 is using URLFetch on Google App Engine sandbox instead of sockets

Upvotes: 4

Views: 284

Answers (1)

snakecharmerb
snakecharmerb

Reputation: 55933

You can do this using the logging.captureWarnings function.

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

Executing logging.captureWarnings(True) in appengine_config.py causes these warnings to be logged as warnings for me.

See also the docs for the warnings module.

Edit:

This question includes this code fragment for suppressing the message altogether:

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

Upvotes: 5

Related Questions