Chris Twichell
Chris Twichell

Reputation: 21

How to disable HTTP response debug?

My python code is currently printing the following line to the logs at the conclusion of every REST API call (high-level info on the response it is sending back):

111.111.111.111 - - [15/Aug/2017:12:03:15 +0000] "POST /some_endpoint 
HTTP/1.1" 202 72 "-" "python-requests/2.2.1 CPython/3.4.3 Linux/3.13.0-83-generic"

Through other searches I've seen the following suggestions on how to get rid of it:

1) logging.getLogger('werkzeug').setLevel(logging.ERROR)

2) logging.getLogger('werkzeug').disabled = True

3) Same as above, but requests instead of werkzeug

These have no effect. Another solution offered up was to use a completely different stream for logging but that's not going to be an option for my needs.

This is obviously getting logged through a separate mechanism than the rest of my logs since the format is different, but I can't seem to pinpoint the culprit.

Upvotes: 2

Views: 1400

Answers (1)

Alexander McFarlane
Alexander McFarlane

Reputation: 11293

In general, you can find the logging instance in any library and adjust the logging level

You can access the logging utility via werkzeug._internal

from werkzeug._internal import _logger

then I adjust the root logger to logging.CRITICAL (which is 50)

_logger.setLevel(50)

This means that Werkzeug will only print CRITICAL (or higher) output

You can then adjust the _logger level according got your needs

Direct example

This applies to your example by searching for the import logging command in the Werkzeug github page. This I found in werkzeug._internal then we can do

In [2]: from werkzeug.wrappers import Request, Response
   ...:
   ...: @Request.application
   ...: def application(request):
   ...:     return Response('Hello World!')
   ...:

In [3]: from werkzeug.serving import run_simple

In [4]: run_simple('localhost', 4000, application)
 * Running on http://localhost:4000/ (Press CTRL+C to quit)

In [5]: from werkzeug import _internal

In [6]: _internal._log('info', 'Setting logger level to Critical!')  # see below why this is required
Out[6]: 'Setting logger level to Critical!'

In [7]: _internal._logger.setLevel(50)

In [7]: run_simple('localhost', 4000, application)

In the case the _logger is None then this occurs because no Werkzeug logging instance has been called. See line 75 in _internal for more clarity

In [1]: from werkzeug import _internal

In [2]: type(_internal._logger)
Out[2]: NoneType

In [3]: _internal._log('info', 'removing logger!')
removing logger!

In [4]: type(_internal._logger)
Out[4]: logging.Logger

Upvotes: 1

Related Questions