Is there a way to configure SMTPHandler in Python to create more advanced materials?

I use the standard SMTPHandler logger to catch my exceptions in Python. Is there a way to put the name of the exception in the subject line? This would be much better than with a static subject, because Gmail (and not just Gmail) can group conversations according to the topic, and therefore can group it according to the type of error.

For example, if 50 completely identical errors occur + 1 different, I would see two conversations in my mailbox instead of 1 group consisting of 51 emails, where I can very easily miss one separate one.

Also, is there a way to prevent sending the same errors? E. g. somehow define my own function deciding whether to send email. The function will receive some basic information in the parameters so that it can solve (for example, the cache and see if such a problem has already been sent).

I looked through the documents, but I can not find anything like it. It seems very simple. If SMTPHandler cannot do this, what would be a better and simpler alternative? Any neat libraries?

Thanks!

+6
source share
3 answers

You just need to create your own subclass of SMTPHandler .

For your first request: you just need to override the getSubject(record) method. As for what you can put in the topic, see help(Formatter) after importing the Formatter from the magazine.

For your second request: you must override the emit(record) method. Inspect the record and make your own decision about sending it. If so, just call the super(SMTPHandler,self).emit(record) method.

 class MySMTPHandler(SMTPHandler): def getSubject(self, record): return "My Error Format from the record dict" def emit(self, record): #check for record in self.already_send or something if sendit: super(MySMTPHandler,self).emit(record) 
+7
source

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) # below is to test logging.getLogger().addHandler(TitledSMTPHandler( ('your.smtp.server',587), ' email@from.address ', ' email@to.address ', '%(asctime)s Error: %(message)s', ('SMTP login', 'SMTP password'), () )) logging.error("TestError") 

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?”)

+1
source

You can inherit the SMTPHandler class and improve or replace its functionality, for example

 class A(object): def __init__(self): self.email = " jjj@jjj.com " def send_error(self, error): send_mail(self.email, error) class B(A): def __init__(self): A.__init__(self) def send_error(self, error): if not error.sent: A.send_mail(self, error) else: #do nothing pass 
0
source

Source: https://habr.com/ru/post/908190/


All Articles