Python Logging: how to add a custom field to LogRecord and register a global callback to set its value

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 
+5
source share
2 answers

You can define a custom filter that enters a value for record.user_id . By doing this, you can define a format that includes %(user_id)s , as well as other (standard) record attributes:

 format='%(asctime)-15s %(name)-5s %(levelname)-8s user_id %(user_id)-15s: %(message)s') 

and then all log calls will automatically add the value user_id .


 import logging class UserIDFilter(logging.Filter): """ This is a filter which injects contextual information into the log. """ from myapp.core import authenticationManager def filter(self, record): user_id = authenticationManager.current_user_id_if_authenticated() user_id = "unknown" if user_id is None else str(user_id) record.user_id = user_id return True logging.basicConfig( level=logging.DEBUG, format='%(asctime)-15s %(name)-5s %(levelname)-8s user_id %(user_id)-15s: %(message)s') logger = logging.getLogger(__name__) filt = UserIDFilter() logger.addFilter(filt) 
+5
source

logaugment ( Github page ) does this:

For use:

 import logaugment logaugment.set(logger, user_id='custom_value') 

All log calls using logger will now use the value 'custom_value' for %(user_id)s in the log message.

For installation:

 pip install logaugment 

(disclaimer: I developed this package)

0
source

All Articles