Synchronous HTTP Handler
SyncHTTPHandler is a stdlib logging.Handler implementation that forwards log records to an HTTP endpoint using the requests library.
Behavior and Context
The handler is intended for sink entries with type: "http" in the config pipeline. It supports:
- configurable HTTP method
- optional headers
- payload normalization from LogRecord
- compatibility with both running and non-running event-loop contexts
Purpose
- Transport Bridge: Connect stdlib logging to HTTP collectors.
- Minimal Coupling: Works with plain logging and structlog output.
- Resilience: Best-effort delivery without raising into application flow.
High-Level API & Examples
Example 1: Direct Handler Usage
import logging
from jazzmine.logging.handlers import SyncHTTPHandler
handler = SyncHTTPHandler(
url="https://collector.internal/logs",
method="POST",
headers={"Authorization": "Bearer <token>"},
)
logger = logging.getLogger("http_logger")
logger.setLevel(logging.INFO)
logger.addHandler(handler)
logger.info('{"event":"startup","ok":true}')Example 2: Through Package Config
from jazzmine.logging import BaseLogger
logger = BaseLogger({
"name": "gateway",
"level": "INFO",
"json": True,
"sinks": [
{
"type": "http",
"url": "https://collector.internal/logs",
"method": "POST",
"headers": {"X-Api-Key": "<key>"},
}
],
})
logger.resolve_config()
logger.info("payment accepted", order_id="o-17")Detailed Class Functionality
init(url, method='POST', headers=None, timeout=2.0)
- Validates requests dependency.
- Stores endpoint, upper-cased method, headers, and timeout.
emit(record: logging.LogRecord) -> None
- Builds payload:
- if record.msg is dict or list, uses directly
- else attempts json.loads(record.getMessage())
- fallback payload is { "message": ..., "level": ... }
- If an event loop is running, submits _post(payload) via asyncio.to_thread.
- If no event loop, sends synchronously.
- Any exception triggers handleError(record).
_post(payload) -> None
- Sends JSON payload using requests.post or generic requests.request.
- Swallows network exceptions.
Error Handling
- LoggerDependencyError during initialization if requests is missing.
- Delivery errors are swallowed in _post.
- emit wraps failures with standard logging handleError semantics.
Remarks
- This handler is best-effort; it does not provide retry, backoff, or DLQ semantics.
- For high-volume async delivery, prefer the async worker-based path.
Exception Hierarchy
This module defines the logging package exception hierarchy. It provides semantic error classes that make failure handling cleaner and easier to route in application code.
Async HTTP Worker
AsyncHttpWorker is an internal queue-backed HTTP delivery worker used by async HTTP sink configurations. It decouples event production from network I/O and supports controlled async flushing during shutdown.