Help me understand the django.request logger

1,468 views
Skip to first unread message

Roy Smith

unread,
Sep 22, 2011, 2:58:29 PM9/22/11
to Django users
I'm running django 1.3, python 2.6. I'm trying to write a custom
formatter for the django.request logger. I need to log one of the
headers from the HTTP request. The documentation for django.request
(https://docs.djangoproject.com/en/1.3/topics/logging/) says:

Messages to this logger have the following extra context:

status_code: The HTTP response code associated with the request.
request: The request object that generated the logging message.

It's not entirely clear what "extra context" means, but I'm assuming
the record passed to my format() method will include those
attributes. It doesn't. My formatter looks like:

class UniqueRequestIdFormatter(logging.Formatter):
def format(self, record):
record.message = record.msg % record.args
record.asctime = self.formatTime(record)
format = '%(asctime)s: %(name)s %(levelname)s %(funcName)s %
(message)s'
pprint.pprint(record.__dict__)
return format % record.__dict__

When I log something to this, it prints:

{'args': (),
'asctime': '2011-09-22 14:54:53,755',
'created': 1316717693.7554641,
'exc_info': None,
'exc_text': None,
'filename': 'views.py',
'funcName': 'listen_content',
'levelname': 'DEBUG',
'levelno': 10,
'lineno': 253,
'message': u"slug = 'essential-motown-hits-songza'",
'module': 'views',
'msecs': 755.46407699584961,
'msg': u"slug = 'essential-motown-hits-songza'",
'name': 'django.request',
'pathname': '/home/roy/src/default/djsite/djfront/views.py',
'process': 18113,
'processName': 'MainProcess',
'relativeCreated': 16236.304044723511,
'thread': 48007945393920,
'threadName': 'Dummy-1'}

How do I get access to the request object?

Roy Smith

unread,
Sep 22, 2011, 5:56:24 PM9/22/11
to Django users
Ugh, I got it figured out. I totally misunderstood what the docs were
saying. I was thinking that somehow the django.request logger
magically found the request object and stuck it into the context.
That's not what it was saying at all. All the quote from the docs
really means is that all the places inside of django where
django.request is used, the request is explicitly passed in as part of
extra.

Sigh.

Russell Keith-Magee

unread,
Sep 22, 2011, 7:24:53 PM9/22/11
to django...@googlegroups.com

Hi Roy,

I'm glad you were able to work this out for yourself. If you want to
be really helpful here, you could turn what you've learned into an
improvement in the docs. Old-timers (like myself) often forget what is
obvious and what is assumed knowledge, but since we are the ones
writing the docs, we sometimes forget to include a pertinent detail.
If you're new to a framework (or new to a particular area of a
framework) you're often the best person to provide feedback on
problems in the docs.

So - if you want to be really helpful, open a ticket [1] describing
the aspect of the docs that was confusing, either by ommision or as a
result of phrasing. Suggest some improved phrasing, or indicate the
detail that is missing. If you're really keen, try working up a patch
that implements your changes.

[1] https://code.djangoproject.com/newticket

Yours,
Russ Magee %-)

Reply all
Reply to author
Forward
0 new messages