468,735 Members | 2,189 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,735 developers. It's quick & easy.

examples of logger using smtp

I am hoping to find some simple examples of how to create a logger
instance using smtphandler. I don't want to create a separate ini
file. I just want to sent the smtphost, from, to right in the code
when I instantiate the logger. I can't seem to find simple code on how
to do this. Any pointers ?
Jan 17 '08 #1
2 3101
DwBear75 <dw******@gmail.comwrites:
I am hoping to find some simple examples of how to create a logger
instance using smtphandler. I don't want to create a separate ini
file. I just want to sent the smtphost, from, to right in the code
when I instantiate the logger. I can't seem to find simple code on how
to do this. Any pointers ?
If you need to use smtp authentication there is a small problem
with `SMTPHandler`. Actually, you need to subclass `SMTPHandler`
and override `emit` method, e.g.:

from logging import getLogger, Formatter, DEBUG
from logging.handlers import SMTPHandler

class SMTPAuthHandler(SMTPHandler):
def __init__(self, mailhost, fromaddr, toaddrs, subject,
user=None, password=None):
SMTPHandler.__init__(self, mailhost, fromaddr, toaddrs, subject)
self.user = user
self.password= password

def emit(self, record):
import smtplib
from email.Utils import formatdate
smtp = smtplib.SMTP(self.mailhost, smtplib.SMTP_PORT)
if self.user and self.password:
smtp.login(self.user, self.password)
msg = self.format(record)
msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\n\r\n%s" % (
self.fromaddr,
','.join(self.toaddrs),
self.getSubject(record),
formatdate(), msg)
smtp.sendmail(self.fromaddr, self.toaddrs, msg)
smtp.quit()
def smtp_logger(level, mailhost, fromaddr, toaddr, subject,
user=None, password=None):
logger = getLogger('AppName')
logger.setLevel(level)
hdlr = SMTPAuthHandler(mailhost, fromaddr, toaddr, subject,
user, password)
fmt = Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
'%Y-%m-%d %H:%M:%S')
hdlr.setFormatter(fmt)
logger.addHandler(hdlr)
hdlr.setLevel(level)
return logger
logger = smtp_logger(DEBUG, 'mailhost', 'fromaddr', 'toaddr',
'DEBUG: AppName', 'user', 'password')
logger.debug('some message')
HTH,
Rob
Jan 17 '08 #2
On Jan 17, 7:46 pm, Rob Wolfe <r...@smsnet.plwrote:
>
If you need to use smtp authentication there is a small problem
with `SMTPHandler`. Actually, you need to subclass `SMTPHandler`
and override `emit` method, e.g.:

fromloggingimport getLogger, Formatter, DEBUG
fromlogging.handlers import SMTPHandler

class SMTPAuthHandler(SMTPHandler):
def __init__(self, mailhost, fromaddr, toaddrs, subject,
user=None, password=None):
SMTPHandler.__init__(self, mailhost, fromaddr, toaddrs, subject)
self.user = user
self.password= password
[snip]

Although the code in the last release does not support authentication
(hence needing the above code), the code currently in SVN does support
it. Instead of separate user and password arguments, a new
"credentials" optional argument ((user, password) tuple, defaulting to
None) has been added. So if you are using Rob Wolfe's approach, it may
be easier for you to use "credentials" because your code will not need
to change when Python 2.6 appears.

Best regards,

Vinay Sajip
Jan 18 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Nermin Brgulja | last post: by
reply views Thread by Laszlo | last post: by
2 posts views Thread by Robert | last post: by
10 posts views Thread by Gary Jefferson | last post: by
5 posts views Thread by CedricCicada | last post: by
4 posts views Thread by garyjefferson123 | last post: by
1 post views Thread by Matimus | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.