logging: my experience thus far. Be Careful With SMTPHandler!!

Pete Jereb petejereb at yahoo.com
Tue Oct 7 18:55:30 EDT 2003


Ok, I've managed to get the logging package to work, sort of.  I'm
writing a text parser, where data entry clerks can edit large,
unwieldy and unforgiving textfiles that are VERY SENSITIVE as to where
things are located, ie the decimal place MUST BE 15 spaces after the
last word on the line.  So I'm writing a backend, and a Microsoft
Access guru is writing the gui.  Once they edit the files by field,
the corrected files are sent to a printer program.  Now what I'm
trying to do is separate out the blocks of text that are still funky,
by scanning for data entry errors.  This errorlist will then be
emailed to the typist and they can see what they did wrong and try
again.


So first,

import logging
import logging.handlers

next instantiate a logger:

logger = logging.getLogger('mylog')

I want to store the log in memory before I email it to the typist, and
for now I want it to be able to hold maxint number of errorlistings:

mem_hdlr = logging.handlers.MemoryHandler(sys.maxint)

Assign mem_hdlr to logger:

logger.addHandler(mem_hdlr)

And sprinkle logger messages throughout the code:

logger.info('subroutine x called')

I want to send all errors in the mem_hdlr to the email_hdlr, which
should email the errors to me, my assistant, and the typist, so we can
all see what's going on:

email_hdlr = logging.handlers.SMTPHandler('xxx.xxx.xxx.xx',
'petejereb at yahoo.com', to_list, 'autogen errorlog')

xxx.xxx.xxx.xx is the address of the my SMTP server.  followed by
from, to_list is a list of email addresses to send the errors to, and
last is the subject.

So these last few lines of the program,

mem_hdlr.setTarget(email_hdlr)
mem_hdlr.close()

supposedly, to the best of my reading comprehension, were to dump
mem_hdlr's log into email_hdlr, which would then email the log to the
listed recipients.

Which is what happens except for one small detail.  The errorlog is
emailed to the recipients.

One
Line
At
A
Time

Which means I have 2,000 new emails as I type this, and the assistant
and typist are going to be mighty surprised when they come in
tomorrow!  I'm just glad I took my supervisor off the testlist :)

I'm getting really confused by the logging package.  If I can't figure
out how to use .logging to do what I need it to do I'll just use
smtplib and write the errors to a string, and email the string.  Oh
well this is how I spent my day, hopefully this will be helpful to
someone.




More information about the Python-list mailing list