Solution
mh = SMTPHandler(
mailhost=app.config["MAIL_SERVER"],
fromaddr=app.config["MAIL_DEFAULT_SENDER"],
toaddrs=app.config["ERRORS_EMAIL"],
subject="Error",
)
mh.setLevel(logging.ERROR)
mh.setFormatter(logging.Formatter('%(user)s - %(message)s'))
app.logger.addHandler(mh)
app.logger = LoggerAdapter(app.logger, {'user': current_user})
class CurrentUserFilter(logging.Filter):
"""
Add a user attribute to all records
"""
def filter(self, record):
record.user = current_user
return True # Return True to pass the record to the next filter
mh = SMTPHandler(
mailhost=app.config["MAIL_SERVER"],
fromaddr=app.config["MAIL_DEFAULT_SENDER"],
toaddrs=app.config["ERRORS_EMAIL"],
subject="Error",
)
mh.setLevel(logging.ERROR)
mh.setFormatter(logging.Formatter('%(user)s - %(message)s'))
app.logger.addHandler(mh)
app.logger.addFilter(CurrentUserFilter())