[Python-Dev] Proposed additional keyword argument in logging calls

Vinay Sajip vinay_sajip at red-dove.com
Tue Nov 22 16:17:19 CET 2005

On numerous occasions, requests have been made for the ability to easily add
user-defined data to logging events. For example, a multi-threaded server
application may want to output specific information to a particular server
thread (e.g. the identity of the client, specific protocol options for the
client connection, etc.)

This is currently possible, but you have to subclass the Logger class and
override its makeRecord method to put custom attributes in the LogRecord.
These can then be output using a customised format string containing e.g.
"%(foo)s %(bar)d". The approach is usable but requires more work than

I'd like to propose a simpler way of achieving the same result, which
requires use of an additional optional keyword argument in logging calls.
The signature of the (internal) Logger._log method would change from

  def _log(self, level, msg, args, exc_info=None)


  def _log(self, level, msg, args, exc_info=None, extra_info=None)

The extra_info argument will be passed to Logger.makeRecord, whose signature
will change from

  def makeRecord(self, name, level, fn, lno, msg, args, exc_info):


  def makeRecord(self, name, level, fn, lno, msg, args, exc_info,

makeRecord will, after doing what it does now, use the extra_info argument
as follows:

If type(extra_info) != types.DictType, it will be ignored.

Otherwise, any entries in extra_info whose keys are not already in the
LogRecord's __dict__ will be added to the LogRecord's __dict__.

Can anyone see any problems with this approach? If not, I propose to post
the approach on python-list and then if there are no strong objections,
check it in to the trunk. (Since it could break existing code, I'm assuming
(please correct me if I'm wrong) that it shouldn't go into the
release24-maint branch.)

Of course, if anyone can suggest a better way of doing it, I'm all ears :-)


Vinay Sajip

More information about the Python-Dev mailing list