Python logging: Retrieving the last log record from a handler

Discussion in 'Python' started by Frank Aune, Mar 3, 2008.

  1. Frank Aune

    Frank Aune Guest

    Hi,

    I'm using a log hierarchy in my application, and sometimes I find myself
    wanting to retrieve the latest log message written to the root logger.
    (Typical usage might be displaying the latest log message at all times in a
    GUI).

    The only way I've found how to solve this, is by adding a custom loghandler:

    ---
    class LastRecordHandler(logging.Handler):

    def __init__(self):
    logging.Handler.__init__(self)
    self.lastRecord = None

    def emit(self, record):
    self.lastRecord = record

    def getRecord(self):
    return self.lastRecord.getMessage()
    ---

    I will be fairly surprised if this functionality is not already built-in for
    the default logging handlers, but so far I've been unable to figure out how
    to pull it of without the custom loghandler above.

    The kind of functionality I'm looking for is something like:

    self.log = logging.getLogger('GUI')
    (...)
    lastRecord = self.log.getLastRecord()
    # Display lastRecord in GUI

    Is this possible in some way, or do I need to extend the default logging
    handlers in order to archieve this?

    Thanks,
    Frank
    Frank Aune, Mar 3, 2008
    #1
    1. Advertising

  2. Frank Aune

    Vinay Sajip Guest

    On Mar 3, 11:41 am, Frank Aune <> wrote:
    > I will be fairly surprised if this functionality is not already built-in for
    > the defaultlogginghandlers, but so far I've been unable to figure out how
    > to pull it of without the custom loghandler above.


    Prepare to be surprised ;-)

    Except for the MemoryHandler, which is designed specifically as a
    buffer for logging events, the handlers in the logging package do not
    store the last record they processed. (Neither do loggers, filters or
    formatters, all of which get to see records as they are processed.)

    Best regards,

    Vinay Sajip
    Vinay Sajip, Mar 4, 2008
    #2
    1. Advertising

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. =?Utf-8?B?SnVzdGlu?=

    Retrieving Record Key while creating the record.

    =?Utf-8?B?SnVzdGlu?=, Oct 4, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    630
    =?Utf-8?B?SnVzdGlu?=
    Oct 5, 2004
  2. Frank Aune
    Replies:
    4
    Views:
    1,186
    Vinay Sajip
    Oct 19, 2007
  3. Gal Aviel
    Replies:
    1
    Views:
    452
    Gerard Flanagan
    Mar 4, 2008
  4. Royan
    Replies:
    1
    Views:
    1,433
    Andrea Francia
    Apr 1, 2008
  5. Lowell Alleman
    Replies:
    3
    Views:
    541
    Vinay Sajip
    May 29, 2008
Loading...

Share This Page