-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
49 lines (36 loc) · 1.29 KB
/
application.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#this is specifically for AWS elastic beanstalk
import logging.handlers
from os import environ
from flaskweb import app
# Create logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# Handler
LOG_FILE = '/tmp/sample-app.log'
handler = logging.handlers.RotatingFileHandler(LOG_FILE, maxBytes=1048576, backupCount=5)
handler.setLevel(logging.INFO)
# Formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# Add Formatter to Handler
handler.setFormatter(formatter)
# add Handler to Logger
logger.addHandler(handler)
def handle_exceptions(err):
# Log exception in your logs
# get traceback and sys exception info and log as required
app.logger.error(getattr(err, 'description', str(err)))
# Print traceback
# return your response using getattr(e, 'code', 500) etc.
return f"{err.__class__.__name__}: {err}"
# Exception is used to catch all exceptions
app.register_error_handler(Exception, handle_exceptions)
#what AWS EB looks for 'application'
application = app
#this is not run if wsgi container invoke this
if __name__ == '__main__':
HOST = environ.get('SERVER_HOST', 'localhost')
try:
PORT = int(environ.get('PORT', '8000'))
except ValueError:
PORT = 8000
application.run(HOST, PORT)