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

Discussion in 'Python' started by Steve Howell, Nov 11, 2012.

  1. Steve Howell

    Steve Howell Guest

    On Nov 11, 9:48 am, 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.
     
    Steve Howell, Nov 11, 2012
    #1
    1. Advertising

  2. Steve Howell

    Guest

    Steve Howell <> wrote:
    > On Nov 11, 9:48 am, 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
     
    , Nov 12, 2012
    #2
    1. Advertising

  3. Steve Howell

    Peter Otten Guest

    wrote:

    > Steve Howell <> wrote:
    >> On Nov 11, 9:48 am, 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",
    "",
    "",
    "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")
     
    Peter Otten, Nov 12, 2012
    #3
  4. Steve Howell

    Guest

    Peter Otten <> wrote:
    > wrote:
    >
    > > Steve Howell <> wrote:
    > >> On Nov 11, 9:48 am, 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",
    > "",
    > "",
    > "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
     
    , Nov 12, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    661
  2. fuzzylollipop
    Replies:
    1
    Views:
    378
  3. pabbu
    Replies:
    8
    Views:
    732
    Marc Boyer
    Nov 7, 2005
  4. Replies:
    2
    Views:
    529
    bruce barker
    Mar 25, 2008
  5. SAN CAZIANO
    Replies:
    8
    Views:
    179
    Dr John Stockton
    Oct 15, 2004
Loading...

Share This Page