Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not render complex objects by default? #35

Closed
caioariede opened this issue Sep 27, 2024 · 3 comments
Closed

Do not render complex objects by default? #35

caioariede opened this issue Sep 27, 2024 · 3 comments

Comments

@caioariede
Copy link

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?

@marselester
Copy link
Owner

Does this PR fit into your use case #36? If so, could you please test it?

@caioariede
Copy link
Author

Yes, @marselester -- I just wanted to add that, if environment variables are leaked, this can be a potential security risk.

@marselester
Copy link
Owner

The FlatJSONFormatter is now available in the v1.1.0 release https://pypi.org/project/JSON-log-formatter/1.1/.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants