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.