app.config["SEND_EMAIL"] = False
app.config["EMAIL_RECIPIENT"] = "analytics-dashboard@codeforamerica.org"
app.config["EMAIL_SENDER"] = "mike@codeforamerica.org"

logger = logging.getLogger("noteworthy")
logger.setLevel(logging.DEBUG)

handler1 = logging.StreamHandler(sys.stderr)
handler1.setLevel(logging.DEBUG)
logger.addHandler(handler1)

if "SENDGRID_USERNAME" in environ and "SENDGRID_PASSWORD" in environ:
    app.config["SMTP_USERNAME"] = environ["SENDGRID_USERNAME"]
    app.config["SMTP_PASSWORD"] = environ["SENDGRID_PASSWORD"]
    app.config["SMTP_HOSTNAME"] = "smtp.sendgrid.net"
    app.config["SEND_EMAIL"] = True

    handler2 = SMTPHandler(
        app.config["SMTP_HOSTNAME"],
        app.config["EMAIL_SENDER"],
        (app.config["EMAIL_RECIPIENT"], app.config["EMAIL_SENDER"]),
        "City Analytics Dashboard error report",
        (app.config["SMTP_USERNAME"], app.config["SMTP_PASSWORD"]),
    )

    handler2.setLevel(logging.WARNING)
    logger.addHandler(handler2)


@app.errorhandler(builders.SetupError)