pip -V
pip install tencentcloud-cls-sdk-python
pip show tencentcloud-cls-sdk-python
Name: tencentcloud-cls-sdk-pythonVersion: 1.0.4Summary: TencentCloud cls log service Python client SDKHome-page: https://github.com/TencentCloud/tencentcloud-cls-sdk-pythonAuthor: farmerx
Variable | Type | Required | Description |
endpoint | String | Yes | Domain information. When filling in, see the domain name in the Available Regions under the API Log Upload Tab. |
accessKeyId | String | Yes | TencentCloud API key information. For key information acquisition, please visit API Key Management. Please ensure the associated account has the appropriate SDK log upload permission. |
accessKey | String | Yes | TencentCloud API key information. For key information acquisition, please visit API Key Management. Please ensure the associated account has the appropriate SDK log upload permission. |
topic_id | String | Yes | Log topic ID information. |
# Import required librariesimport timefrom tencentcloud.log.logclient import LogClientfrom tencentcloud.log.logexception import LogExceptionfrom tencentcloud.log.cls_pb2 import LogGroupListimport osdef upload(topic_id, client):LogLogGroupList = LogGroupList()LogGroup = LogLogGroupList.logGroupList.add()# Customize the source of the file to be uploaded this time, which will be used as a metadata field in logsLogGroup.filename = "pyXXXX.log"# Customize the address source for this upload, which will be used as a metadata field in logsLogGroup.source = "192.XX.XX.XX"# Custom metadata fieldLogTag = LogGroup.logTags.add()LogTag.key = "key"LogTag.value = "value"Log = LogGroup.logs.add()Log.time = int(round(time.time() * 1000000)) # Get current time as timestamp# Define the content of logsContent = Log.contents.add()Content.key = "Hello"Content.value = "World"try:request = client.put_log_raw(topic_id, LogLogGroupList)print("Request ID:", request.get_request_id())except LogException as e:print("Error uploading log:", e)except Exception as e:# Extend exception handling to cover other potential issues such as network connectivityprint("Unexpected Error:", e)if __name__ == '__main__':# Fill in domain information. Completion guide: https://www.tencentcloud.com/document/product/614/18940#.E5.9F.9F.E5.90.8D. See the domain name in the Log upload via API Tab of the link.endpoint = 'https://ap-xxxxxxxx.cls.tencentcs.com'# Fill in cloud API Key Information. For key information acquisition, please visit: https://console.tencentcloud.com/cam/capi# Please ensure the associated account of the key has appropriate log upload permissions. Permission configuration guide: https://www.tencentcloud.com/document/product/614/45004#.E4.BD.BF.E7.94.A8-api-.E4.B8.8A.E4.BC.A0.E6.95.B0.E6.8D.AE# This example retrieves from environmental variable. Environment variable configuration guide: https://www.tencentcloud.com/document/product/614/113851?from_cn_redirect=1accessKeyId = os.environ["TENCENTCLOUD_SECRET_ID"]accessKey = os.environ["TENCENTCLOUD_SECRET_KEY"]if not accessKeyId or not accessKey:raise EnvironmentError("Error: TENCENTCLOUD_SECRET_ID or TENCENTCLOUD_SECRET_KEY is not set")# Fill in the topic IDtopic_id = '59XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'if not endpoint.strip():raise ValueError("The endpoint cannot be left empty. Please enter a valid CLS domain name.")if not topic_id.strip():raise ValueError("The topic_id cannot be left empty. Please enter a valid log topic ID.")# Build a log clientclient = LogClient(endpoint, accessKeyId, accessKey)# Call the upload functionupload(topic_id, client)
フィードバック