{"id":2081,"library":"json-log-formatter","title":"JSON Log Formatter","description":"The `json-log-formatter` library provides a Python logging formatter that outputs log records as JSON strings. This structured logging approach facilitates easier integration with log aggregation and analysis systems like Logstash or ElasticSearch. The library is currently at version 1.1.1 and appears to be actively maintained, with regular updates to support modern Python logging practices.","status":"active","version":"1.1.1","language":"en","source_language":"en","source_url":"https://github.com/marselester/json-log-formatter","tags":["logging","json","formatter","logstash","structured logging","python"],"install":[{"cmd":"pip install json-log-formatter","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"This is the primary formatter class for basic JSON output.","symbol":"JSONFormatter","correct":"from json_log_formatter import JSONFormatter"},{"note":"Use this formatter to include all built-in log record attributes.","symbol":"VerboseJSONFormatter","correct":"from json_log_formatter import VerboseJSONFormatter"},{"note":"Use this formatter to flatten complex objects into strings.","symbol":"FlatJSONFormatter","correct":"from json_log_formatter import FlatJSONFormatter"}],"quickstart":{"code":"import logging\nimport sys\nfrom json_log_formatter import JSONFormatter\n\n# Configure a basic logger\nlogger = logging.getLogger('my_app')\nlogger.setLevel(logging.INFO)\n\n# Create a JSON formatter instance\nformatter = JSONFormatter()\n\n# Create a StreamHandler that writes to stdout\nhandler = logging.StreamHandler(sys.stdout)\nhandler.setFormatter(formatter)\n\n# Add the handler to the logger\nlogger.addHandler(handler)\n\n# Log some messages\nlogger.info('User signed up', extra={'user_id': 123, 'email': 'test@example.com'})\nlogger.warning('Payment failed', extra={'order_id': 'abc-123', 'reason': 'card declined'})\n\ntry:\n    raise ValueError('Something went wrong!')\nexcept ValueError:\n    logger.error('An unexpected error occurred', exc_info=True, extra={'transaction_id': 'xyz-456'})","lang":"python","description":"This quickstart demonstrates how to set up a basic logger using `JSONFormatter` to output structured JSON logs to standard output. It shows logging of informational messages with custom `extra` fields and how exceptions are handled."},"warnings":[{"fix":"Review logs to understand how non-serializable objects are represented. If specific serialization is required, override `JSONFormatter.json_record()` or ensure objects are pre-processed to be JSON-serializable.","message":"As of v0.3.0, the formatter attempts a 'best effort' to serialize log records containing non-serializable values (e.g., WSGIRequest objects) instead of raising a TypeError. While this prevents crashes, it might result in altered or omitted data for those specific fields if not explicitly handled.","severity":"gotcha","affected_versions":">=0.3.0"},{"fix":"If using `ujson` with custom types, ensure all objects passed to the logger are natively serializable by `ujson` or pre-serialize them. Alternatively, override `JSONFormatter.json_record()` to handle complex types explicitly before `ujson` attempts serialization. Consider `simplejson` for better `default` argument support.","message":"When using alternative JSON libraries like `ujson` or `simplejson` (by overriding `JSONFormatter.json_serializer`), be aware that `ujson` specifically does not support the `json.dumps(default=f)` argument. This can lead to `TypeError` exceptions or silently skipped attributes if objects cannot be serialized directly.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Subclass `JSONFormatter` and override the `json_record(self, message, extra, record)` method to manipulate the dictionary before JSON serialization. For example, add `extra` fields or format `datetime` objects.","message":"To add custom fields to every log record (e.g., user ID, IP address) or to customize the serialization of specific object types (e.g., `datetime` objects to timestamps), you must override the `json_record()` method in a custom formatter subclass. Not doing so will prevent these customizations from appearing in your logs.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Implement strict data filtering and redaction mechanisms *before* data reaches the logger. Carefully review `extra` dictionaries and any objects passed for serialization to ensure no sensitive information is present.","message":"Logging sensitive data (e.g., passwords, API keys, PII) in JSON logs is a significant security risk. JSON's flexible structure makes it easy to accidentally include more data than intended, which can violate privacy regulations.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}