The Logger.debug example (at <a href="http://docs.python.org/py3k/library/logging.html#logging.Logger.debug">http://docs.python.org/py3k/library/logging.html#logging.Logger.debug</a>) does not actually work in Python 3.2:<br>
<br>Python 3.2 (r32:88452, Feb 20 2011, 11:12:31) <br>[GCC 4.2.1 (Apple Inc. build 5664)] on darwin<br>Type "help", "copyright", "credits" or "license" for more information.<br>>>> import logging<br>
>>> FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s'<br>>>> logging.basicConfig(format=FORMAT)<br>>>> d = {'clientip': '192.168.0.1', 'user': 'fbloggs'}<br>
>>> logging.warning('Protocol problem: %s', 'connection reset', extra=d)<br>Traceback (most recent call last):<br> File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/logging/__init__.py", line 934, in emit<br>
msg = self.format(record)<br> File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/logging/__init__.py", line 809, in format<br> return fmt.format(record)<br> File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/logging/__init__.py", line 551, in format<br>
s = self.formatMessage(record)<br> File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/logging/__init__.py", line 520, in formatMessage<br> return self._style.format(record)<br> File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/logging/__init__.py", line 371, in format<br>
return self._fmt % record.__dict__<br>KeyError: 'asctime'<br>Logged from file <stdin>, line 1<br><br>The problem is that the logger expects to see the exact string "%(asctime)s" in the format string before it will add the asctime attribute. Using the "-15" modifier on the asctime causes the match to fail, and the asctime attribute is not emitted. To fix the example, change %(asctime)-15s to %(asctime)s:<br>
<br>Python 3.2 (r32:88452, Feb 20 2011, 11:12:31) <br>[GCC 4.2.1 (Apple Inc. build 5664)] on darwin<br>Type "help", "copyright", "credits" or "license" for more information.<br>>>> import logging<br>
>>> FORMAT = '%(asctime)s %(clientip)s %(user)-8s %(message)s'<br>>>> logging.basicConfig(format=FORMAT)<br>>>> d = { 'clientip' : '192.168.0.1', 'user' : 'fbloggs' }<br>
>>> logger = logging.getLogger('tcpserver')<br>>>> logger.warning('Protocol problem: %s', 'connection reset', extra=d)<br>2011-07-12 13:39:44,317 192.168.0.1 fbloggs Protocol problem: connection reset<br>
<br>This also reflects a problem in the library in that it is not flexible enough to understand different formats. As a separate matter, perhaps the library could be amended to always include the asctime field (the asctime string could be lazily computed by using a class instance for which the __str__ method produces the actual formatted date), rather than trying to guess when it is needed.<br>
<br>Robert<br>