Expected Behaviour
I've integrated Rollbar with Python's standard logging module using the RollbarHandler.
Some of my logging statements use the extra keyword parameter to add extra context, e.g.
except:
logger.exception("Uncaught error", extra={"customer.id": customer_id})
I expected RollbarHandler would include the data in the extra argument automatically, but it doesn't.
Actual
RollbarHandler looks for an attribute called extra_data on the LogRecord, which isn't a standard part of the logging API that I can see.
While pyrollbar provides a way to transform the payload, the LogRecord isn't available to that callback, so there's no way to get hold of the extra data at that point.
Workaround
I was able to put the extra_data attribute that RollbarHandler is expecting into the LogRecord by providing my own subclass of Logger and overriding the makeRecord function to add it in:
if extra:
record.extra_data = extra
Seeing as the purpose of RollbarHandler is to integrate with the logging module, it feels like this step shouldn't be necessary on my part. I think RollbarHandler should be figuring out from the LogRecord's __dict__ what data was added via the extra parameter and including this in the custom data.
Environment
Python 3.9.6 (v3.9.6:db3ff76da1, Jun 28 2021, 11:49:53) [Clang 6.0 (clang-600.0.57)]
pyrollbar===0.16.1
Expected Behaviour
I've integrated Rollbar with Python's standard
loggingmodule using theRollbarHandler.Some of my logging statements use the
extrakeyword parameter to add extra context, e.g.I expected
RollbarHandlerwould include the data in the extra argument automatically, but it doesn't.Actual
RollbarHandlerlooks for an attribute calledextra_dataon theLogRecord, which isn't a standard part of theloggingAPI that I can see.While pyrollbar provides a way to transform the payload, the
LogRecordisn't available to that callback, so there's no way to get hold of the extra data at that point.Workaround
I was able to put the
extra_dataattribute thatRollbarHandleris expecting into theLogRecordby providing my own subclass ofLoggerand overriding themakeRecordfunction to add it in:Seeing as the purpose of
RollbarHandleris to integrate with theloggingmodule, it feels like this step shouldn't be necessary on my part. I thinkRollbarHandlershould be figuring out from theLogRecord's__dict__what data was added via theextraparameter and including this in the custom data.Environment
Python 3.9.6 (v3.9.6:db3ff76da1, Jun 28 2021, 11:49:53) [Clang 6.0 (clang-600.0.57)]pyrollbar===0.16.1