You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've not yet tested this code, but here's a starting point for doing both:
Note: this code is written for logging.LogRecord which assume that all data is in __dict__ already.
classCustomJsonFormatter(jsonlogger.JsonFormatter):
defadd_fields(self, log_record, record, message_dict):
super(CustomJsonFormatter, self).add_fields(log_record, record, message_dict)
## Add Everytingforname, valueinrecord.__dict__.items():
ifnamenotinlog_recordandnotname.startswith("_"):
# Only log names that have not already been added and aren't some# "private" attribute.log_record[name] =value## Exclude# TODO: populate self.excluded_fields# self.excluded_fields = ["foo", "bar", "threadName"]fornameinself.excluded_fields:
ifnameinlog_record:
dellog_record[name]
return
Edit
It might be possible to do this by setting reserved_attrs=tuple() agument, excluding specific fields can then be done by adding them back in to the reserved_attrs.
Note: this won't work for "calculated" attributes such as message, asctime, exc_info without first adding the attributes to _required_fields (via parsinging the format).
Currently, I am writing a custom formatter to include all fields with the intention of adding some of my own later.
I have two sets of questions
Q1 : Is this the correct way? i.e. subclass the formatter and than copying field by field over ? or am I going about it the wrong way ?
Ultimately, I will want to include most of the default fields plus I am going to add some custom in-house fields.
Q2 : What if I want most but not all the default fields, is there some pythonic way to do that ?
The text was updated successfully, but these errors were encountered: