[Flask] insert current_user value into logger generated emails?

Corey Boyle coreybrett at gmail.com
Tue Jul 13 10:36:35 EDT 2021


Inserting the following into my app factory did the trick for me.

    if app.config["ENV"] == "development":
        pass
    else:
        mh = SMTPHandler(
            mailhost=app.config["MAIL_SERVER"],
            fromaddr=app.config["MAIL_DEFAULT_SENDER"],
            toaddrs=app.config["ERRORS_EMAIL"],
            subject="Integrity Error",
        )
        mh.setLevel(logging.ERROR)
        mh.setFormatter(
            logging.Formatter(
                "%(user)s \n[%(asctime)s] \n%(levelname)s in %(module)s:
%(message)s"
            )
        )
        app.logger.addHandler(mh)
        app.logger = LoggerAdapter(app.logger, {"user": current_user})
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/flask/attachments/20210713/c8ff19f0/attachment.html>


More information about the Flask mailing list