Skip to main content

A robust JSON logging library for Python

Project description

JSON Logger

A robust, production-ready JSON logging library for Python. This library makes it easy to output logs in JSON format, which is essential for modern observability stacks (ELK, Datadog, CloudWatch, etc.).

Installation

pip install dh-json-logger

Usage

Basic Usage

import logging
import sys
from json_logger import JSONFormatter

# Setup logger
logger = logging.getLogger("my_app")
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(JSONFormatter())
logger.addHandler(handler)
logger.setLevel(logging.INFO)

# Log something
logger.info("Application started", extra={"user_id": 42})

Output:

{"timestamp": "2023-10-27 10:00:00,123", "level": "INFO", "name": "my_app", "message": "Application started", "module": "test", "filename": "test.py", "funcName": "<module>", "lineno": 10, "user_id": 42}

Advanced Configuration

You can configure which system fields to include in the JSON output:

# Include process and thread information
formatter = JSONFormatter(include_fields={"process", "thread", "taskName"})

# The default includes 'taskName' and 'stack_info'

Features

  • JSON Output: Properly formatted JSON for all log records.
  • Extra Fields: Automatically merges extra={...} dictionary into the top-level JSON object.
  • Exception Handling: Formats exceptions and stack traces into the exception field.
  • System Info: Optional support for process, thread, taskName, and stack_info.
  • Robustness: Handles non-serializable objects gracefully by converting them to strings.
  • Unicode Support: Correctly handles emojis and special characters.

Contributing

We welcome contributions! Please follow these steps:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/amazing-feature).
  3. Install development dependencies:
    pip install -e .[dev]
    
  4. Run tests to ensure everything is working:
    pytest
    
  5. Commit your changes.
  6. Push to the branch.
  7. Open a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dh_json_logger-0.1.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dh_json_logger-0.1.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file dh_json_logger-0.1.1.tar.gz.

File metadata

  • Download URL: dh_json_logger-0.1.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for dh_json_logger-0.1.1.tar.gz
Algorithm Hash digest
SHA256 376284921e2c14a66646ae877abb27e2859308fcc280333c36e8d6c4dcedc88d
MD5 16beefd3e9192da50af4fd6cffa78301
BLAKE2b-256 f184fd63abfd2b1bc67111b90b315b1fd911448e70a98d763e31f90361f56b03

See more details on using hashes here.

File details

Details for the file dh_json_logger-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: dh_json_logger-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for dh_json_logger-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6a8ab31c5e190b3c0d4367cf2cd5fc651e110a3f0e3351e91ca23bd9b948c475
MD5 33c65c195a6a28dd65fbbeb5a097e48f
BLAKE2b-256 00babf073614503b882b153b6866e4b2ed73570aef7c39cb4c806041eb566ba7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page