tinn...@isbd.co.uk wrote:

> Steve Howell <showel...@yahoo.com> wrote:
>> On Nov 11, 9:48 am, tinn...@isbd.co.uk wrote:
>> > I'm sure this must be possible but at the moment I can't see how to do
>> > it.
>> >
>> > I want to send an E-Mail when the logging module logs a message above
>> > a certain level (probably for ERROR and CRITICAL messages only).
>> >
>> > I.e. I want some sort of hook that will be called when these messages
>> > are logged (I can do the sendmail bit OK, I've got code that does
>> > that).
>> >
>> > --
>> > Chris Green
>> 
>> Scroll down to the Handlers section here:
>> 
>>    http://docs.python.org/2/howto/logging.html#logging-basic-tutorial
>> 
>> I'm not sure this explains perfectly what you're gonna need to do, but
>> I hope it gets you in the ballpark.
>> 
> Thank you, but yes I agree it's not terribly informative is it.

Here's a minimal example:

import logging
from logging.handlers import SMTPHandler

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)

    mail_handler = SMTPHandler(
        "smtp.example.com",
        "naviga...@example.com",
        "captain.n...@example.com",
        "fyi")
    mail_handler.setLevel(logging.CRITICAL)

    root = logging.getLogger()
    root.addHandler(mail_handler)

    # will print amessage
    root.warn("this is fishy") 

    # will print a message and send an email
    root.critical("giant squid sighted")


-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to