example of logging w/ user-defined keywords?

C

Chris Curvey

Several things that I've read lead me to think this is possible, but I
can't figure out how to do it. I have some information (a "job
number") that I would like logged on every log message, just like the
time or the severity.

I saw some mail threads that suggested that there was an easy way to do
this, but I havent' found any examples. Is there a simple way to do
this, or do I need to write my own logger subclass?
 
P

Peter Hansen

Chris said:
Several things that I've read lead me to think this is possible, but I
can't figure out how to do it. I have some information (a "job
number") that I would like logged on every log message, just like the
time or the severity.

I saw some mail threads that suggested that there was an easy way to do
this, but I havent' found any examples. Is there a simple way to do
this, or do I need to write my own logger subclass?

class MyClass:
def __init__(self, name, job):
self.logger = logging.getLogger(name)
self.job = job


def log(self, msg):
self.logger.debug('#%d: %s' % (self.job, msg)


Then just call self.log('foo') whenever you want to log something.

If this isn't sufficient please describe what requirements you have that
it doesn't meet. (Subclassing Logger is certainly a simple way to do it
though... in that case you would just pass the job number to the
subclass when you construct it and it could do something like that above.

-Peter
 
V

Vinay Sajip

Chris said:
Several things that I've read lead me to think this is possible, but I
can't figure out how to do it. I have some information (a "job
number") that I would like logged on every log message, just like the
time or the severity.

I saw some mail threads that suggested that there was an easy way to do
this, but I havent' found any examples. Is there a simple way to do
this, or do I need to write my own logger subclass?

The latest version in Subversion has an 'extra' keyword argument which
allows almost arbitrary items to be added to a LogRecord. See the
"in-development" docs at

http://docs.python.org/dev/lib/node365.html

and search for "extra".

Best regards,

Vinay Sajip
 

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
474,292
Messages
2,571,494
Members
48,183
Latest member
GarfieldBa

Latest Threads

Top