import logging import logging.config import uuid from app import app as application from flask import request, Request from util.names import urn_generator from data.model import db as model_db # Turn off debug logging for boto logging.getLogger('boto').setLevel(logging.CRITICAL) from endpoints.api import api_bp from endpoints.index import index from endpoints.web import web from endpoints.tags import tags from endpoints.registry import registry from endpoints.webhooks import webhooks from endpoints.realtime import realtime from endpoints.callbacks import callback from logentries import LogentriesHandler logger = logging.getLogger(__name__) werkzeug = logging.getLogger('werkzeug') werkzeug.setLevel(logging.DEBUG) profile = logging.getLogger('profile') profile.setLevel(logging.DEBUG) logentries_key = application.config.get('LOGENTRIES_KEY', None) if logentries_key: logger.debug('Initializing logentries with key: %s' % logentries_key) werkzeug.addHandler(LogentriesHandler(logentries_key)) profile.addHandler(LogentriesHandler(logentries_key)) application.register_blueprint(web) application.register_blueprint(callback, url_prefix='/oauth2') application.register_blueprint(index, url_prefix='/v1') application.register_blueprint(tags, url_prefix='/v1') application.register_blueprint(registry, url_prefix='/v1') application.register_blueprint(api_bp, url_prefix='/api') application.register_blueprint(webhooks, url_prefix='/webhooks') application.register_blueprint(realtime, url_prefix='/realtime') class RequestWithId(Request): request_gen = staticmethod(urn_generator(['request'])) def __init__(self, *args, **kwargs): super(RequestWithId, self).__init__(*args, **kwargs) self.request_id = self.request_gen() @application.before_request def _request_start(): profile.debug('Starting request: %s', request.path) @application.after_request def _request_end(r): profile.debug('Ending request: %s', request.path) return r class InjectingFilter(logging.Filter): def filter(self, record): record.msg = '[%s] %s' % (request.request_id, record.msg) return True profile.addFilter(InjectingFilter()) def close_db(exc): db = model_db if not db.is_closed(): logger.debug('Disconnecting from database.') db.close() application.teardown_request(close_db) application.request_class = RequestWithId if __name__ == '__main__': logging.config.fileConfig('conf/logging.conf', disable_existing_loggers=False) application.run(port=5000, debug=True, threaded=True, host='0.0.0.0')