In a Flask application, I want to add a user_id field added to every error log record that gets produced, whenever a user_id exists in the flask.session.
I came up with the solution below but it's hackish, since it doesn't allow for the format string to drive the formatting of the user_id, and since the logging API seems to provides ways to customize logging (LoggerAdapter, logging.makeRecord, etc...) I figure there has to be a cleaner way.
What would be the "python way" to do it ?
class CustomFormatter(Formatter):
def format(self, record):
from myapp.core import authenticationManager
user_id = authenticationManager.current_user_id_if_authenticated()
user_id = "unknown" if user_id is None else str(user_id)
return super(F,self).format(record) + ", user_id" + user_id