473,390 Members | 817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

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 3282
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nermin Brgulja | last post by:
Hi, I am using jdk1.4 Logger with a FileHandler for logging in a web application, and have two questions regarding logging: The first question is: I've created an Logger and mapped it to an...
0
by: Laszlo | last post by:
Hi I need a GL::Logger wich implements Class::Singleton to be a singleton and Log::Log4perl to can log amy message into a file. Here is the module source:...
1
by: sachin | last post by:
hello everyone, i am trying to convert my XSL-FO to PDF file by using fop-0.20.5 by importing the following classes: import java.io.File; import java.io.IOException; import...
2
by: Robert | last post by:
Hi all I have been trying to set up a framework with logging implemented using the built in Python logging module. For better or worse, I have decided to base the logger names on the module...
10
by: Gary Jefferson | last post by:
Suppose I have 3 modules that belong to a project, 'A', 'A.a' and 'B', and each module has its own logger, created with: module1logger = logging.getLogger('project.A') and module2logger =...
5
by: CedricCicada | last post by:
Greetings! I want to write messages into the Windows event log. I found sevicemanager, but the source is always "Python Service", and I'd like to be a bit more descriptive. Poking around on...
4
by: garyjefferson123 | last post by:
Suppose I have some sort of context variable that I want to appear in log messages. E.g.: logger = logging.getLogger("somelogger") class SomeOp: def __init__(self, ctx): self.ctx = ctx def...
1
by: nagk9 | last post by:
Hi , I am using log4j in my application. I have the problem with log4j.properties file . It will automatically appends another logger file from the current logger which i am using for the...
1
by: Matimus | last post by:
Yes but in the other hand :http://docs.python.org/library/logging.html#logger-objects That is part of the power of the logging module. If you ask for a logger of the same name in two different...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.