Prepend to logging message

Discussion in 'Python' started by Joan Miller, Jan 10, 2010.

  1. Joan Miller

    Joan Miller Guest

    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)

    logger = logging.LoggerAdapter(logging.getLogger('foo'), ExtraLog())
    ------------------


    [1] http://docs.python.org/library/logging.html#logging.LoggerAdapter
     
    Joan Miller, Jan 10, 2010
    #1
    1. Advertising

  2. Joan,

    2010/1/10 Joan Miller <>:
    > 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)
    >
    > logger = logging.LoggerAdapter(logging.getLogger('foo'), ExtraLog())
    > ------------------


    Yep. Just subclass LoggerAdapter and override process(..)
    Read this: http://docs.python.org/library/logging.html#adding-contextual-information-to-your-logging-output
    --
    Regards
    Ishwor Gurung
    Key id:0xa98db35e
    Key fingerprint:FBEF 0D69 6DE1 C72B A5A8 35FE 5A9B F3BB 4E5E 17B5
     
    Ishwor Gurung, Jan 10, 2010
    #2
    1. Advertising

  3. Joan Miller

    Joan Miller Guest

    On 10 ene, 03:27, Ishwor Gurung <> wrote:
    > Joan,
    >
    > 2010/1/10 Joan Miller <>:
    >
    >
    >
    > > 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)

    >
    > > logger = logging.LoggerAdapter(logging.getLogger('foo'), ExtraLog())
    > > ------------------

    >
    > Yep. Just subclass LoggerAdapter and override process(..)
    > Read this:http://docs.python.org/library/logging.html#adding-contextual-informa...
    > --
    > Regards
    > Ishwor Gurung
    > Key id:0xa98db35e
    > Key fingerprint:FBEF 0D69 6DE1 C72B A5A8  35FE 5A9B F3BB 4E5E 17B5


    Any example to override process() ?
     
    Joan Miller, Jan 10, 2010
    #3
  4. Joan Miller

    Peter Otten Guest

    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
     
    Peter Otten, Jan 10, 2010
    #4
  5. Joan Miller

    Joan Miller Guest

    On 10 ene, 10:26, Peter Otten <> 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)
     
    Joan Miller, Jan 10, 2010
    #5
  6. Joan Miller

    Peter Otten Guest

    Joan Miller wrote:

    > On 10 ene, 10:26, Peter Otten <> 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
     
    Peter Otten, Jan 10, 2010
    #6
  7. Joan Miller

    Joan Miller Guest

    On 10 ene, 12:36, Peter Otten <> wrote:
    > Joan Miller wrote:
    > > On 10 ene, 10:26, Peter Otten <> 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__.py#cl-1264
     
    Joan Miller, Jan 10, 2010
    #7
  8. Joan Miller

    Joan Miller Guest

    On 10 ene, 13:10, Joan Miller <> wrote:
    > On 10 ene, 12:36, Peter Otten <> wrote:
    >
    >
    >
    > > Joan Miller wrote:
    > > > On 10 ene, 10:26, Peter Otten <> 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__().
     
    Joan Miller, Jan 10, 2010
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    2,199
  2. flamesrock

    how to prepend string to a string?

    flamesrock, May 31, 2005, in forum: Python
    Replies:
    6
    Views:
    16,163
    newcoder
    May 31, 2005
  3. Replies:
    37
    Views:
    2,064
    Flash Gordon
    Jan 9, 2006
  4. dspfun

    Prepend name to function names?

    dspfun, Jan 29, 2008, in forum: C Programming
    Replies:
    8
    Views:
    861
    dspfun
    Jan 29, 2008
  5. bdb112
    Replies:
    6
    Views:
    4,266
    Robert Kern
    Jul 25, 2009
Loading...

Share This Page