• If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Stop wasting time looking for files and revisions. Connect your Gmail, DriveDropbox, and Slack accounts and in less than 2 minutes, Dokkio will automatically organize all your file attachments. Learn more and claim your free account.


Example of Modifying the Logging Module

Page history last edited by Elizabeth Leddy 10 years, 11 months ago

NOTE: This is just an example of what you can do with the logging module. For zope, just initializing this code somewhere in the system (i.e. __init__.py) will make it apply to every call to logging.*




We want to have a little bit of logging customization here

such that all bad errors get emailed to someone


import logging

import logging.handlers

from logging.handlers import MemoryHandler


                    format='%(asctime)s %(levelname)s @%(pathname)s/%(filename)s %(module)s %(lineno)d  : %(message)s ',




# who gets stuck reading these

SYSTEM_WHORE = ['support@mysite.com']

# who are all these emails from?

BIG_WHINER = 'errors@mysite.com'

# the mail server that will be sending all this mail


def patchLogger():

    bufferHandler = MyLoggingHandler(capacity=25, flushLevel=logging.CRITICAL)

    logger = logging.getLogger()


    # check to see if this logger has this handler

    # since we don't want to accidentally have multiple smtp handlers

    # and get multiple copies of the same meassages

    alreadyPatched = False

    for currentHandler in logger.handlers:

        if isinstance(currentHandler, MyLoggingHandler):

            alreadyPatched = True



    if not alreadyPatched:



# batch up email requests and send as one

class MyLoggingHandler(MemoryHandler):

    def __init__(self, **kwargs):

        MemoryHandler.__init__(self, **kwargs)

        self.setFormatter(logging.Formatter('%(asctime)s %(levelname)s @%(pathname)s/%(filename)s %(module)s %(lineno)d  : %(message)s'))

        mailHandler = logging.handlers.SMTPHandler( TOOL_OF_DELIVERER_OF_BAD_NEWS,



                                            'ERROR logged in the network: Please Investigate')




    def flush(self):


        Make one message to send instead of gagilliogns


        sendMe = ""

        for record in self.buffer:

            sendMe += "\n-----------------------------------\n"

            sendMe += self.format(record)

            sendMe += "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"


        self.buffer = []

        if sendMe: # there are utilities thtat could potentially send empty error messages

            newRecord = logging.LogRecord('root', logging.ERROR, '', '', sendMe, None, None)

            if self.target:



class LevelFilter(logging.Filter):

    def __init__(self, level):

        self.level = level

    def filter(self, record):

        return self.level == record.levelno


Comments (0)

You don't have permission to comment on this page.