{"id":4621,"library":"logzio-python-handler","title":"Logz.io Python Handler","description":"The `logzio-python-handler` library provides a Python logging handler that sends application logs to a Logz.io account. It supports bulk sending over HTTPS with SSL, handling logs asynchronously in a background thread, and offers optional OpenTelemetry trace context integration. As of version `4.1.9`, the library is actively maintained with frequent updates, primarily focusing on dependency management and robustness improvements.","status":"active","version":"4.1.9","language":"en","source_language":"en","source_url":"https://github.com/logzio/logzio-python-handler/","tags":["logging","logzio","observability","handler","cloud"],"install":[{"cmd":"pip install logzio-python-handler","lang":"bash","label":"Core handler"},{"cmd":"pip install 'logzio-python-handler[opentelemetry-logging]'","lang":"bash","label":"With OpenTelemetry trace context support"}],"dependencies":[{"reason":"HTTP client for sending logs. Regularly updated for security and stability.","package":"requests","optional":false},{"reason":"Used internally by the library. Regularly updated for security and stability.","package":"protobuf","optional":false},{"reason":"Build-time dependency, regularly updated.","package":"setuptools","optional":false},{"reason":"Build-time dependency, regularly updated.","package":"wheel","optional":false},{"reason":"Required for OpenTelemetry trace context integration.","package":"opentelemetry-instrumentation-logging","optional":true}],"imports":[{"symbol":"LogzioHandler","correct":"from logzio.handler import LogzioHandler"},{"note":"For dynamically adding extra fields to logs.","symbol":"ExtraFieldsLogFilter","correct":"from logzio.handler import ExtraFieldsLogFilter"},{"note":"Primarily for serverless environments to ensure logs are flushed.","symbol":"LogzioFlusher","correct":"from logzio.flusher import LogzioFlusher"}],"quickstart":{"code":"import logging\nimport os\nfrom logzio.handler import LogzioHandler\n\n# Replace with your Logz.io token and log type\nLOGZIO_TOKEN = os.environ.get('LOGZIO_TOKEN', 'YOUR_LOGZIO_TOKEN')\nLOGZIO_TYPE = os.environ.get('LOGZIO_TYPE', 'python-application')\n\nif LOGZIO_TOKEN == 'YOUR_LOGZIO_TOKEN':\n    print(\"WARNING: Please set the LOGZIO_TOKEN environment variable or replace 'YOUR_LOGZIO_TOKEN' in the script.\")\n    print(\"Skipping Logz.io handler configuration for quickstart.\")\n    # Fallback to console logging if token is not set\n    logging.basicConfig(level=logging.INFO)\n    logger = logging.getLogger(__name__)\nelse:\n    logger = logging.getLogger(__name__)\n    logger.setLevel(logging.INFO)\n\n    try:\n        handler = LogzioHandler(\n            LOGZIO_TOKEN,\n            logzio_type=LOGZIO_TYPE\n            # url=\"https://listener.logz.io:8071\" # Default, uncomment to specify a different listener URL\n            # debug=True # Uncomment for debug output to stdout\n        )\n        logger.addHandler(handler)\n        print(f\"Logz.io handler configured for type: {LOGZIO_TYPE}\")\n    except Exception as e:\n        print(f\"Error configuring Logz.io handler: {e}\")\n        print(\"Falling back to console logging.\")\n        logging.basicConfig(level=logging.INFO)\n        logger = logging.getLogger(__name__)\n\nlogger.info(\"This is an informational message sent via Logz.io handler.\")\nlogger.warning(\"This is a warning message.\")\ntry:\n    1 / 0\nexcept ZeroDivisionError:\n    logger.exception(\"An error occurred during division. Full stack trace will be sent.\")\n\n# In a real application, the handler manages a background thread for sending logs.\n# For simple scripts, a short delay might be needed to ensure logs are sent\n# before the script exits, though graceful shutdown attempts to drain remaining logs.\nimport time\ntime.sleep(1) # Give the handler a moment to flush logs for short scripts","lang":"python","description":"This quickstart demonstrates how to configure the `LogzioHandler` with Python's standard `logging` module. It uses environment variables for the Logz.io token and log type, falling back to a console logger if the token isn't set. It shows how to log informational, warning, and exception messages."},"warnings":[{"fix":"Upgrade your Python environment to version 3.5 or newer.","message":"Version 3.0.0 of `logzio-python-handler` (released before current 4.x series) dropped support for Python 2.7, 3.3, and 3.4. Users on these older Python versions must upgrade to Python 3.5 or newer to use versions 3.0.0 or later.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Implement proper shutdown hooks or a small delay in short-lived scripts if logs appear to be missing. For serverless environments, consider using `LogzioFlusher`.","message":"The handler uses a background thread to send logs asynchronously. While version 4.1.7 included fixes to prevent the main thread from blocking, applications may still experience a short delay during shutdown as the handler attempts a final log drain. Ensure your application's graceful termination allows sufficient time for this.","severity":"gotcha","affected_versions":"<4.1.7 (more prominent blocking); All versions (shutdown delay)"},{"fix":"Always verify your Logz.io token and listener URL are correct. Check Logz.io documentation for regional listener addresses.","message":"Providing an invalid Logz.io token or listener URL will prevent logs from being sent. Older versions might enter an endless loop attempting to send, while newer versions will drop unauthorized logs. This can silently cause data loss.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Monitor log sizes for exceptions. If critical information is being truncated, consider structuring your exception logging to send key details in separate, smaller fields or custom parsing rules in Logz.io.","message":"Historically, very large exception logs might have their `logzio_type` overwritten to `logzio-invalid-log` if they exceeded indexing limits, making them harder to query. While the system now truncates them to fit, be aware of potential truncation or unexpected type changes for extremely verbose exceptions.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}