Making a logging handler that produces context.
antoon.pardon at rece.vub.ac.be
Mon Jan 14 12:03:41 CET 2013
I have some in house code for which I am considering replacing the
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
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)
def handle(self, record):
rv = self.filter(record)
for rec in self.recqueue:
More information about the Python-list