{"id":5783,"library":"logtail-python","title":"Logtail Python Client","description":"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.","status":"active","version":"0.3.4","language":"en","source_language":"en","source_url":"https://github.com/logtail/logtail-python","tags":["logging","log management","betterstack","observability","handler","cloud logging"],"install":[{"cmd":"pip install logtail-python","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"LogtailHandler","correct":"from logtail import LogtailHandler"}],"quickstart":{"code":"import logging\nimport os\nfrom logtail import LogtailHandler\n\n# Ensure SOURCE_TOKEN is set in your environment variables\n# or replace with your actual source token for testing.\nSOURCE_TOKEN = os.environ.get('LOGTAIL_SOURCE_TOKEN', 'YOUR_LOGTAIL_SOURCE_TOKEN')\nINGESTING_HOST = os.environ.get('LOGTAIL_INGESTING_HOST', 'in.logtail.com') # Default host, may vary by region/setup\n\nhandler = LogtailHandler(\n    source_token=SOURCE_TOKEN,\n    host=INGESTING_HOST\n)\n\nlogger = logging.getLogger(__name__)\nlogger.setLevel(logging.INFO)\n\n# Clear existing handlers to prevent duplicate logs if running multiple times\n# or if root logger already has handlers. Optional, but good practice.\nlogger.handlers = []\nlogger.addHandler(handler)\n\nlogger.info('Hello from Logtail-python!', extra={'service': 'my-app', 'environment': 'development'})\nlogger.warning('This is a warning message.', extra={'user_id': 123})\n\ntry:\n    1 / 0\nexcept ZeroDivisionError:\n    logger.exception('An error occurred during division.')\n\nprint(\"Logs sent to Logtail (check your Logtail Live tail). If nothing appears, ensure your SOURCE_TOKEN and INGESTING_HOST are correct.\")\n","lang":"python","description":"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."},"warnings":[{"fix":"Remove the protocol (e.g., `https://`) from the `host` parameter when initializing `LogtailHandler`.","message":"Starting from v0.3.3, the `host` parameter for `LogtailHandler` should *not* include the protocol (e.g., `https://`). If you were previously passing `https://in.logtail.com`, you should now pass `in.logtail.com` to avoid potential issues or misconfigurations.","severity":"gotcha","affected_versions":">=0.3.3"},{"fix":"Always use `sys.exit()` for application termination to allow the logging handler's background flusher to send all pending logs.","message":"When shutting down your application, using `os._exit()` will terminate all threads immediately, which can result in unsent logs. For graceful termination and to ensure all buffered logs are sent to Logtail, use `sys.exit()` instead.","severity":"gotcha","affected_versions":"All"},{"fix":"Use `logger = logging.getLogger(__name__)` and explicitly add `logger.addHandler(handler)` to gain fine-grained control over your logging configuration.","message":"Avoid configuring logging directly on the root logger (`logging.getLogger()`) in production applications. It can lead to less control and unexpected log destinations. Instead, get a named logger (`logging.getLogger(__name__)`) and add your `LogtailHandler` to it.","severity":"gotcha","affected_versions":"All"},{"fix":"Sanitize or simplify complex objects passed in the `extra` dictionary to ensure they are easily serializable.","message":"While the library attempts to handle circular references in log records (improved in v0.2.10, v0.3.1, v0.3.2), passing extremely complex or deeply nested objects in `extra` data might still lead to serialization issues or performance overhead. Ensure that extra data is generally JSON-serializable.","severity":"gotcha","affected_versions":"All (mitigated in >=0.2.10)"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}