tencent cloud

Cloud Log Service

Python SDK Log Upload

Download
フォーカスモード
フォントサイズ
最終更新日: 2026-05-11 18:02:19
This article introduces how to quickly get started with the Python SDK of Cloud Log Service (CLS) to implement log upload. For more details on SDK usage, refer to the code repository tencentcloud-cls-sdk-python.

Prerequisites

Create and obtain TencentCloud API key information accessKeyId and accessKey. For key information acquisition, please visit API Key Management.
Please ensure the associated account has appropriate SDK log upload permission.

Preparing a Development Environment

Please refer to Python official website to download and install Python development environment or use conda to create a Python virtual environment.
The CLS Python SDK supports PyPy 2, 3 and Python 2.7, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 3.10, 3.11, 3.12, and 3.13 versions.
You can run the following command to check the current Python version information.
pip -V

Installing the Python SDK

Run the following command in the command line tool to install the Python SDK.
pip install tencentcloud-cls-sdk-python

Verifying SDK Installation

After installing the SDK, perform the following steps to verify the installed Python SDK.
pip show tencentcloud-cls-sdk-python
If the following message is returned, it signifies the installation is successful.
Name: tencentcloud-cls-sdk-python
Version: 1.0.4
Summary: TencentCloud cls log service Python client SDK
Home-page: https://github.com/TencentCloud/tencentcloud-cls-sdk-python
Author: farmerx

Request Parameters

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.

Upload Example Code for Logs

The following code uses the Python SDK as an example to demonstrate how to upload logs by calling the SDK. The sample code is shown below.
It is not recommended to store TencentCloud API key information in plaintext in project code. You can dynamically obtain API key information through environment variables. For detailed operations, please refer to configure environment variables.
# Import required libraries
import time
from tencentcloud.log.logclient import LogClient
from tencentcloud.log.logexception import LogException
from tencentcloud.log.cls_pb2 import LogGroupList
import os

def 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 logs
LogGroup.filename = "pyXXXX.log"
# Customize the address source for this upload, which will be used as a metadata field in logs
LogGroup.source = "192.XX.XX.XX"
# Custom metadata field
LogTag = 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 logs
Content = 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 connectivity
print("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=1
accessKeyId = 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 ID
topic_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 client
client = LogClient(endpoint, accessKeyId, accessKey)
# Call the upload function
upload(topic_id, client)

Conclusions

By following the steps above, you can quickly use the Tencent Cloud CLS Python SDK to upload logs. If you encounter any issues, please contact us to obtain assistance.



ヘルプとサポート

この記事はお役に立ちましたか?

フィードバック