Make Airbrake optional

This commit is contained in:
Midgard 2020-07-17 10:42:16 +02:00
parent 6282eed349
commit d904f3c562
Signed by: midgard
GPG key ID: 511C112F1331BBB4

View file

@ -6,7 +6,10 @@ from logging.handlers import TimedRotatingFileHandler
import typing import typing
from datetime import datetime from datetime import datetime
from airbrake import Airbrake, AirbrakeHandler try:
import airbrake
except ImportError:
airbrake = None
from flask import Flask, render_template from flask import Flask, render_template
from flask_bootstrap import Bootstrap, StaticCDN from flask_bootstrap import Bootstrap, StaticCDN
from flask_debugtoolbar import DebugToolbarExtension from flask_debugtoolbar import DebugToolbarExtension
@ -37,18 +40,25 @@ def register_plugins(app: Flask) -> Manager:
loglogger.addHandler(timedFileHandler) loglogger.addHandler(timedFileHandler)
app.logger.addHandler(timedFileHandler) app.logger.addHandler(timedFileHandler)
if app.config["AIRBRAKE_ID"]:
if airbrake is None:
raise Exception(
"Airbrake support was requested (AIRBRAKE_ID is present in config), "
"but could not import airbrake. Make sure it's installed"
)
airbrakelogger = logging.getLogger("airbrake") airbrakelogger = logging.getLogger("airbrake")
# Airbrake # Airbrake
airbrake = Airbrake(project_id=app.config["AIRBRAKE_ID"], airbrake = airbrake.Airbrake(project_id=app.config["AIRBRAKE_ID"],
api_key=app.config["AIRBRAKE_KEY"]) api_key=app.config["AIRBRAKE_KEY"])
# ugly hack to make this work for out errbit # ugly hack to make this work for out errbit
airbrake._api_url = "http://errbit.awesomepeople.tv/api/v3/projects/{}/notices".format( # pylint: disable=W0212 airbrake._api_url = "http://errbit.awesomepeople.tv/api/v3/projects/{}/notices".format( # pylint: disable=W0212
airbrake.project_id airbrake.project_id
) )
airbrakelogger.addHandler(AirbrakeHandler(airbrake=airbrake)) airbrakelogger.addHandler(airbrake.AirbrakeHandler(airbrake=airbrake))
app.logger.addHandler(AirbrakeHandler(airbrake=airbrake)) app.logger.addHandler(airbrake.AirbrakeHandler(airbrake=airbrake))
# Initialize SQLAlchemy # Initialize SQLAlchemy
db.init_app(app) db.init_app(app)