This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: logging.error('asdf', extra={'msg':123}) raise KeyError exception
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: amezhenin, christian.heimes
Priority: normal Keywords:

Created on 2013-07-09 12:42 by amezhenin, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (2)
msg192744 - (view) Author: Артем Меженин (amezhenin) Date: 2013-07-09 12:42
Hello.
I noticed strange behavior of logging module, when debugging my code:

$ python
Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logging.error('asdf')
ERROR:root:asdf
>>> logging.error('asdf', extra={'msg':123})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/logging/__init__.py", line 1575, in error
    root.error(msg, *args, **kwargs)
  File "/usr/lib/python2.7/logging/__init__.py", line 1166, in error
    self._log(ERROR, msg, args, **kwargs)
  File "/usr/lib/python2.7/logging/__init__.py", line 1257, in _log
    record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
  File "/usr/lib/python2.7/logging/__init__.py", line 1235, in makeRecord
    raise KeyError("Attempt to overwrite %r in LogRecord" % key)
KeyError: "Attempt to overwrite 'msg' in LogRecord"


Originally code was extra=locals() and 'msg' was local variable, but it doesn't matter.
msg192746 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-07-09 13:04
The issue is covered by the docs:

http://docs.python.org/2.7/library/logging.html#logging.Logger.debug

> The keys in the dictionary passed in extra should not clash with the keys used by the logging system.

You can't use a couple of reserved keys like 'msg' in the extra args. Perhaps you can find a simple workaround for your issue?
History
Date User Action Args
2022-04-11 14:57:47adminsetgithub: 62613
2013-07-09 13:04:20christian.heimessetstatus: open -> closed

nosy: + christian.heimes
messages: + msg192746

resolution: wont fix
2013-07-09 12:43:30amezheninsettype: behavior
components: + Library (Lib)
versions: + Python 2.7
2013-07-09 12:42:45amezhenincreate