In a Flask application, I want to add a user_id field added to each error log entry that is created whenever << 20> exists in flask.session .
I came up with a solution below, but it is hacked because it does not allow formatting a string for user_id formatting, and since the logging API seems to provide ways to configure logging ( LoggerAdapter , logging.makeRecord , etc.). I believe there should be a cleaner way.
What will be the python way?
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
source share