tencent cloud

Python SDK Log Upload
Last updated: 2025-11-07 17:40:37
Python SDK Log Upload
Last updated: 2025-11-07 17:40:37
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.
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 versions.
You can execute the following command to check 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 git+https://github.com/TencentCloud/tencentcloud-cls-sdk-python.git@v1.0.4

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

Variables
Type
Required
Description
endpoint
String
Yes
Domain information. Please see the domain name in the Log upload via API Tab of available domain.
accessKeyId
String
Yes
Cloud 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 Python SDK as an example to show how to complete log upload by calling the SDK. Sample code:
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)
if name == 'main':

# Fill in domain information. Completion guide: https://www.tencentcloud.com/document/product/614/18940?from_cn_redirect=1#.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/68374?from_cn_redirect=1#.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"]
# Fill in the topic ID
topic_id = '59XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
# Build a log client
client = LogClient(endpoint, accessKeyId, accessKey)
# Call the upload function
upload(topic_id, client)

Conclusions

Through these steps, you can quickly use the Tencent Cloud CLS Python SDK to complete log upload. If you encounter any issues, please contact us to get help.

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback