Extra fields for logging

J

Joan Miller

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 ...')
 
S

Steven D'Aprano

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.
 
J

Joan Miller

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

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.

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'
 
S

Steven D'Aprano

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.

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.
 
J

Joan Miller

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.
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.

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())
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top