Reputation:
I am trying to do the following:
# settings.py
'json': {
'format': 'Avails: {"levelname": "%(levelname)s", "asctime": "%(asctime)s", "funcName": "%(funcName)s", "filename": "%(filename)s", "lineno": "%(lineno)s", "message": "%(message)s"}',
},
And in the view:
log.info('this is my message', extra={'user': request.user}
How would I grab the extra
info in the log formatter?
Upvotes: 4
Views: 2173
Reputation:
Based on This, you can format logging any fields you wish to log. Likewise
LOGGING = {
[...]
'formatters': {
'verbose': {
'format': '{name} {levelname} {asctime} {module} {process:d} {thread:d} {message}',
'style': '{',
},
'simple': {
'format': '{levelname} {message}',
'style': '{',
},
},
}
Upvotes: 0
Reputation: 38952
The user in extra
can be added in the logging json formatter as
'json': {
'format': (
'Avails: {'
'"levelname": "%(levelname)s",'
'"asctime": "%(asctime)s",'
'"funcName": "%(funcName)s",'
'"filename": "%(filename)s",'
'"lineno": "%(lineno)s",'
'"message": "%(message)s",'
'"user": "%(user)s"'
'}',
)
},
Note that you need to pass a string value for user instead of the user object in the extra options. When this is not a string object, the __repr__
or __str__
value of the object passed replaces the corresponding conversion specification in the format string.
log.info('this was your message :D', extra={'user': request.user.pk}
Upvotes: 3