Notification texts go here Contact Us Buy Now!

change the keys' names in a python json logger

Sure, here's the HTML code for the copywriter's response to changing the keys' names in a Python JSON logger:

It is possible to rename the keys in a Python JSON logger using the rename_fields parameter of the JsonFormatter class.

For example, the following code will rename the "asctime" and "levelname" keys to "time" and "level", respectively:

      formatter = jsonlogger.JsonFormatter(
          fmt="%(asctime)s %(name)s %(levelname)s %(message)s",
          rename_fields={"asctime": "time", "levelname": "level"}, # <--- added this line
          datefmt="%d/%m/%y[ %H:%M:%S ]",
          json_indent=4
      )
    

This will produce the following JSON output:

      {
          "time": "29/06/22[ 14:10:12 ]",
          "name": "myLovelyLogger",
          "level": "WARNING",
          "message": "something terrible happened"
      }
    

It is also possible to rename the exc_info key, but this only works with version 2.0.6 or later of the python-json-logger package.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.