Forums

Flask, logging, smtp handler, no mails

Hi everybody,

While I receive error mails in my development setup (on localhost) I dont get them in my 'production' setup on pythonanywhere. I use the code from the Flask Error Handling Docs.

When I catch exceptions I use

app.log_exception(exc)

in order to send the exception into the pipeline to eventually receive mails.

Could you help me out here? What am I possibly doing wrong?

Kind regards, Tom

run.py:

#!flask/bin/python
from app import app

if __name__ == '__main__':

    if not app.debug:
        import logging
        from logging.handlers import SMTPHandler
        from logging import Formatter
        mail_handler = SMTPHandler(app.config['MAIL_SERVER'],
                                   app.config['MAIL_DEFAULT_SENDER'],
                                   app.config['MAIL_ERROR_RECIPIENT'],
                                   app.config['MAIL_ERROR_SUBJECT'],
                                   credentials=(app.config['MAIL_USERNAME'],
                                                    app.config['MAIL_PASSWORD']),
                                   secure=())
        mail_handler.setLevel(logging.ERROR)
        mail_handler.setFormatter(Formatter('''
                                Message type:       %(levelname)s
                                Location:           %(pathname)s:%(lineno)d
                                Module:             %(module)s
                                Function:           %(funcName)s
                                Time:               %(asctime)s

                                Message:

                                %(message)s
                                '''))
        app.logger.addHandler(mail_handler)
    app.run()

Which mail server are you using?

it's mail.gmx.net

Ah, sorry, I missed something when looking at your code the first time around. Your config for the logging is all happening inside the

if __name__ == "__main__":

block, so it won't be called on PythonAnywhere. If you move it outside the if statement, then it should be applied.

oh man... true. thanks a bunch for the enlightenment! works perfectly.

Excellent, glad I could help!