def init_app(self, app): # Ensure that the traceid is parsed/propagated on every request app.before_request(before_request) # Let's get the app's base package name so we can set the correct formatter, filter and logger names app_module_name = Path(__file__).resolve().parents[2].parts[-1] logconfig = { "version": 1, "disable_existing_loggers": False, "formatters": { "simple": { "()": app_module_name + ".custom_extensions.enhanced_logging.formatters.JsonFormatter" }, "content_security_policy": { "()": app_module_name + ".custom_extensions.enhanced_logging" ".formatters.ContentSecurityPolicyFormatter" }, }, "filters": { "contextual": { "()": app_module_name + ".custom_extensions.enhanced_logging.filters.ContextualFilter" } }, "handlers": { "console": { "class": "logging.StreamHandler", "formatter": "simple", "filters": ["contextual"], "stream": "ext://sys.stdout", }, "content_security_policy": { "class": "logging.StreamHandler", "formatter": "content_security_policy", "filters": ["contextual"], "stream": "ext://sys.stdout", }, }, "loggers": { app.logger.name: { "handlers": ["console"], "level": app.config["FLASK_LOG_LEVEL"] }, "content_security_policy": { "handlers": ["content_security_policy"], "level": app.config["FLASK_LOG_LEVEL"], }, }, } app.config.update(LOGCONFIG=logconfig) logger = LogConfig() logger.init_app(app)
def init_app(self, app): # Ensure that the traceid is parsed/propagated on every request app.before_request(before_request) # Let's get the app's base package name so we can set the correct formatter, filter and logger names app_module_name = Path(__file__).resolve().parents[2].parts[-1] logconfig = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'simple': { '()': app_module_name + '.custom_extensions.enhanced_logging.formatters.JsonFormatter' }, 'content_security_policy': { '()': app_module_name + '.custom_extensions.enhanced_logging' '.formatters.ContentSecurityPolicyFormatter' } }, 'filters': { 'contextual': { '()': app_module_name + '.custom_extensions.enhanced_logging.filters.ContextualFilter' } }, 'handlers': { 'console': { 'class': 'logging.StreamHandler', 'formatter': 'simple', 'filters': ['contextual'], 'stream': 'ext://sys.stdout' }, 'content_security_policy': { 'class': 'logging.StreamHandler', 'formatter': 'content_security_policy', 'filters': ['contextual'], 'stream': 'ext://sys.stdout' } }, 'loggers': { app.logger.name: { 'handlers': ['console'], 'level': app.config['FLASK_LOG_LEVEL'] }, 'content_security_policy': { 'handlers': ['content_security_policy'], 'level': app.config['FLASK_LOG_LEVEL'] } } } app.config.update(LOGCONFIG=logconfig) logger = LogConfig() logger.init_app(app)
def make_app(**extra_config): app = Flask(__name__, static_folder='static', template_folder='templates') app.config.from_object('f2e.default_config') for k in app.config: value_from_env = os.getenv(k) if value_from_env is None: continue app.config[k] = value_from_env app.config.update(extra_config) logcfg = LogConfig() logcfg.init_app(app) app.twilio_client = TwilioRestClient(app.config['TWILIO_ACCOUNT_SID'], app.config['TWILIO_AUTH_TOKEN']) app.twilio_download_session = requests.Session() retries = Retry(total=3, backoff_factor=1, status_forcelist=[404, 502, 503, 504]) app.twilio_download_session.auth = (app.config['TWILIO_ACCOUNT_SID'], app.config['TWILIO_AUTH_TOKEN']) app.twilio_download_session.mount('http://', HTTPAdapter(max_retries=retries)) app.twilio_download_session.mount('https://', HTTPAdapter(max_retries=retries)) app.sendgrid_client = SendGridAPIClient( apikey=app.config['SENDGRID_API_KEY']) app.pdf = pdf.PDFKit(app.config) logging.info('Mounting twilio directory') app.register_blueprint(bp_twilio.mod, url_prefix='/twilio') logging.info('Mounting sendgrid directory') app.register_blueprint(bp_sg.mod, url_prefix='/sg') if util.truthish(app.config['SECURE']): log.info('Only accepting https') @app.before_request def before_request(): proto = request.headers.get('X-Forwarded-Proto', 'http') if proto == 'https': return if request.url.startswith('http://'): log.info('*** redirect url %s => https', request.url) log.info('... request headers follow') for k, v in request.headers.items(): log.info('... %20s => %r', k, v) url = request.url.replace('http://', 'https://', 1) return redirect(url, 301) return app
def init_app(app, settings): app.config.from_object(settings) logcfg = LogConfig() logcfg.init_app(app) return logcfg