Logtail-python is the official Python client library for Logtail.com (part of Better Stack). It provides a `logging.Handler` to easily send logs from your Python applications, including Django and FastAPI, to Logtail for centralized log management, aggregation, and analysis. It is actively maintained with frequent updates, currently at version 0.3.4.
pip install logtail-pythonVerified import paths — ran on the pinned version, not inferred.
This quickstart sets up a basic Python logger with `LogtailHandler` to send logs to Logtail. It demonstrates logging INFO, WARNING, and an exception with extra structured data. Remember to replace `YOUR_LOGTAIL_SOURCE_TOKEN` with your actual token or set `LOGTAIL_SOURCE_TOKEN` as an environment variable, and verify your `INGESTING_HOST` if not using the default.
Remove the protocol (e.g., `https://`) from the `host` parameter when initializing `LogtailHandler`.
Always use `sys.exit()` for application termination to allow the logging handler's background flusher to send all pending logs.
Use `logger = logging.getLogger(__name__)` and explicitly add `logger.addHandler(handler)` to gain fine-grained control over your logging configuration.
Sanitize or simplify complex objects passed in the `extra` dictionary to ensure they are easily serializable.
Ensure the `logtail-python` package is installed (`pip install logtail-python`) and import `LogtailHandler` directly from the `logtail` module: `from logtail import LogtailHandler`.
Pass your Logtail source token as the `source_token` argument when initializing `LogtailHandler`: `handler = LogtailHandler(source_token='YOUR_SOURCE_TOKEN')` or ensure it's set as an environment variable that the handler can pick up if configured to do so.
First, import and instantiate `LogtailHandler`, then add the handler instance to your logger: `from logtail import LogtailHandler import logging handler = LogtailHandler(source_token='YOUR_SOURCE_TOKEN') logger = logging.getLogger(__name__) logger.addHandler(handler)`.
Verify that your `host` parameter in `LogtailHandler` is correct (e.g., `https://in.logtail.com`), check your network connectivity, and ensure no firewalls are blocking outbound connections to the Logtail ingestion endpoint. If deploying to a platform, check its network policies.
Ensure your application terminates gracefully. Use `sys.exit()` instead of `os._exit()` to allow background threads, including the Logtail flusher, to complete their tasks before the program exits.
No dependency data recorded yet.