A Python library for collecting and dumping data from an api service.
Project description
Escalite
A Python library for per-request logging and escalation, designed for API services.
Installation
Using pip:
pip install escalite
Using Poetry:
poetry add escalite
Configuration
Configure notifiers (e.g., email) in a dictionary format. Example:
notifier_configs = {
"notifiers": [
{
"type": "email",
"config": {
"smtp_server": "smtp.example.com",
"smtp_port": 587,
"sender_email": "your@email.com",
"sender_password": "yourpassword",
"recipient_emails": ["admin@example.com"],
"use_tls": True
}
}
]
}
Usage Example with FastAPI
from fastapi import FastAPI, Request
from escalite.escalite import Escalite
app = FastAPI()
notifier_configs = {
"notifiers": [
{
"type": "email",
"config": {
"smtp_server": "smtp.example.com",
"smtp_port": 587,
"sender_email": "your@email.com",
"sender_password": "yourpassword",
"recipient_emails": ["admin@example.com"],
"use_tls": True
}
}
]
}
@app.middleware("http")
async def escalite_logging_middleware(request: Request, call_next):
with Escalite().logging_context(notifier_configs):
Escalite.add_to_log("request_path", str(request.url.path), tag="api_logs")
response = await call_next(request)
Escalite.add_to_log("response_status", response.status_code, tag="api_logs")
return response
@app.get("/")
def read_root():
Escalite.add_service_log("root_service", "Root endpoint accessed")
return {"Hello": "World"}
Manual Usage Example
Here is an additional usage example for showing how to use Escalite without the logging_context() context manager. This demonstrates manual configuration, starting and ending logging, and triggering escalation.
from escalite.escalite import Escalite
notifier_configs = {
"notifiers": [
{
"type": "email",
"config": {
"smtp_server": "smtp.example.com",
"smtp_port": 587,
"sender_email": "your@email.com",
"sender_password": "yourpassword",
"recipient_emails": ["admin@example.com"],
"use_tls": True
}
}
]
}
# Set up notifiers from configs
Escalite.set_notifiers_from_configs(notifier_configs)
# Start logging for a request or operation
Escalite.start_logging()
# Add logs as needed
Escalite.add_to_log("event", "User login", tag="api_logs")
Escalite.add_service_log("auth_service", "User authenticated", level="info")
# End logging and collect logs
logs = Escalite.end_logging()
print("Collected logs:", logs)
# Trigger escalation (e.g., send notifications)
Escalite.escalate()
This example shows explicit control over the logging lifecycle and escalation, suitable for use outside of context managers or in custom workflows.
Usage Example - Service Call Logging
We can use start_service_log and stop_service_log to track the lifecycle of a service call:
from escalite import Escalite
# Start logging for the request
Escalite.start_logging()
# Log the start of a service call
Escalite.start_service_log(
"oauth_service",
"Starting OAuth service",
url="/oauth/start",
code=200
)
# ... your service logic here ...
# Log the end of a service call
Escalite.stop_service_log(
"oauth_service",
"OAuth service completed",
url="/oauth/start",
code=200
)
# End logging and retrieve logs
Escalite.end_logging()
logs = Escalite.get_all_logs()
print(logs)
Notifiers
Here are some notifier configuration examples that are currently supported. Replace the configuration values with your actual credentials or endpoints.
Telegram Notifier
from escalite.escalite import Escalite
notifier_configs = {
"notifiers": [
{
"type": "telegram",
"config": {
"bot_token": "your-telegram-bot-token",
"chat_id": "your-chat-id"
}
}
]
}
Escalite.set_notifiers_from_configs(notifier_configs)
Escalite.start_logging()
Escalite.add_to_log("event", "Telegram notifier test", tag="api_logs")
Escalite.end_logging()
Escalite.escalate()
WhatsApp Notifier
from escalite.escalite import Escalite
notifier_configs = {
"notifiers": [
{
"type": "whatsapp",
"config": {
"api_url": "https://your-whatsapp-api-endpoint.com/send",
"access_token": "your-access-token",
"phone_number": "recipient-phone-number"
}
}
]
}
Escalite.set_notifiers_from_configs(notifier_configs)
Escalite.start_logging()
Escalite.add_to_log("event", "WhatsApp notifier test", tag="api_logs")
Escalite.end_logging()
Escalite.escalate()
Slack Notifier
from escalite.escalite import Escalite
notifier_configs = {
"notifiers": [
{
"type": "slack",
"config": {
"webhook_url": "https://hooks.slack.com/services/your/webhook/url"
}
}
]
}
Escalite.set_notifiers_from_configs(notifier_configs)
Escalite.start_logging()
Escalite.add_to_log("event", "Slack notifier test", tag="api_logs")
Escalite.end_logging()
Escalite.escalate()
Email Notifier
from escalite.escalite import Escalite
notifier_configs = {
"notifiers": [
{
"type": "email",
"config": {
"smtp_server": "smtp.example.com",
"smtp_port": 587,
"sender_email": "your@email.com",
"sender_password": "yourpassword",
"recipient_emails": ["admin@example.com"],
"use_tls": True
}
}
]
}
Escalite.set_notifiers_from_configs(notifier_configs)
Escalite.start_logging()
Escalite.add_to_log("event", "Email notifier test", tag="api_logs")
Escalite.end_logging()
Escalite.escalate()
Features
- Per-request logging using contextvars
- Pluggable notifiers (email, etc.)
- Easy integration with FastAPI and other frameworks
Contributing
Contributions are welcome! Please see the CONTRIBUTING.md file for guidelines.
License
MIT
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 escalite-0.1.8.tar.gz.
File metadata
- Download URL: escalite-0.1.8.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.2 Darwin/24.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc6532005de5404c7c192085d4b436c1a7a365ae8e32bb51aadb9a09a54e1b82
|
|
| MD5 |
e1b552ef30f42ca17af178da641db173
|
|
| BLAKE2b-256 |
17f1fc8197172aaa20a759e02f6a8549417544bcf544e56db03f62c7966f5a9a
|
File details
Details for the file escalite-0.1.8-py3-none-any.whl.
File metadata
- Download URL: escalite-0.1.8-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.2 Darwin/24.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0732f84cca80caa7ea0edd624e751933a9fe70a1cf780e8f1c0ef0a923023b35
|
|
| MD5 |
a019b82fe80890670ea9ff24a0f4312d
|
|
| BLAKE2b-256 |
73e01d0b299c1d0a8e50984689df9918fa608cd99b1a1209d412978b235a0b40
|