Skip to main content

A Python library for collecting and dumping data from an api service.

Project description

Escalite

PyPI version Python versions codecov

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 = {
    "email": {
        "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 = {
    "email": {
        "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 = {
    "email": {
        "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.

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 = {
    "telegram": {
        "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 = {
    "whatsapp": {
        "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 = {
    "slack": {
        "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 = {
    "email": {
        "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

License

MIT


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

escalite-0.1.3.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

escalite-0.1.3-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file escalite-0.1.3.tar.gz.

File metadata

  • Download URL: escalite-0.1.3.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.2 Darwin/24.3.0

File hashes

Hashes for escalite-0.1.3.tar.gz
Algorithm Hash digest
SHA256 c326f445e265f0d99d314fb14c210d08fbe310e9782a56749fae3710392882f6
MD5 61a870cef58d71109c46c936ac62974d
BLAKE2b-256 1cf77bbddea3e799473bb8fbb01a41ec66f68f03fa19115ebb39db2cdf467842

See more details on using hashes here.

File details

Details for the file escalite-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: escalite-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 11.3 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

Hashes for escalite-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6859488e8f2414e3b11175908e728ea2a0e9f94aa1275bcaa873b44a7c7603e7
MD5 4ac7cffdc29843e81ea41cbeed9e919c
BLAKE2b-256 99f8888f78a287e940ad781748f46b753be7db5c422e86fbfa43db250b7236a3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page