Updated on 2025-09-07 GMT+08:00

LTS Python SDK

This LTS SDK provides a series of methods for reporting logs in the Python language, enabling you to directly report logs to LTS in encoding mode.

Transport Protocol

HTTPS

Prerequisites

Instructions

After a permission is modified, the new permission takes effect one day later. When logs are reported to LTS via the SDK, the time difference between the log reporting time you set and the current time must not exceed two days. Otherwise, the reported logs will be deleted by LTS.

Precautions

Hard-coded or plaintext AK and SK are risky. For security, encrypt your AK and SK, store them in the configuration file or as environment variables, and decrypt them during usage.

Installing the Python SDK

  1. Obtain the source code of the LTS Python SDK.

    git clone https://gitee.com/lordstar-habile/huaweicloud-lts-python-sdk.git

  2. Install dependencies.

    pip3 install requests
    pip3 install loguru
    pip3 install six

  3. Compile the code for reporting logs.

    You can customize the callback methods for successful and failed log reporting. The implementation method is as follows: Create a CallBack object, implement the invoke_on_success and invoke_on_failed methods, and use them directly when sending logs.

    class CallBack(object):
        def invoke_on_success(self, result):
            pass
    
        def invoke_on_failed(self, result):
            pass

    Example:

    import random
    import time
    from concurrent.futures import ThreadPoolExecutor
    from loguru import logger
    from producer.core.producer import Producer
    from producer.callback.call_back import CallBack
    from producer.model import lts_store
    from producer.model.config import Config
    from producer.utils import common
    def generate_random_str(str_len=16):
        """
        Generate a random string of a specified length.
        """
        random_str = ''
        base_str = 'ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789'
        length = len(base_str) - 1
        for i in range(str_len):
            random_str += base_str[random.randint(0, length)]
        return random_str
    def send_log_single_producer():
        thread_num = 100
        threads = []
        begin_ms = common.current_time_ms()
        submit_send_log_thread_pool = ThreadPoolExecutor(max_workers=thread_num, thread_name_prefix="send_log_thread")
        config = Config()
        config.endpoint = ""
        config.access_key = ""
        config.access_secret = ""
        config.region_id = ""
        config.project_id = ""
        producer = Producer.init_producer(config)
        producer.start_producer()
        group_id = ""
        stream_id = ""
    
        log_content = generate_random_str(1024)
        log = [log_content]
        labels = {"keyA": "valueA"}
        log_p = lts_store.generate_log(log, labels)
        for i in range(thread_num):
            new_thread = submit_send_log_thread_pool.submit(send_log_function, producer, group_id, stream_id, log_p)
            threads.append(new_thread)
        for t in threads:
            t.result()
        end_ms = common.current_time_ms()
        logger.info("end send log, cost [{}]ms", end_ms - begin_ms)
    def send_log_function(one_producer, group_id, stream_id, log_p):
        for i in range(2000):
            one_producer.send_log_with_callback(group_id, stream_id, log_p)
    class MyCallBack(CallBack):
        def invoke_on_success(self, result):
            logger.info("send log success")
        def invoke_on_failed(self, result):
            logger.error("send log success")
    if __name__ == '__main__':
        send_log_single_producer()
        time.sleep(1000)

Parameters

  • Parameters for producer config

    Parameter

    Description

    Type

    Mandatory

    Default Value

    projectId

    Project ID of the Huawei Cloud account.

    String

    Yes

    -

    ak

    AK of the Huawei Cloud account.

    String

    Yes

    -

    sk

    SK of the Huawei Cloud account.

    String

    Yes

    -

    region

    Region where LTS is deployed.

    String

    Yes

    -

    endpoint

    Destination to which logs are reported.

    String

    Yes

    -

    logGroup

    LTS log group ID.

    String

    Yes

    -

    logStream

    LTS log stream ID.

    String

    Yes

    -

  • Parameters of the GenerateLog method for generating logs The number of logs reported at a time is less than 4,096, and the size is less than 512 KB.

    Parameter

    Description

    Type

    Mandatory

    contents

    Content of a batch of logs.

    []string

    Yes

    labels

    Log label, in map format.

    map[string]string

    Yes

  • Parameters of the SendLog method for reporting logs
    Table 1 Parameters of the SendLog method

    Parameter

    Description

    Type

    Mandatory

    groupId

    Log group ID.

    String

    Yes

    streamId

    Log stream ID.

    String

    Yes

    log

    Log structure.

    class Log

    Yes

Obtaining Parameters

  • See the following region information for using Huawei Cloud hosts:
    Table 2 Regions

    Region Name

    Region

    Endpoint

    CN North-Beijing4

    cn-north-4

    https://lts-access.cn-north-4.myhuaweicloud.com:8102

    CN East-Shanghai1

    cn-east-3

    https://lts-access.cn-east-3.myhuaweicloud.com:8102

    CN South-Guangzhou

    cn-south-1

    https://lts-access.cn-south-1.myhuaweicloud.com:8102

    AP-Singapore

    ap-southeast-3

    https://lts-access.ap-southeast-3.myhuaweicloud.com:8102

    The SDK supports cross-cloud and local log reporting only in regions CN North-Beijing4, CN East-Shanghai1, and CN South-Guangzhou. It uses endpoint port 443.

  • To obtain a log group's ID, choose Log Management in the navigation pane of the LTS console and hover over the target log group's name.
  • To obtain a log stream's ID, click next to the corresponding log group and hover over the target log stream's name.