{"library":"logging-json","title":"logging-json","description":"logging-json is a Python library that provides a JSON formatter for the standard `logging` module, allowing applications to output logs in a structured JSON format. This is particularly useful for centralized logging systems and machine readability. The current version is 0.6.0, and it generally maintains an active release cadence with periodic updates for bug fixes and new features.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install logging-json"],"cli":null},"imports":["from logging_json import JSONFormatter"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import logging\nfrom logging_json import JSONFormatter\nimport sys\n\n# Configure a logger\nlogger = logging.getLogger(__name__)\nlogger.setLevel(logging.INFO)\n\n# Create a stream handler (e.g., to stdout)\nhandler = logging.StreamHandler(sys.stdout)\n\n# Create a JSON formatter instance\n# Optional: Customize indentation, ASCII encoding, or datetime format\nformatter = JSONFormatter(\n    json_indent=2, # Pretty-print JSON\n    json_ensure_ascii=False, # Allow non-ASCII characters directly\n    datetime_format=\"%Y-%m-%dT%H:%M:%S%z\" # Custom datetime format with timezone\n)\n\n# Set the formatter for the handler\nhandler.setFormatter(formatter)\n\n# Add the handler to the logger\nlogger.addHandler(handler)\n\n# Log messages with standard fields and extra context\nlogger.info(\"This is an info message.\")\nlogger.warning(\"Something potentially bad happened.\", extra={\"user_id\": 123, \"session\": \"abc\"})\n\ntry:\n    raise ValueError(\"Example error\")\nexcept ValueError:\n    # logger.exception automatically includes traceback\n    logger.exception(\"An error occurred during processing.\")\n\nlogger.debug(\"This message will not be shown as level is INFO.\")","lang":"python","description":"This quickstart demonstrates how to set up a basic logger with `logging-json`. It configures a `StreamHandler` to output JSON formatted logs to standard output. It also shows how to include extra dictionary fields in your logs, which are automatically merged into the root JSON object.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}