Asyncio logging handlers
Project description
scietex.logging
scietex.logging is an asynchronous logging package designed for high-performance applications that require non-blocking logging. It uses asyncio to manage log message queues and provides multiple backends, such as console and Redis logging, allowing for easy extension to other logging targets.
Features
- Asynchronous Logging: Log messages are queued and handled asynchronously, reducing impact on application performance.
- Multiple Backends: Supports console and Redis logging out of the box.
- Flexible Logging Levels: Compatible with Python’s standard logging levels (
DEBUG,INFO,WARNING,ERROR,CRITICAL). - Optional Dependencies: Only installs dependencies for the specific backends you need.
Examples
Explore the examples/ directory to see usage examples that demonstrate how to set up and work with scietex.logging. Each example provides a practical setup for different logging scenarios, including basic console logging and Redis-based logging.
For detailed descriptions of each example, refer to the Examples README.
Requirements
- Python 3.9+
- Additional dependencies for specific backends:
- Redis support:
redis.asyncio(pip install scietex.logging[redis]) - PostgreSQL support: Coming Soon!
asyncpg(pip install scietex.logging[postgres])
- Redis support:
Installation
Install the base package with:
pip install scietex.logging
To install all optional dependencies (including Redis and upcoming PostgreSQL support), use:
pip install scietex.logging[all]
Or, to install individual dependencies as needed:
pip install scietex.logging[redis] # For Redis logging
pip install scietex.logging[postgres] # For PostgreSQL logging
Basic Usage
Console Logging
The following example shows how to set up asynchronous console logging.
import logging
from scietex.logging import AsyncBaseHandler
import asyncio
# Set up logger and handler
logger = logging.getLogger("MyAsyncLogger")
logger.setLevel(logging.DEBUG)
handler = AsyncBaseHandler()
logger.addHandler(handler)
async def main():
await handler.start_logging()
logger.info("This is an asynchronous log message")
await handler.stop_logging()
asyncio.run(main())
Redis Logging
This example demonstrates logging to a Redis stream.
import logging
from scietex.logging import AsyncRedisHandler
import asyncio
# Set up logger and Redis handler
logger = logging.getLogger("MyAsyncLogger")
logger.setLevel(logging.DEBUG)
handler = AsyncRedisHandler(stream_name="my_log_stream")
logger.addHandler(handler)
async def main():
await handler.start_logging()
logger.error("This error message will be logged to Redis!")
await handler.stop_logging()
asyncio.run(main())
Configuration
scietex.logging is designed to allow easy configuration of additional backends and custom logging formats:
Formatting: Use Python’s standard logging Formatter to customize output. For example, to log timestamps in ISO format:
formatter = logging.Formatter(
fmt="%(asctime)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%dT%H:%M:%SZ"
)
handler.setFormatter(formatter)
Extending scietex.logging
To add support for additional logging backends, subclass AsyncBaseHandler and implement new workers as shown in the Redis example. The structure of the package allows for seamless extension by adding new worker methods for different logging destinations.
Example: Custom Database Handler
from scietex.logging import AsyncBaseHandler
import asyncpg
class AsyncPostgresHandler(AsyncBaseHandler):
def __init__(self, db_url):
super().__init__()
self.db_url = db_url
self.queues["postgres"] = asyncio.Queue()
self.workers.append(self._postgres_worker())
async def _postgres_worker(self):
self.conn = await asyncpg.connect(self.db_url)
while self.logging_running_event.is_set() or not self.queues["postgres"].empty():
record = await self.queues["postgres"].get()
await self.conn.execute("INSERT INTO logs (level, message) VALUES ($1, $2)", record.levelname, record.getMessage())
self.queues["postgres"].task_done()
def emit(self, record):
super().emit(record)
asyncio.create_task(self.queues["postgres"].put(record))
Contributing
Contributions are welcome! If you find a bug or want to add a feature, please open an issue or submit a pull request.
License
This project is licensed under the MIT License.
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
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 scietex_logging-0.1.2.tar.gz.
File metadata
- Download URL: scietex_logging-0.1.2.tar.gz
- Upload date:
- Size: 15.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e4a1cfda77540350d050873889aa7a6f6dbebeac858112b5e0b0de07ab3d19eb
|
|
| MD5 |
25811ac37a675581283697474e070402
|
|
| BLAKE2b-256 |
df1c163bdd00a9414b8c66eccb8135b0a0f7bde048eadfffa9b442c69bee160b
|
Provenance
The following attestation bundles were made for scietex_logging-0.1.2.tar.gz:
Publisher:
python-publish.yml on bond-anton/scietex.logging
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
scietex_logging-0.1.2.tar.gz -
Subject digest:
e4a1cfda77540350d050873889aa7a6f6dbebeac858112b5e0b0de07ab3d19eb - Sigstore transparency entry: 984980257
- Sigstore integration time:
-
Permalink:
bond-anton/scietex.logging@484cb295ea838791446d35e63458872b83320ca1 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/bond-anton
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@484cb295ea838791446d35e63458872b83320ca1 -
Trigger Event:
release
-
Statement type:
File details
Details for the file scietex_logging-0.1.2-py3-none-any.whl.
File metadata
- Download URL: scietex_logging-0.1.2-py3-none-any.whl
- Upload date:
- Size: 14.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a30507e23c44f9b91407ac90d169c6200ee0a206883931cb8ac9deca6476874
|
|
| MD5 |
68b1a84f494bfec6c7b5a6edb7b84a8e
|
|
| BLAKE2b-256 |
c058695a588a47e4916e8b1adf617bf7f4d7881459efb74fb0e41ce6c507925e
|
Provenance
The following attestation bundles were made for scietex_logging-0.1.2-py3-none-any.whl:
Publisher:
python-publish.yml on bond-anton/scietex.logging
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
scietex_logging-0.1.2-py3-none-any.whl -
Subject digest:
4a30507e23c44f9b91407ac90d169c6200ee0a206883931cb8ac9deca6476874 - Sigstore transparency entry: 984980270
- Sigstore integration time:
-
Permalink:
bond-anton/scietex.logging@484cb295ea838791446d35e63458872b83320ca1 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/bond-anton
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@484cb295ea838791446d35e63458872b83320ca1 -
Trigger Event:
release
-
Statement type: