Unable to use a custom log formatter
jackton1 opened this issue · 2 comments
jackton1 commented
Formatter
...
'formatters': {
'custom': {
'format': '%(message)s %(my_object)r',
},
},
Handler
'handlers': {
'custom': {
'level': 'WARNING',
'class': 'rollbar.logger.RollbarHandler',
'formatter': 'custom',
},
},
Using:
log.warning('This is a test', extra={'my_object': my_object})
Expectation:
The correct formatted message shows up in rollbar.
currently shows
"This is a test"
Instead of
"This is a test <MyObject.__repr__(...)>"
phillipuniverse commented
I noticed this same issue. Any sort of timeline available for a fix?
phillipuniverse commented
I think the problem is in the emit
:
Line 89 in f67253a
It is not taking into account the __dict__
of the LogRecord
.
Here's how you could grab it out though:
DEFAULT_ATTRS = (
'args',
'asctime',
'created',
'exc_info',
'exc_text',
'filename',
'funcName',
'levelname',
'levelno',
'lineno',
'module',
'msecs',
'message',
'msg',
'name',
'pathname',
'process',
'processName',
'relativeCreated',
'stack_info',
'thread',
'threadName',
)
def get_extra_from_record(self, record):
"""
Returns the dict that was passed in as extra data
e.g. logging,info('Log log log', extra={'extra_code': 'ABC123'})
"""
return {
attr: record.__dict__[attr]
for attr in record.__dict__
if attr not in DEFAULT_ATTRS
}
You could extend from the RollbarHandler
and use the information on get_extra_from_record
to customize the message, maybe pass in as extra_data
?
class RollbarWithExtraHandler(RollbarHandler):
def emit(self, record):
record.extra_data = self._get_extra_from_record(record)
super().emit(record)
def _get_extra_from_record(self, record):
"""
Returns the dict that was passed in as extra data
e.g. logging,info('Log log log', extra={'extra_code': 'ABC123'})
"""
return {
attr: record.__dict__[attr]
for attr in record.__dict__
if attr not in DEFAULT_ATTRS
}