Extra fields for logging
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Fri Dec 25 23:34:32 EST 2009
On Fri, 25 Dec 2009 12:07:20 -0800, Joan Miller wrote:
> On 25 dic, 13:24, Steven D'Aprano <st... at REMOVE-THIS-
> cybersource.com.au> wrote:
>> On Thu, 24 Dec 2009 05:06:48 -0800, Joan Miller wrote:
>> > I'm trying to add some extra fields to logging, I'm following this
>> > information [1] but it faills in my case.
>> [...]
>> > I get => KeyError: 'host'
>>
>> Please post the entire traceback you get.
>>
>> --
>> Steven
>
> Traceback (most recent call last):
> File "/usr/lib/python2.6/logging/__init__.py", line 768, in emit
> msg = self.format(record)
> File "/usr/lib/python2.6/logging/__init__.py", line 648, in format
> return fmt.format(record)
> File "/usr/lib/python2.6/logging/__init__.py", line 439, in format
> s = self._fmt % record.__dict__
> KeyError: 'host'
Hmmm... that wasn't as helpful as I had hoped. Oh well.
Going back to your earlier post, I can see a couple of problems.
Firstly, your __getitem__ method always returns None. You need to return
the result.
Secondly, this works for me:
import logging
class ExtraInfo(object):
def __getitem__(self, name):
if name == 'host':
result = 'foo'
return result
def __iter__(self):
keys = ['host',]
keys.extend(self.__dict__.keys())
return iter(keys) # better than keys.__iter__
def setup(filename='/tmp/foo.log'):
log = logging.LoggerAdapter(logging.getLogger('foo'), ExtraInfo())
logging.basicConfig(
level=logging.DEBUG,
format="""Date-Time: %(asctime)s
Host: %(host)s
%(levelname)s:
%(message)s""",
datefmt="%Y-%m-%dT%H:%M:%S%z",
filename=filename,
filemode='w')
return log
log = setup()
log.error('testing ...')
log.debug('something happened')
log.critical("it's the end of the world as we know it!")
Hope this helps.
--
Steven
More information about the Python-list
mailing list