On 26 dic, 04:34, Steven D'Aprano <st...@remove-this- cybersource.com.au> wrote: > On Fri, 25 Dec 2009 12:07:20 -0800, Joan Miller wrote: > > On 25 dic, 13:24, Steven D'Aprano <st...@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
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()) -- http://mail.python.org/mailman/listinfo/python-list