{"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":"python","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,"search_vec":"'0.9.45':38 'addit':53 'alibaba':19,55 'alibaba-cloud':54 'aliyun':1,7,57 'allow':25 'bug':49 'cadenc':46 'client':14 'cloud':20,56 'collect':28 'consum':29 'continu':44 'current':35 'featur':52 'fix':50 'follow':42 'frequent':48 'general':41 'interact':17 'librari':15 'log':2,8,22,33,58,61 'log-servic':60 'process':32 'provid':12 'python':4,10 'queri':30 'releas':45 'sdk':5,11,59 'servic':3,9,23,62 'user':26 'version':36","created_at":"2026-04-16T13:38:16.485481+00:00","updated_at":"2026-04-16T13:38:16.485481+00:00","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'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.9.47","cli_name":"","cli_version":null,"type":"library","homepage":"https://www.alibabacloud.com/product/log-service","github":"https://github.com/aliyun/aliyun-log-python-sdk","docs":null,"changelog":null,"pypi":"https://pypi.org/project/aliyun-log-python-sdk/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["http-networking","data"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-06-28","next_check":"2026-07-28","install_tag":null}}