Skip to content

RollbarHandler should include extra data from LogRecords automatically (or provide a switch to enable that) #388

@GrahamLea

Description

@GrahamLea

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions