ignoring some default fields from SimpleJsonFormatter
Peter Otten
__peter__ at web.de
Tue Aug 21 12:27:37 EDT 2018
shradhattx at gmail.com wrote:
> I am using for my logger
>
> handler.setFormatter(SimpleJsonFormatter(json.dumps))
Where does SimpleJsonFormatter come from?
If it uses the json.dumps argument to serialize a dict you may pass a
modified version instead:
# untested
KEYS_TO_IGNORE = {"fields", "you", "do", "not", "want"}
def my_pruning_dumps(d):
for k in KEYS_TO_IGNORE:
try:
del d[k]
except KeyError:
pass
return json.dumps(d)
handler.setFormatter(SimpleJsonFormatter(my_pruning_dumps))
> It had some default fields - timestamp, function, line_number, module,
> level
>
> and flexibility to provide extra fields in json log with use of
>
> logger.info("my test message", extra={"anotherfield1": "test"})
>
> I am using decorator functions so some of the default fields provided ex-
> function, line_number, module are not useful as it gives information on
> decorator module, line_number. I like to remove some fields from being
> logged while retaining others, keeping also the use of "extra"
>
> This is what I tried -
> logging.Formatter('{"timestamp": "%(asctime)s.%(msecs)06d",
> "level":"%(levelname)s"}',
> '%Y-%m-%dT%H:%M:%S')
>
> The problem is that it doesn't print the fields given in 'extra'
>
>
> How do I accomplish this?
More information about the Python-list
mailing list