Django: Ticket Queryhttps://code.djangoproject.com/query?status=!closed&keywords=~WSGIRequest&order=priority
The Web framework for perfectionists with deadlines.en-USDjangohttps://www.djangoproject.com/s/img/site/hdr_logo.gifhttps://code.djangoproject.com/query?status=!closed&keywords=~WSGIRequest&order=priority
Trac 1.2.2https://code.djangoproject.com/ticket/27234
https://code.djangoproject.com/ticket/27234#27234: Clarify the type of the django.server logger's 'request' extra contextFri, 16 Sep 2016 06:12:55 GMTBen Whale<p>
This bug (if indeed it is a bug) is probably related to: <a class="closed ticket" href="https://code.djangoproject.com/ticket/27233" title="#27233: Bug: IndexError when using django.request (closed: invalid)">#27233</a> (<a href="https://code.djangoproject.com/ticket/27233">https://code.djangoproject.com/ticket/27233</a>).
</p>
<p>
The documentation for the django.request logger (<a class="ext-link" href="https://docs.djangoproject.com/en/1.10/topics/logging/#django-request"><span class="icon">​</span>https://docs.djangoproject.com/en/1.10/topics/logging/#django-request</a>) implies that the request variable (passed in as extra context) is a request (or something request like). Instead, it can be either an instance of WSGIRequest or socket.
</p>
<p>
I expected it to be something request like so that it would be possible to log the GET parameters or the POST data. I'm pretty sure that the socket the webserver listens to shouldn't be passed to a logging object that a user could then interact with.
</p>
<p>
How to replicate:
I'm going to give to examples, the first will show that the request variable is a socket, the second will show that it is sometimes a WSGIRequest and sometimes a socket.
</p>
<p>
First example:
1) install vanilla django and set up a project.
2) Put the following into the settings.py file:
</p>
<pre class="wiki">LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'custom': {
'()': 'django.utils.log.ServerFormatter',
'format': '[%(server_time)s] %(message)s %(request)r',
}
},
'handlers': {
'custom': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'custom',
},
},
'loggers': {
'django.request': {
'handlers': ['custom'],
'level': 'DEBUG',
'propagate': False,
},
}
}
</pre><p>
3) Edit the file &lt;path_to_django_install&gt;/django/utils/log.py and put the following <code>print repr(record.request)</code> into the format method of the ServerFormatter object
4) use <code>python manage.py runserver</code> to launch the server
5) navigate to a valid page, e.g. 127.0.0.1:8000 (I use port 8001 because reasons)
</p>
<p>
You should see something like
</p>
<pre class="wiki"># python manage.py runserver 8001
System check identified no issues (0 silenced).
September 16, 2016 - 06:01:12
Django version 1.10.1, using settings 'mwe.settings'
Starting development server at http://127.0.0.1:8001/
Quit the server with CONTROL-C.
&lt;socket._socketobject object at 0x7fc4fccc4c90&gt;
[16/Sep/2016 06:01:14] "GET / HTTP/1.1" 200 1767
</pre><p>
The line <code>&lt;socket._socketobject object at 0x7fc4fccc4c90&gt;</code> is the string representation of the request.
</p>
<p>
Second example:
Using the same set up as for the first example navigate to an invalid page, e.g. <code>127.0.0.1:8001/foo</code>
</p>
<p>
You should see something like
</p>
<pre class="wiki">System check identified no issues (0 silenced).
September 16, 2016 - 06:06:15
Django version 1.10.1, using settings 'mwe.settings'
Starting development server at http://127.0.0.1:8001/
Quit the server with CONTROL-C.
&lt;WSGIRequest: GET '/foo'&gt;
Traceback (most recent call last):
File "/usr/lib/python2.7/logging/__init__.py", line 861, in emit
msg = self.format(record)
File "/usr/lib/python2.7/logging/__init__.py", line 734, in format
return fmt.format(record)
File "/home/benwhale/Documents/Projects/mwe/venv/local/lib/python2.7/site-packages/django/utils/log.py", line 174, in format
if args[1][0] == '2':
IndexError: tuple index out of range
Logged from file base.py, line 152
&lt;socket._socketobject object at 0x7f4997751a60&gt;
[16/Sep/2016 06:06:18] "GET /foo HTTP/1.1" 404 1909
</pre><p>
on the command line. Please ignore the IndexError for the purposes of this bug see <a href="https://code.djangoproject.com/ticket/27233">https://code.djangoproject.com/ticket/27233</a>. Observe the two lines <code>&lt;WSGIRequest: GET '/foo'&gt;</code> and <code>&lt;socket._socketobject object at 0x7f4997751a60&gt;</code> these indicate that the request object can be either a WSGIRequest or a socket object.
</p>
<p>
Placing a break point in the format method is not sufficient to see both calls with WSGIRequest and socket objects as, at least for me, the break point would only trigger once.
</p>
<p>
The code paths that result in both calls are slightly different and result from a difference in the handlers array in the Logger object. This difference in code path also responsible for the IndexError and so fixing this ticket is likely to be related to fixing ticket <a class="closed ticket" href="https://code.djangoproject.com/ticket/27233" title="#27233: Bug: IndexError when using django.request (closed: invalid)">#27233</a>.
</p>
Resultshttps://code.djangoproject.com/ticket/27234#changelog