[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

ignoring some default fields from SimpleJsonFormatter

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:
            del d[k]
        except KeyError:
    return json.dumps(d)


> 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?