An easy and flexible way is to format not only the email content, but also the email subject. This requires a subclassification of logging.handlers.SMTPHandler
.
Thus, if there are any references to variables in the theme you have configured, it will expand on demand. Here is an implementation, including test code:
import logging, logging.handlers class TitledSMTPHandler(logging.handlers.SMTPHandler): def getSubject(self, record): formatter = logging.Formatter(fmt=self.subject) return formatter.format(record)
Replace the SMTP configuration with your own, run the code, and you should receive an email containing the error message (also in the body of the message).
If you define a handler in the logging configuration file, be sure to avoid parameter references. for example, %(asctime)s
should become %%(asctime)s
to prevent premature interpolation of configparser - however, the %%
escape is not processed before python 3.1.
Please, if you ask one question at a time, you can help other googlers. You should start two topics, one called "Is there a way to put an exception name in the subject line?" and another - is there a way to prevent the sending of the same errors? "
I will answer only your first question to focus on one topic. You may have thought that two questions could be dependent on each other, so they should be asked together while they just come to your mind together. I think it’s still possible to suggest that you change the name of your question to emphasize one problem, instead of saying “advanced material”.
I would also suggest visiting comp.lang.python if the question is an open topic and / or cannot be clearly defined (for example, “Do I need advanced materials that everyone else uses?”)