Discussion:
Extra fields for logging
Joan Miller
2009-12-24 13:06:48 UTC
Permalink
I'm trying to add some extra fields to logging, I'm following this
information [1] but it faills in my case.

# module logger.py
--------------------
import logging

class ExtraInfo(object):
def __getitem__(self, name):
if name == 'host':
result = 'foo'
def __iter__(self):
keys = ['host',]
keys.extend(self.__dict__.keys())
return keys.__iter__()

def setup(filename='/tmp/foo.log'):
log = logging.LoggerAdapter(logging.getLogger('foo'), ExtraInfo())
#log = logging.LoggerAdapter(logging.getLogger('foo'), {'host':
'bar'})

logging.basicConfig(
level=logging.DEBUG,
format=(
"Date-Time: %(asctime)s\n"
"Host: %(host)s\n"
"%(levelname)s:\n"
"%(message)s"),
# %f => microseconds is not showed, bug?
datefmt="%Y-%m-%dT%H:%M:%S%z",
filename=filename,
filemode='w')
--------------------

# module another.py
--------------------
import logger

logger.setup()
logging.getLogger('foo')
logging.error('testing ...')
--------------------

I get => KeyError: 'host'


-----------
System: Ubuntu 9.10 - Python 2.6.4
-----------

[1] http://docs.python.org/library/logging.html#adding-contextual-information-to-your-logging-output
Steven D'Aprano
2009-12-25 13:24:55 UTC
Permalink
Post by Joan Miller
I'm trying to add some extra fields to logging, I'm following this
information [1] but it faills in my case.
[...]
Post by Joan Miller
I get => KeyError: 'host'
Please post the entire traceback you get.
--
Steven
Joan Miller
2009-12-25 20:07:20 UTC
Permalink
On 25 dic, 13:24, Steven D'Aprano <st... at REMOVE-THIS-
Post by Steven D'Aprano
Post by Joan Miller
I'm trying to add some extra fields to logging, I'm following this
information [1] but it faills in my case.
[...]
Post by Joan Miller
I get => KeyError: 'host'
Please post the entire traceback you get.
--
Steven
On 25 dic, 13:24, Steven D'Aprano <st... at REMOVE-THIS-
Post by Steven D'Aprano
Post by Joan Miller
I'm trying to add some extra fields to logging, I'm following this
information [1] but it faills in my case.
[...]
Post by Joan Miller
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'
Steven D'Aprano
2009-12-26 04:34:32 UTC
Permalink
Post by Joan Miller
On 25 dic, 13:24, Steven D'Aprano <st... at REMOVE-THIS-
Post by Steven D'Aprano
Post by Joan Miller
I'm trying to add some extra fields to logging, I'm following this
information [1] but it faills in my case.
[...]
Post by Joan Miller
I get => KeyError: 'host'
Please post the entire traceback you get.
--
Steven
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
Joan Miller
2009-12-26 10:28:12 UTC
Permalink
On 26 dic, 04:34, Steven D'Aprano <st... at REMOVE-THIS-
Post by Steven D'Aprano
Post by Joan Miller
On 25 dic, 13:24, Steven D'Aprano <st... at REMOVE-THIS-
Post by Steven D'Aprano
Post by Joan Miller
I'm trying to add some extra fields to logging, I'm following this
information [1] but it faills in my case.
[...]
Post by Joan Miller
I get => KeyError: 'host'
Please post the entire traceback you get.
--
Steven
? 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.
import logging
? ? ? ? ? ? result = 'foo'
? ? ? ? return result
? ? ? ? keys = ['host',]
? ? ? ? keys.extend(self.__dict__.keys())
? ? ? ? return iter(keys) ?# better than keys.__iter__
? ? log = logging.LoggerAdapter(logging.getLogger('foo'), ExtraInfo())
? ? logging.basicConfig(
? ? ? ? level=logging.DEBUG,
? ? ? ? format="""Date-Time: %(asctime)s
Host: %(host)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
Thanks Steven! You give me the idea.

* In the setup(), is not necessary to use:

log = logging.LoggerAdapter(logging.getLogger('foo'), ExtraInfo()

neither return a value:

return log

* The problem is that I'm using the logger at module-level so I had in
each module:

logger = logging.getLogger(__name__)

but there is to use:

logger = logging.LoggerAdapter(
logging.getLogger(__name__), log.ExtraInfo())

Loading...