A custom formatter for Python's logging module that outputs logs in the Bunyan JSON format.
Project description
Bunyan Formatter
A custom formatter for Python's logging module that outputs logs in the Bunyan JSON format.
Description
This package provides a BunyanFormatter
class that formats log records into
the Bunyan JSON format. Bunyan is a lightweight JSON logger for Node.js, but
this formatter allows you to use the same log format in Python projects.
Key features:
- Outputs logs in JSON format
- Includes project name, hostname, file path, line number, and other metadata
- Supports various log levels (DEBUG, INFO, WARNING, ERROR, CRITICAL)
- Handles both project and external file paths
Installation
To install the Bunyan Formatter package, run:
pip install bunyan-formatter
Usage
Here's a basic example of how to use the Bunyan Formatter in your Python project:
import logging
from bunyan_formatter import BunyanFormatter
# Create a logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# Create a handler and set the formatter
handler = logging.StreamHandler()
formatter = BunyanFormatter(project_name="MyProject", project_root="/path/to/my/project")
handler.setFormatter(formatter)
# Add the handler to the logger
logger.addHandler(handler)
# Now you can use the logger
logger.debug("This is a debug message")
logger.info("This is an info message")
logger.warning("This is a warning message")
logger.error("This is an error message")
logger.critical("This is a critical message")
Django
In your Django project's settings.py
file, add the following logging configuration:
BASE_DIR = Path(__file__).resolve().parent.parent
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"bunyan": {
"()": BunyanFormatter,
"project_name": "MyProject",
"project_root": BASE_DIR,
},
},
"handlers": {
"console": {
"level": "DEBUG",
"class": "logging.StreamHandler",
"formatter": "bunyan",
"stream": "ext://sys.stdout",
},
"file": {
"level": "DEBUG",
"class": "logging.FileHandler",
"filename": BASE_DIR / "logs" / "django.log",
"formatter": "bunyan",
},
},
"root": {
"level": "DEBUG",
"handlers": ["console", "file"],
},
}
Examples
Basic Logging
logger.info("User logged in", extra={"username": "john_doe"})
Output:
{
"v": 0,
"name": "MyProject",
"msg": "User logged in",
"level": 30,
"levelname": "INFO",
"hostname": "your-hostname",
"target": "__main__",
"line": 10,
"file": "main.py",
"extra": {
"username": "john_doe"
}
}
Error Logging with Exception
try:
result = 1 / 0
except ZeroDivisionError as e:
logger.exception("An error occurred", exc_info=True)
Output:
{
"v": 0,
"name": "MyProject",
"msg": "An error occurred",
"level": 50,
"levelname": "ERROR",
"hostname": "your-hostname",
"target": "__main__",
"line": 15,
"file": "main.py",
"err": {
"message": "division by zero",
"name": "ZeroDivisionError",
"stack": [
// Stack trace here
]
}
}
Custom Fields
You can add custom fields to your log entries:
logger.info("Order processed", extra={
"order_id": 12345,
"customer_id": 67890,
"total_amount": 100.00
})
Output:
{
"v": 0,
"name": "MyProject",
"msg": "Order processed",
"level": 30,
"levelname": "INFO",
"hostname": "your-hostname",
"target": "__main__",
"line": 20,
"file": "main.py",
"extra": {
"order_id": 12345,
"customer_id": 67890,
"total_amount": 100.0
}
}
Contributing
Contributions are welcome! Please submit pull requests or issues on our GitHub repository.
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
File details
Details for the file bunyan_formatter-0.1.4.tar.gz
.
File metadata
- Download URL: bunyan_formatter-0.1.4.tar.gz
- Upload date:
- Size: 21.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | df7230bc0fc0f2d90e2b5324f7b0741d0b1910f3d245f70cd4e29bf5449b0b79 |
|
MD5 | 771218b8f23cc426fd348805ca4c4105 |
|
BLAKE2b-256 | 9c772f8835b9a90e22cbba7c983d17173ed2fee34597a9e62ee0037e5c4c2827 |
File details
Details for the file bunyan_formatter-0.1.4-py3-none-any.whl
.
File metadata
- Download URL: bunyan_formatter-0.1.4-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e6ed3461c607e2a52d9f7b4e63fc454772e9322003b68a4d970dabe599c30e37 |
|
MD5 | b66ff05097caaae285db6c266c52ed3e |
|
BLAKE2b-256 | 0bff5361bcda50e978fb8f9b03a876cba66e9f621cbd2d07219689bf10141b79 |