{"id":6980,"library":"aliyun-log-python-sdk","title":"Aliyun Log Service Python SDK","description":"The Aliyun Log Service Python SDK provides a client library for interacting with Alibaba Cloud's Log Service. It allows users to collect, consume, query, and process logs. The current version is 0.9.45, and it generally follows a continuous release cadence with frequent bug fixes and feature additions.","status":"active","version":"0.9.45","language":"en","source_language":"en","source_url":"https://github.com/aliyun/aliyun-log-python-sdk","tags":["alibaba-cloud","aliyun","logging","sdk","log-service"],"install":[{"cmd":"pip install aliyun-log-python-sdk","lang":"bash","label":"Install stable version"},{"cmd":"pip install aliyun-log-python-sdk[lz4]","lang":"bash","label":"Install with optional LZ4 compression support"}],"dependencies":[{"reason":"Used for data serialization. Specific versions are critical for Python 2 compatibility.","package":"protobuf","optional":false},{"reason":"HTTP client for making API calls.","package":"requests","optional":false},{"reason":"Parsing various date formats, often used in log querying.","package":"dateparser","optional":false},{"reason":"Python 2 and 3 compatibility utilities.","package":"six","optional":false},{"reason":"Optional dependency for LZ4 compression, providing faster data transmission for PutLogs.","package":"lz4a","optional":true}],"imports":[{"symbol":"LogClient","correct":"from aliyun.log import LogClient"},{"symbol":"LogItem","correct":"from aliyun.log import LogItem"},{"symbol":"PutLogsRequest","correct":"from aliyun.log.put_logs_request import PutLogsRequest"},{"note":"The ConsumerGroupWorker class is nested deeper within the 'consumer' module in recent versions, requiring a more specific import path.","wrong":"from aliyun.log.consumer import ConsumerGroupWorker","symbol":"ConsumerGroupWorker","correct":"from aliyun.log.consumer.consumer_group_worker import ConsumerGroupWorker"}],"quickstart":{"code":"import os\nfrom aliyun.log import LogClient, LogItem, PutLogsRequest\nimport time\n\n# Replace with your actual Aliyun Log Service details\nendpoint = os.environ.get('ALIYUN_LOG_ENDPOINT', 'https://cn-hangzhou.log.aliyuncs.com') # e.g., 'cn-hangzhou.log.aliyuncs.com'\naccesskey_id = os.environ.get('ALIYUN_AK_ID', '')\naccesskey_secret = os.environ.get('ALIYUN_AK_SECRET', '')\nproject_name = os.environ.get('ALIYUN_LOG_PROJECT', 'your-project-name')\nlogstore_name = os.environ.get('ALIYUN_LOG_LOGSTORE', 'your-logstore-name')\n\nif not all([accesskey_id, accesskey_secret]):\n    print(\"Please set ALIYUN_AK_ID and ALIYUN_AK_SECRET environment variables.\")\nelse:\n    client = LogClient(endpoint, accesskey_id, accesskey_secret)\n\n    # Create a log item\n    log_item = LogItem()\n    log_item.set_time(int(time.time()))\n    log_item.set_contents([('level', 'INFO'), ('message', 'Hello from Python SDK'), ('source', 'quickstart')])\n\n    # Put logs into a list\n    log_items = [log_item]\n\n    # Create a PutLogsRequest\n    request = PutLogsRequest(\n        project_name, \n        logstore_name, \n        'your-topic', \n        'your-source-ip', # Or use client.get_source_ip() if needed\n        log_items\n    )\n\n    try:\n        response = client.put_logs(request)\n        print(f\"PutLogs successful. Request ID: {response.get_request_id()}\")\n    except Exception as e:\n        print(f\"Failed to put logs: {e}\")","lang":"python","description":"This quickstart demonstrates how to initialize the LogClient and put a single log item to an Aliyun Log Service logstore. Ensure you replace placeholder values with your actual project, logstore, endpoint, and credentials, preferably loaded from environment variables."},"warnings":[{"fix":"Review the official documentation or release notes for v0.6.45 regarding Shipper API changes and update your code accordingly.","message":"The API signatures for the Shipper component were updated in version 0.6.45. Existing code using the Shipper API will likely break.","severity":"breaking","affected_versions":"0.6.45 and later"},{"fix":"Consult the updated examples and documentation for `ConsumerProcessorBase` in the `tests/consumer_group_examples` directory of the SDK repository.","message":"The interface for `ConsumerProcessorBase` used in Consumer Group implementations was enhanced in version 0.6.40. Custom processors might require updates to align with the new interface.","severity":"breaking","affected_versions":"0.6.40 and later"},{"fix":"For Python 2 compatibility, ensure your environment adheres to the pinned dependency versions mentioned in SDK releases (e.g., `protobuf<=3.17.3, dateparser<=0.7.6`). Python 3 is generally more robust and recommended for new development.","message":"While the SDK generally supports both Python 2 and 3, specific dependency versions (e.g., protobuf, dateparser) were pinned in v0.7.0 and later to explicitly maintain Python 2 compatibility. Using different versions or older SDK releases with Python 2 might lead to unexpected compatibility issues.","severity":"gotcha","affected_versions":"All versions, especially older versions with Python 2"},{"fix":"Install the `lz4a` dependency: `pip install aliyun-log-python-sdk[lz4]` or `pip install lz4a`.","message":"To enable LZ4 compression for faster log transmission (supported from v0.6.42), the `lz4a` package must be explicitly installed. Without it, the SDK will automatically fall back to GZIP compression.","severity":"gotcha","affected_versions":"0.6.42 and later"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"The `ConsumerGroupWorker` class is located at `aliyun.log.consumer.consumer_group_worker`. Use `from aliyun.log.consumer.consumer_group_worker import ConsumerGroupWorker`.","cause":"Attempting to import `ConsumerGroupWorker` directly from `aliyun.log.consumer`, which is not the correct path in recent versions.","error":"ModuleNotFoundError: No module named 'aliyun.log.consumer'"},{"fix":"Verify your `accesskey_id` value. Ensure it is correctly copied, has no leading/trailing spaces, and is associated with your Alibaba Cloud account.","cause":"The provided Alibaba Cloud Access Key ID is either incorrect, empty, or malformed.","error":"Failed to put logs: RequestError: InvalidAccessKeyId, Message: The Access Key ID is invalid."},{"fix":"Double-check your `accesskey_secret`, `accesskey_id`, and `endpoint`. Ensure the endpoint matches the region of your Log Service project. Also, verify your system's time is accurate and synchronized.","cause":"This error typically indicates an incorrect Access Key Secret, an incorrect Endpoint, or unsynchronized system time, leading to a mismatch in the request signature calculation.","error":"RequestError: LogServiceError: SignatureDoesNotMatch, Message: The request signature we calculated does not match the signature you provided."},{"fix":"For LZ4 compression, ensure `lz4a` is installed via `pip install aliyun-log-python-sdk[lz4]`. The `put_logs` method will then automatically utilize LZ4 if available. Avoid calling specific compression methods directly unless specified by the latest documentation.","cause":"Attempting to call an outdated or non-existent method for putting compressed logs. In newer versions, compression is often handled internally based on available dependencies or through parameters of existing methods.","error":"AttributeError: 'LogClient' object has no attribute 'put_compress_logs'"}]}