{"id":451,"library":"python-json-logger","title":"Python JSON Logger","description":"A JSON log formatter for the Python logging package that supports various JSON encoders. Current version is 4.0.0, with regular releases typically made every few months.","status":"active","version":"4.0.0","language":"python","source_language":"en","source_url":"https://github.com/nhairs/python-json-logger","tags":["logging","JSON","formatter","Python"],"install":[{"cmd":"pip install python-json-logger","lang":"bash","label":"Install python-json-logger"}],"dependencies":[],"imports":[{"note":"Make sure to use the full path for compatibility.","symbol":"JsonFormatter","correct":"from pythonjsonlogger.jsonlogger import JsonFormatter"}],"quickstart":{"code":"import logging\nfrom pythonjsonlogger.jsonlogger import JsonFormatter\n\nlogger = logging.getLogger('my_logger')\nhandler = logging.StreamHandler()\nformatter = JsonFormatter()\nhandler.setFormatter(formatter)\nlogger.addHandler(handler)\nlogger.setLevel(logging.INFO)\nlogger.info('This is a log message with JSON formatting')","lang":"python","description":"Basic usage of JsonFormatter to log messages in JSON format."},"warnings":[{"fix":"Migrate to 'from pythonjsonlogger.jsonlogger import JsonFormatter'.","message":"The use of some older imports has changed, e.g., 'from pythonjsonlogger.jsonlogger import jsonlogger' is incorrect.","severity":"breaking","affected_versions":"<4.0.0"},{"fix":"Use the new formatter syntax that supports comma-separated lists.","message":"Ensure that required fields are correctly specified in comma-separated format to avoid formatting issues.","severity":"gotcha","affected_versions":">=4.0.0"}],"env_vars":null,"last_verified":"2026-05-12T13:54:01.334Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"pip install python-json-logger","cause":"The `python-json-logger` package, which provides the `json_log_formatter` module, is not installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'json_log_formatter'"},{"fix":"from json_log_formatter import JsonFormatter","cause":"Although the package is named `python-json-logger`, the main module containing the `JsonFormatter` class is `json_log_formatter`.","error":"ModuleNotFoundError: No module named 'python_json_logger'"},{"fix":"from json_log_formatter import JsonFormatter","cause":"The class name `JsonLogger` is incorrect; the correct class for JSON formatting in this library is `JsonFormatter`.","error":"ImportError: cannot import name 'JsonLogger' from 'json_log_formatter'"},{"fix":"Configure `JsonFormatter` with a `json_default` function to convert non-serializable objects into a serializable format:\n```python\nimport datetime\nfrom json_log_formatter import JsonFormatter\n\ndef custom_serializer(o):\n    if isinstance(o, datetime.datetime):\n        return o.isoformat()\n    raise TypeError(f\"Object of type {o.__class__.__name__} is not JSON serializable\")\n\nformatter = JsonFormatter(json_default=custom_serializer)\n```","cause":"A log record contains an object (e.g., datetime, custom class instance) that the default JSON encoder cannot serialize, and no custom `json_default` function or `json_encoder` class was provided to `JsonFormatter`.","error":"TypeError: Object of type <ClassName> is not JSON serializable"},{"fix":"Refer to the library's documentation for the correct list of supported arguments for `JsonFormatter`, such as `json_default`, `json_encoder`, `json_message_key`, `json_extra`, etc.","cause":"An unrecognized or misspelled keyword argument was passed to the `JsonFormatter` constructor.","error":"TypeError: JsonFormatter.__init__() got an unexpected keyword argument 'some_arg'"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":80,"quickstart_tag":"verified","pypi_latest":null,"install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.07,"mem_mb":2.6,"disk_size":"17.9M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.04,"mem_mb":2.6,"disk_size":"18M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.11,"mem_mb":2.8,"disk_size":"19.7M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.1,"mem_mb":2.8,"disk_size":"20M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.09,"mem_mb":2.7,"disk_size":"11.6M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.09,"mem_mb":2.7,"disk_size":"12M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.08,"mem_mb":2.9,"disk_size":"11.2M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.11,"mem_mb":2.7,"disk_size":"12M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.07,"mem_mb":3,"disk_size":"17.7M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.06,"mem_mb":3,"disk_size":"18M"}]},"quickstart_checks":{"last_tested":"2026-04-23","tag":"verified","tag_description":"quickstart runs on critical runtimes, recently tested","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}