Prepend to logging message

Joan Miller peloko45 at gmail.com
Sun Jan 10 08:49:35 EST 2010


On 10 ene, 13:10, Joan Miller <pelok... at gmail.com> wrote:
> On 10 ene, 12:36, Peter Otten <__pete... at web.de> wrote:
>
>
>
> > Joan Miller wrote:
> > > On 10 ene, 10:26, Peter Otten <__pete... at web.de> wrote:
> > >> Joan Miller wrote:
> > >> > How to prepend anything to a logging message? Is possible to do it
> > >> > from the dictionary object (ExtraLog) or is there is that override
> > >> > process() [1]?
>
> > >> > ------------------
> > >> > class ExtraLog(object):
>
> > >> > def __getitem__(self, name):
> > >> > if name == 'foo':
> > >> > result = 'testing'
> > >> > return result
>
> > >> > def __iter__(self):
> > >> > keys = ['foo',]
> > >> > keys.extend(self.__dict__.keys())
> > >> > return iter(keys)
>
> > >> format = "foo=%(foo)s " + logging.BASIC_FORMAT
> > >> logging.basicConfig(format=format)
> > >> logger = logging.LoggerAdapter(logging.getLogger('foo'), ExtraLog())
> > >> logger.error("yadda")
>
> > >> Is that what you want?
>
> > >> Peter
>
> > > I want that a message can be modified before of being logged. i.e. for
> > > "yadda" I would that were preppend 2 spaces. (And I want not manage
> > > that in the format to manage the indentation of all text)
>
> > Following Ishwor's advice:
>
> > import logging
>
> > class MyLoggerAdapter(logging.LoggerAdapter):
> >     def process(self, msg, kwargs):
> >         return "message->" + msg.upper(), kwargs
>
> > logging.basicConfig()
> > logger = MyLoggerAdapter(logging.getLogger('foo'), {})
> > logger.error("yadda")
>
> > Peter
>
> Thanks!
>
> I had to see the code to override it correctly [1]
>
> --------------------
> class LoggerAdapter_(logging.LoggerAdapter):
>
>     def __init__(self, logger, extra):
>         self.logger = logger
>         self.extra = extra
>
>     def process(self, msg, kwargs):
>         kwargs["extra"] = self.extra
>         return "message->" + msg.upper(), kwargs
> --------------------
>
> [1]http://bitbucket.org/mirror/python-trunk/src/tip/Lib/logging/__init__...

Sorry! It isn't necessary to copy __init__().



More information about the Python-list mailing list