Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Prepend to logging message

Reply
Thread Tools

Prepend to logging message

 
 
Joan Miller
Guest
Posts: n/a
 
      01-10-2010
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/loggi....LoggerAdapter
 
Reply With Quote
 
 
 
 
Ishwor Gurung
Guest
Posts: n/a
 
      01-10-2010
Joan,

2010/1/10 Joan Miller <(E-Mail Removed)>:
> 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/loggi...logging-output
--
Regards
Ishwor Gurung
Key id:0xa98db35e
Key fingerprint:FBEF 0D69 6DE1 C72B A5A8 35FE 5A9B F3BB 4E5E 17B5
 
Reply With Quote
 
 
 
 
Joan Miller
Guest
Posts: n/a
 
      01-10-2010
On 10 ene, 03:27, Ishwor Gurung <(E-Mail Removed)> wrote:
> Joan,
>
> 2010/1/10 Joan Miller <(E-Mail Removed)>:
>
>
>
> > 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/loggi...extual-informa...
> --
> Regards
> Ishwor Gurung
> Key id:0xa98db35e
> Key fingerprint:FBEF 0D69 6DE1 C72B A5A8 *35FE 5A9B F3BB 4E5E 17B5


Any example to override process() ?
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      01-10-2010
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
 
Reply With Quote
 
Joan Miller
Guest
Posts: n/a
 
      01-10-2010
On 10 ene, 10:26, Peter Otten <(E-Mail Removed)> 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)
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      01-10-2010
Joan Miller wrote:

> On 10 ene, 10:26, Peter Otten <(E-Mail Removed)> 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
 
Reply With Quote
 
Joan Miller
Guest
Posts: n/a
 
      01-10-2010
On 10 ene, 12:36, Peter Otten <(E-Mail Removed)> wrote:
> Joan Miller wrote:
> > On 10 ene, 10:26, Peter Otten <(E-Mail Removed)> 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-t...t__.py#cl-1264
 
Reply With Quote
 
Joan Miller
Guest
Posts: n/a
 
      01-10-2010
On 10 ene, 13:10, Joan Miller <(E-Mail Removed)> wrote:
> On 10 ene, 12:36, Peter Otten <(E-Mail Removed)> wrote:
>
>
>
> > Joan Miller wrote:
> > > On 10 ene, 10:26, Peter Otten <(E-Mail Removed)> 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__().
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
AS Path Prepend Problem amyl@paxemail.com Cisco 5 10-12-2008 11:32 PM
Help -> Cisco BGP as-path prepend not working BG Cisco 0 01-03-2007 12:57 AM
Automatically prepend all stdout/stderr output with timestamp? nobody@nowhere.com C Programming 37 01-09-2006 11:40 PM
how to prepend string to a string? flamesrock Python 6 05-31-2005 07:51 PM
ant java task bootclasspath prepend vnssoftware@myway.com Java 0 02-02-2005 04:53 PM



Advertisments