Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'context': 0.05; 'finally:': 0.05; 'received:134': 0.05; 'try:': 0.07; 'does,': 0.09; 'handlers': 0.09; 'def': 0.10; 'skip:# 20': 0.13; '2")': 0.16; 'stderr.': 0.16; 'stderr:': 0.16; 'module,': 0.17; 'replacing': 0.17; 'specify': 0.17; 'context.': 0.22; 'seems': 0.23; 'tried': 0.25; 'header:User-Agent:1': 0.26; 'logging': 0.27; 'module.': 0.27; 'lines': 0.28; 'in-house': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'code': 0.31; 'file': 0.32; 'info': 0.32; 'to:addr:python-list': 0.33; 'code:': 0.33; 'produced': 0.33; 'typically': 0.33; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'level': 0.37; 'uses': 0.37; 'some': 0.38; 'to:addr:python.org': 0.39; 'below,': 0.60; 'provide': 0.62; 'info)': 0.84; 'subject:Making': 0.84 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AkIRAAPl81CGuA9G/2dsb2JhbABEiQi1CoQQPRYYAwIBAgFYCAKIFZU4l1OHCo4FgykDlguFWIpxgnY Date: Mon, 14 Jan 2013 12:03:41 +0100 From: Antoon Pardon User-Agent: Mozilla/5.0 (X11; Linux i686; rv:10.0.11) Gecko/20121123 Icedove/10.0.11 MIME-Version: 1.0 To: python-list@python.org Subject: Making a logging handler that produces context. Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 61 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1358161431 news.xs4all.nl 6877 [2001:888:2000:d::a6]:37761 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:36778 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