Making a logging handler that produces context.

Discussion in 'Python' started by Antoon Pardon, Jan 14, 2013.

  1. 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
     
    Antoon Pardon, Jan 14, 2013
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VS_NET_DEV
    Replies:
    2
    Views:
    4,018
    jenny
    May 25, 2004
  2. =?Utf-8?B?U3VuU21pbGU=?=
    Replies:
    0
    Views:
    831
    =?Utf-8?B?U3VuU21pbGU=?=
    Jan 10, 2006
  3. Flip Rayner
    Replies:
    1
    Views:
    979
    bruce barker
    Jan 23, 2007
  4. asd
    Replies:
    1
    Views:
    613
    www.pulpjava.com
    Nov 9, 2006
  5. Jani Tiainen
    Replies:
    3
    Views:
    1,183
    Jani Tiainen
    Aug 27, 2007
  6. Gal Aviel
    Replies:
    1
    Views:
    586
    Gerard Flanagan
    Mar 4, 2008
  7. Bo Lindbergh

    List context versus list context

    Bo Lindbergh, Jun 21, 2006, in forum: Perl Misc
    Replies:
    12
    Views:
    413
    Charles DeRykus
    Jun 28, 2006
  8. Florian Kaufmann

    Why isn't void context a 'real' context

    Florian Kaufmann, Dec 11, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    158
    Tad J McClellan
    Dec 13, 2007
Loading...