Prepend to logging message

J

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

Ishwor Gurung

Joan,

2010/1/10 Joan Miller said:
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
 
J

Joan Miller

Joan,

2010/1/10 Joan Miller <[email protected]>:


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]?
   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() ?
 
P

Peter Otten

Joan said:
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
 
J

Joan Miller

Joan said:
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]?
    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)
 
P

Peter Otten

Joan said:
Joan said:
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]?
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
 
J

Joan Miller

Joan said:
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
 
J

Joan Miller

Joan said:
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")

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

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

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top