The Flask tutorial has an example of emailing yourself when an error occurs. I would like to add some information from request, but kept receiving an error:
RuntimeError: working outside of request context
Here is what I have:
if __name__ == '__main__':
if not app.debug:
# create mail handler
import logging
from logging.handlers import SMTPHandler
mail_handler = SMTPHandler('127.0.0.1',
'[email protected]',
['[email protected]'], 'YourApplication Failed')
# Log format
from logging import Formatter
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
''' % request.headers )) # Added request here
# Attach log handler to app
mail_handler.setLevel(logging.ERROR)
app.logger.addHandler(mail_handler)
How do I get the request context for logging?