{"id":6691,"library":"jsonformatter","title":"JSON Formatter","description":"jsonformatter is a Python library that provides a logging formatter to output log records as JSON objects. It allows for easy customization of LogRecord attributes, enabling users to include or replace fields, for instance, to integrate with log aggregation systems like Logstash. It supports Python 2.7 and Python 3. The latest version, 0.3.4, was released in November 2024, with an irregular but ongoing release cadence.","status":"active","version":"0.3.4","language":"en","source_language":"en","source_url":"https://github.com/MyColorfulDays/jsonformatter.git","tags":["logging","json","formatter","log-management"],"install":[{"cmd":"pip install jsonformatter","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"basicConfig","correct":"from jsonformatter import basicConfig"},{"symbol":"JsonFormatter","correct":"from jsonformatter import JsonFormatter"}],"quickstart":{"code":"import logging\nfrom jsonformatter import basicConfig, JsonFormatter\nimport sys\n\n# Basic configuration for JSON output to console\nbasicConfig(level=logging.INFO)\nlogging.info('Hello, jsonformatter basic config!')\n\n# Example with custom formatter and fields\nFORMAT_STRING = '''{\n    \"time\": \"asctime\",\n    \"level\": \"levelname\",\n    \"message\": \"message\",\n    \"logger_name\": \"name\",\n    \"process_id\": \"process\"\n}'''\n\nlogger = logging.getLogger('my_app')\nlogger.setLevel(logging.DEBUG)\n\nhandler = logging.StreamHandler(sys.stdout)\nformatter = JsonFormatter(FORMAT_STRING)\nhandler.setFormatter(formatter)\nlogger.addHandler(handler)\n\nlogger.info('User logged in', extra={'user_id': 123, 'ip_address': '192.168.1.1'})\ntry:\n    raise ValueError('Something went wrong')\nexcept ValueError:\n    logger.exception('An error occurred during processing.')","lang":"python","description":"This quickstart demonstrates two ways to use `jsonformatter`: first, using `basicConfig` for a quick setup, and second, by creating a `JsonFormatter` instance with a custom format string and attaching it to a `StreamHandler`. It also shows how to include extra fields in log messages and handle exceptions."},"warnings":[{"fix":"Verify the PyPI package name (`jsonformatter` vs `python-json-logger`) and the GitHub repository (`MyColorfulDays/jsonformatter` vs `nhairs/python-json-logger`) to ensure you are installing and using the intended library.","message":"This `jsonformatter` library (by MyColorfulDays, version 0.3.4) is distinct from `python-json-logger` (by madzak/nhairs), which is a more widely used and actively maintained alternative with a similar purpose. Users often confuse the two. If you're looking for the commonly recommended JSON logger, you might be looking for `python-json-logger`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Pass a `default` callable or a custom `json.JSONEncoder` subclass via the `cls` parameter to the `JsonFormatter` constructor to define how non-serializable objects should be converted. For example, `JsonFormatter(format_string, default=str)` will convert such objects to their string representation.","message":"When logging objects that are not natively JSON serializable (e.g., custom class instances, `datetime` objects without special handling), `jsonformatter` may raise `TypeError` unless a custom `default` or `cls` parameter is provided to the `JsonFormatter` constructor. This is a common pitfall in Python's `json` module usage.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For new development, use Python 3 exclusively. For existing projects, consider migrating to Python 3. `jsonformatter` is also compatible with Python 3, so ensure your environment uses a supported Python 3 version.","message":"While `jsonformatter` officially supports Python 2.7, Python 2 has reached its End-of-Life (EOL) and is no longer maintained. Using it in new projects or maintaining existing Python 2 projects carries security and compatibility risks.","severity":"deprecated","affected_versions":"All versions supporting Python 2.7 (>=0.2.x)"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}