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
exceptionfield. - System Info: Optional support for
process,thread,taskName, andstack_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:
- Fork the repository.
- Create a new branch (
git checkout -b feature/amazing-feature). - Install development dependencies:
pip install -e .[dev]
- Run tests to ensure everything is working:
pytest
- Commit your changes.
- Push to the branch.
- Open a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
376284921e2c14a66646ae877abb27e2859308fcc280333c36e8d6c4dcedc88d
|
|
| MD5 |
16beefd3e9192da50af4fd6cffa78301
|
|
| BLAKE2b-256 |
f184fd63abfd2b1bc67111b90b315b1fd911448e70a98d763e31f90361f56b03
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a8ab31c5e190b3c0d4367cf2cd5fc651e110a3f0e3351e91ca23bd9b948c475
|
|
| MD5 |
33c65c195a6a28dd65fbbeb5a097e48f
|
|
| BLAKE2b-256 |
00babf073614503b882b153b6866e4b2ed73570aef7c39cb4c806041eb566ba7
|