Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #33137 > unrolled thread
| Started by | tinnews@isbd.co.uk |
|---|---|
| First post | 2012-11-11 17:42 +0000 |
| Last post | 2012-11-12 11:36 +0000 |
| Articles | 5 — 3 participants |
Back to article view | Back to comp.lang.python
logging, can one get it to email messages over a certain level? tinnews@isbd.co.uk - 2012-11-11 17:42 +0000
Re: logging, can one get it to email messages over a certain level? Steve Howell <showell30@yahoo.com> - 2012-11-11 09:56 -0800
Re: logging, can one get it to email messages over a certain level? tinnews@isbd.co.uk - 2012-11-12 09:44 +0000
Re: logging, can one get it to email messages over a certain level? Peter Otten <__peter__@web.de> - 2012-11-12 11:54 +0100
Re: logging, can one get it to email messages over a certain level? tinnews@isbd.co.uk - 2012-11-12 11:36 +0000
| From | tinnews@isbd.co.uk |
|---|---|
| Date | 2012-11-11 17:42 +0000 |
| Subject | logging, can one get it to email messages over a certain level? |
| Message-ID | <ql45n9-lli.ln1@chris.zbmc.eu> |
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
[toc] | [next] | [standalone]
| From | Steve Howell <showell30@yahoo.com> |
|---|---|
| Date | 2012-11-11 09:56 -0800 |
| Message-ID | <db43fde1-8edc-4839-a3b3-a742fffcfb22@me7g2000pbb.googlegroups.com> |
| In reply to | #33137 |
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.
[toc] | [prev] | [next] | [standalone]
| From | tinnews@isbd.co.uk |
|---|---|
| Date | 2012-11-12 09:44 +0000 |
| Message-ID | <k0t6n9-m6r.ln1@chris.zbmc.eu> |
| In reply to | #33138 |
Steve Howell <showell30@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.
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
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2012-11-12 11:54 +0100 |
| Message-ID | <mailman.3578.1352717700.27098.python-list@python.org> |
| In reply to | #33170 |
tinnews@isbd.co.uk wrote:
> Steve Howell <showell30@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",
"navigator@example.com",
"captain.nemo@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")
[toc] | [prev] | [next] | [standalone]
| From | tinnews@isbd.co.uk |
|---|---|
| Date | 2012-11-12 11:36 +0000 |
| Message-ID | <fj37n9-bjs.ln1@chris.zbmc.eu> |
| In reply to | #33173 |
Peter Otten <__peter__@web.de> wrote:
> tinnews@isbd.co.uk wrote:
>
> > Steve Howell <showell30@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",
> "navigator@example.com",
> "captain.nemo@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")
>
Thank you.
--
Chris Green
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web