Skip to content

Do not render complex objects by default? #35

@caioariede

Description

@caioariede

I've had an interesting experience recently with Django and Granian. We have migrated from Gunicorn to Granian, and, suddenly, while testing it in local, we saw environment variables being logged, including things like SECRET_KEY. After investigating it, we figured it was due to the request object being JSON serialized. It appears that, with Gunicorn, the request object isn't serializable, but it seems to be with Granian.

I wonder if, by default, logs should not include complex objects, and instead, do something like repr(obj).
Consider the following code:

def json_record(self, message: str, extra: dict, record: logging.LogRecord):
    extra = {
        key: value if isinstance(value, (int, float, complex, str)) else repr(value)
        for key, value in extra.items()
        if not isinstance(value, (list, dict))
    }

This way, if one wants to have those values, they can override the json_record method, adding their own logic.

What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions