drjackild
drjackild

Reputation: 472

How to send error logs on email in Scrapy

I want to send emails with errors, if they occur while Scrapy is working. I've added email handler and connect it to spider.logger:

def __init__(self, test=False, *args, **kwargs):
    eh = SMTPHandler('localhost', fromaddr='[email protected]',
                     toaddrs=LOG_EMAIL_LIST, subject='Error report')
    eh.setLevel(logging.ERROR)
    self.logger.logger.addHandler(eh)
    super(BaseGenericSpider, self).__init__(*args, **kwargs)

When I add errors manually (e. g. self.logger.error('Test')), I receive a notification on my email. But When I test it with scrapy errors (e. g. I've made syntax typo in a crawler's code to test it), I see that Scrapy logged that error, but I didn't receive anything in my email. Can you help? Thank you in advance.

UPD. Ok, I think I've found a problem, but I don't know how to solve it now. When I add a handler to self.logger, I have messages like this:

2016-02-12 14:23:19 [sslv] ERROR: Test error case

But scrapy errors have this format:

2016-02-12 14:44:28 [scrapy] ERROR: Error while obtaining start requests

So, as you see, scrapy logger has another name. Actually, also we have twisted logger and many other. How to set email handler to all of that's loggers?

Upvotes: 1

Views: 967

Answers (1)

drjackild
drjackild

Reputation: 472

Alreasy solved. I've add handler in __init__.py file and connect it to root logger.

import logging
from logging.handlers import SMTPHandler

from settings import LOG_EMAIL_LIST

eh = SMTPHandler('localhost', fromaddr='[email protected]',
                         toaddrs=LOG_EMAIL_LIST, subject='Error report')
eh.setLevel(logging.ERROR)
logging.getLogger().addHandler(eh)

Upvotes: 2

Related Questions