Peculiarity of '@' in logging.Formatter

Charlie Martin chasrmartin at gmail.com
Mon Nov 21 18:17:05 EST 2011


This is what seems like an odd bug, but in code I'd thing often-enough used it must be the expected behavior and I just don't understand.  Please, sirs/mesdames, is this a bug?

Example code:
---------------- begin code -------------------
#!/usr/bin/env python

"""
@-character WTF?
"""
import sys
import os
import logging, logging.handlers
import socket

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)

fmtColon = logging.Formatter('[%(module)s:%(lineno)03d]:%(message)s')
strC = logging.handlers.SysLogHandler(address='/dev/log')
strC.setFormatter(fmtColon)
strC.setLevel(logging.DEBUG)
log.addHandler(strC)

fmtAt = logging.Formatter('[%(module)s@%(lineno)03d]:%(message)s')
strA = logging.handlers.SysLogHandler(address='/dev/log')
strA.setFormatter(fmtAt)
strA.setLevel(logging.DEBUG)
log.addHandler(strA)

log.info("My log message:isn't it special?")
---------------- end code ----------------

produces these entries in the syslog messages:

---------------- begin results ----------------------
Nov 21 16:09:56 crmartin [atSign: 026]:My log message:isn't it special?
Nov 21 16:09:56 crmartin [atSign at 026]: My log message:isn't it special?
---------------- end results ------------------------

Observe: 

* in the first entry, "[atSign: 026]:My" with space after the first ":"; that space isn't in the format string.

* in the second entry "[atSign at 026]: My" again has an additional space after the first ":"

the colons following are unchanged.

This **seems** like it must be some obscure bug, but perhaps it's some undocumented feature?



More information about the Python-list mailing list