Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: logging, can one get it to email messages over a certain level?

Reply
Thread Tools

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

 
 
Steve Howell
Guest
Posts: n/a
 
      11-11-2012
On Nov 11, 9:48*am, (E-Mail Removed) 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/loggi...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.

 
Reply With Quote
 
 
 
 
tinnews@isbd.co.uk
Guest
Posts: n/a
 
      11-12-2012
Steve Howell <(E-Mail Removed)> wrote:
> On Nov 11, 9:48*am, (E-Mail Removed) 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/loggi...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.

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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      11-12-2012
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Steve Howell <(E-Mail Removed)> wrote:
>> On Nov 11, 9:48 am, (E-Mail Removed) 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/loggi...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",
"(E-Mail Removed)",
"(E-Mail Removed)",
"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")


 
Reply With Quote
 
tinnews@isbd.co.uk
Guest
Posts: n/a
 
      11-12-2012
Peter Otten <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
>
> > Steve Howell <(E-Mail Removed)> wrote:
> >> On Nov 11, 9:48 am, (E-Mail Removed) 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/loggi...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",
> "(E-Mail Removed)",
> "(E-Mail Removed)",
> "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")
>

Thank you.

--
Chris Green
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows Vista cannot obtain an IP address from certain routers or from certain non-Microsoft DHCP Brian W Wireless Networking 7 01-31-2010 03:46 AM
VOIP over VPN over TCP over WAP over 3G Theo Markettos UK VOIP 2 02-14-2008 03:27 PM
Finding one certain line in a file is easy but how to look forheadlines and something just under this certain headline? kazaam Ruby 3 08-26-2007 03:34 PM
sip_reg_timeout: messages over and over again alexd UK VOIP 1 10-10-2005 09:24 PM
Expanding certain path to certain node in a JTree arun.hallan@gmail.com Java 0 01-08-2005 08:26 PM



Advertisments