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, Redis, and Valkey 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, Redis, and Valkey 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.10+
- Additional dependencies for specific backends:
- Redis support:
redis(pip install scietex.logging[redis]) - Valkey support:
valkey-glide(pip install scietex.logging[valkey])
- Redis support:
Installation
Install the base package with:
pip install scietex.logging
To install all optional dependencies (including Redis and Valkey 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[valkey] # For Valkey 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())
Valkey Logging
This example demonstrates logging to a Valkey stream.
import logging
from scietex.logging import AsyncValkeyHandler
import asyncio
# Set up logger and Valkey handler
logger = logging.getLogger("MyAsyncLogger")
logger.setLevel(logging.DEBUG)
handler = AsyncValkeyHandler(stream_name="my_log_stream")
logger.addHandler(handler)
async def main():
await handler.start_logging()
logger.error("This error message will be logged to Valkey!")
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)
A formatter affects the console (stdout) output only; broker backends build
their payloads from the handler's service_name/worker_id config and the
record directly, so they are invariant under setFormatter. See
docs/configuration.md.
Extending scietex.logging
To add support for additional logging backends, subclass AsyncBrokerHandler and implement connect(), disconnect(), and send_message() methods. AsyncLoggingHandler is the pure-machinery base that owns the queue/worker infrastructure but no sink of its own; AsyncBaseHandler builds on it and registers the console as a peer backend (enabled by default via stdout_enable), while AsyncBrokerHandler is designed for message broker backends like Redis or Valkey.
Example: Custom Database Handler
from scietex.logging import AsyncBrokerHandler
class AsyncPostgresHandler(AsyncBrokerHandler):
def __init__(self, db_url):
super().__init__(queue_name="postgres")
self.db_url = db_url
self._db_conn = None
async def connect(self):
import asyncpg
self._db_conn = await asyncpg.connect(self.db_url)
async def disconnect(self):
if self._db_conn:
await self._db_conn.close()
async def send_message(self, record):
await self._db_conn.execute(
"INSERT INTO logs (level, message) VALUES ($1, $2)",
record["level"],
record["message"],
)
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.
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-1.2.0.tar.gz.
File metadata
- Download URL: scietex_logging-1.2.0.tar.gz
- Upload date:
- Size: 41.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
afce576716e846562519e4b5bc3131b81b1e6a123ce1388b8ae7a03974f15834
|
|
| MD5 |
b502341af973f4e1afda46a49a350e53
|
|
| BLAKE2b-256 |
dbe9d93d52f83ed1bb4e1f7858b15f1d63fe11caad2505e5616683286f92693d
|
Provenance
The following attestation bundles were made for scietex_logging-1.2.0.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-1.2.0.tar.gz -
Subject digest:
afce576716e846562519e4b5bc3131b81b1e6a123ce1388b8ae7a03974f15834 - Sigstore transparency entry: 2749652306
- Sigstore integration time:
-
Permalink:
bond-anton/scietex.logging@55c6d6e284eebc669dac9a23c0a1f7212d9f2647 -
Branch / Tag:
refs/tags/v1.2.0 - Owner: https://github.com/bond-anton
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@55c6d6e284eebc669dac9a23c0a1f7212d9f2647 -
Trigger Event:
release
-
Statement type:
File details
Details for the file scietex_logging-1.2.0-py3-none-any.whl.
File metadata
- Download URL: scietex_logging-1.2.0-py3-none-any.whl
- Upload date:
- Size: 30.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6b785af1195727a4413314c34ebbb29264d3241565438474e3769a9c9b3791b2
|
|
| MD5 |
012a966c79f2acb3f346f2b67b2f0197
|
|
| BLAKE2b-256 |
966677cf646909d91c0a99c00523760ca8272f8c3ae7b65ab724eb9bf859b74f
|
Provenance
The following attestation bundles were made for scietex_logging-1.2.0-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-1.2.0-py3-none-any.whl -
Subject digest:
6b785af1195727a4413314c34ebbb29264d3241565438474e3769a9c9b3791b2 - Sigstore transparency entry: 2749652387
- Sigstore integration time:
-
Permalink:
bond-anton/scietex.logging@55c6d6e284eebc669dac9a23c0a1f7212d9f2647 -
Branch / Tag:
refs/tags/v1.2.0 - Owner: https://github.com/bond-anton
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@55c6d6e284eebc669dac9a23c0a1f7212d9f2647 -
Trigger Event:
release
-
Statement type: