How to log data as JSON with Python

Better Stack Team
Updated on May 4, 2022

The simplest way is to use a custom module:

 
pip install json-logging

To initialize the logger when not using the web framework, use this line of code:

 
json_logging.init_non_web(enable_json=True)

For web applications, replace _non_web with the name of framework. (_flask, _sanic,...)

More documentation about this module is provided here.