Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Making a logging handler that produces context.

Reply
Thread Tools

Making a logging handler that produces context.

 
 
Antoon Pardon
Guest
Posts: n/a
 
      01-14-2013

I have some in house code for which I am considering replacing the
logging code
with something that uses the logging module.

The code is typically used as a cron job with everything higher than
info logged to
a file and everything higher than warning logged to stderr.

However there is one thing the in-house log code does, that seems
difficult to do
with the logging module, provide some context. The in-house handlers
give the
possibilty to specify the number of lines of context the hander can provide.

So the following code:

Logger(fn = "file.log", level = info)
Logger(fl = stderr, level = warning, context = 2)

log(INFO, "line 1")
log(INFO, "line 2")
log(INFO, "line 3")
log(INFO, "line 4")
log(WARNING, "line 5")

Will sent something like the following lines to stderr:

INFO: line 3
INFO: line 4
WARNING: line 5

I tried the code below, but that produced the same
as the ordinary StreamHandler.

class ContextStreamHandler (StreamHandler):

def __init__(self, stream=None, context = 5):
self.recqueue = deque([], context)
StreamHandler.__init__(self, stream)
#__init__

def handle(self, record):
print("CONTEXT HANDLER")
rv = self.filter(record)
if rv:
self.acquire()
try:
for rec in self.recqueue:
self.emit(rec)
self.recqueue.clear()
self.emit(record)
finally:
self.release
else:
self.recqueue.append(record)
return rv
#handle
#ContextStreamHandler


 
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
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Re: Making a logging handler that produces context. Antoon Pardon Python 0 01-14-2013 02:26 PM
Re: Making a logging handler that produces context. Peter Otten Python 0 01-14-2013 12:38 PM
Removing default logging handler (causes duplicate logging) Gal Aviel Python 1 03-04-2008 03:35 PM
OutputStream from a URLConnection produces an OutOfMemory OutputStream from a URLConnection produces an OutOfMemory WinstonSmith_101@hotmail.com Java 2 10-25-2006 04:45 PM



Advertisments