Closed fgimian closed 1 year ago
I don't see an answer posted either here or in https://github.com/madzak/python-json-logger/issues/78. I will share my investigation and workaround.
JsonFormatter accepts a reserved_attrs
parameter. The value of the parameter defaults to RESERVED_ATTRS. You can add additional attributes but make sure you append the original list. If not, all the ignored attributes by default will be added to the log entry.
Same situation as OP with Uvicorn's custom record attrs being forwarded through although not mentioned in format string.
A code example of the suggestion above:
# myproject/logging.py
from pythonjsonlogger import jsonlogger
class JsonFormatter(jsonlogger.JsonFormatter):
def __init__(self, *args, **kwargs):
kwargs["reserved_attrs"] = ["color_message", *jsonlogger.RESERVED_ATTRS]
super().__init__(*args, **kwargs)
Then in config:
[formatter_default_json]
class = myprojectt.logging.JsonFormatter
format = %(asctime)s
Hello,
This library outputs all extra data all the time, even when such fields don't exist in the format string.
I used a very minimal format to demonstrate the problem.
some uvicorn output with this config:
Similarly with the access logs:
You can see that these are all extra fields in uvicorn's source code:
Is there any way to exclude these fields from output via an INI config file at present?
Thanks Fotis