Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

logging, can one get it to email messages over a certain level?

53 views
Skip to first unread message

tin...@isbd.co.uk

unread,
Nov 11, 2012, 12:42:50 PM11/11/12
to
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

Steve Howell

unread,
Nov 11, 2012, 12:56:46 PM11/11/12
to
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.

tin...@isbd.co.uk

unread,
Nov 12, 2012, 4:44:20 AM11/12/12
to
Thank you, but yes I agree it's not terribly informative is it.

However I think I understand what I need to do. I currently have a
function to set up my logging:-

def initLog(name):
log = logging.getLogger(name)
log.setLevel(logging.DEBUG)
f = logging.handlers.RotatingFileHandler("/home/chris/tmp/mail.log", 'a', 1000000, 4)
f.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
f.setFormatter(formatter)
log.addHandler(f)
return log

As I understand it I just add another line like the 'f =' line but
using the SMTPHandler and then set an appropriate level for that
handler (and formatting).


--
Chris Green

Peter Otten

unread,
Nov 12, 2012, 5:54:49 AM11/12/12
to pytho...@python.org
tin...@isbd.co.uk wrote:

> Steve Howell <show...@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",
"navi...@example.com",
"captai...@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")


tin...@isbd.co.uk

unread,
Nov 12, 2012, 6:36:47 AM11/12/12
to
Thank you.

--
Chris Green
0 new messages