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 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.0.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.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dh_json_logger-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 ed4eb9af84f3e4434fa3109416c9f55a0ca9fb90eb40a89235fb1b17f6c2d036
MD5 edfc9db71fc65faa89054940396e8385
BLAKE2b-256 8a5c665cc0eb98ce499a22724fab297433e3b9c7beabe84269c6d25400921576

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dh_json_logger-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 001d4d37b54c5b2f22a2876ad5ab0c590b4bca34ee0ed57370326dd7b607de0c
MD5 d6e6c4ef0c721f7979041615faa1a6a5
BLAKE2b-256 6fbe6a3d283d0ffdb08196a72301ef34e01312a4666c826d35de5cf0fa16afd5

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