This topic describes how to get started with Log Service SDK for Python to perform common operations. For example, you can create a project, create a Logstore, write log data, and query log data.

Prerequisites

Log Service SDK for Python is installed. For more information, see Install Log Service SDK for Python.

Sample code

In this example, the SLSQuickStart.py file is created. The sample code in this file shows how to call the required API operations to create a project, create a Logstore, create indexes, write log data, and query log data. Example:
from aliyun.log import LogClient, PutLogsRequest, LogItem, GetLogsRequest, IndexConfig
import time

# Configure the required information, such as an AccessKey pair, an endpoint, a project name, and a Logstore name. 
# The AccessKey pair that is used to access Log Service. For more information, see AccessKey pair. 
# An Alibaba Cloud account has permissions to call all API operations. Therefore, security risks may arise if you use the AccessKey pair of an Alibaba Cloud account. We recommend that you log on as a RAM user that has permissions to call API operations or perform routine O&M. 
accessKeyId = "your_access_id"  
accessKey = "your_access_key" 
# The endpoint of Log Service. For more information, see Endpoints. In this example, the endpoint of the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint. 
endpoint = "cn-hangzhou.log.aliyuncs.com" 

# Create a Log Service client.  
client = LogClient(endpoint, accessKeyId, accessKey)

# The name of the project. 
project_name = "aliyun-test-project"
# The name of the Logstore.
logstore_name = "aliyun-test-logstore"
# The query statement. 
query = "*| select dev,id from " + logstore_name
# Indexes. 
logstore_index = {'line': {
    'token': [',', ' ', "'", '"', ';', '=', '(', ')', '[', ']', '{', '}', '?', '@', '&', '<', '>', '/', ':', '\n', '\t',
              '\r'], 'caseSensitive': False, 'chn': False}, 'keys': {'dev': {'type': 'text',
                                                                             'token': [',', ' ', "'", '"', ';', '=',
                                                                                       '(', ')', '[', ']', '{', '}',
                                                                                       '?', '@', '&', '<', '>', '/',
                                                                                       ':', '\n', '\t', '\r'],
                                                                             'caseSensitive': False, 'alias': '',
                                                                             'doc_value': True, 'chn': False},
                                                                     'id': {'type': 'long', 'alias': '',
                                                                            'doc_value': True}}, 'log_reduce': False,
    'max_text_len': 2048}

# The from_time and to_time variables specify the start time and end time of a time range within which you want to query log data. The values of the variables are UNIX timestamps. 
from_time = int(time.time()) - 3600
to_time = time.time() + 3600

# Create a project. 
def create_project():
    print("ready to create project %s" % project_name)
    client.create_project(project_name, project_des="")
    print("create project %s success " % project_name)
    time.sleep(60)

# Create a Logstore. 
def create_logstore():
    print("ready to create logstore %s" % logstore_name)
    client.create_logstore(project_name, logstore_name, ttl=3, shard_count=2)
    print("create logstore %s success " % project_name)
    time.sleep(30)

# Create indexes. 
def create_index():
    print("ready to create index for %s" % logstore_name)
    index_config = IndexConfig()
    index_config.from_json(logstore_index)
    client.create_index(project_name, logstore_name, index_config)
    print("create index for %s success " % logstore_name)
    time.sleep(60 * 2)

# Write data to the Logstore. 
def put_logs():
    print("ready to put logs for %s" % logstore_name)
    log_group = []
    for i in range(0, 100):
        log_item = LogItem()
        contents = [
            ('dev', 'test_put'),
            ('id', str(i))
        ]
        log_item.set_contents(contents)
        log_group.append(log_item)
    request = PutLogsRequest(project_name, logstore_name, "", "", log_group, compress=False)
    client.put_logs(request)
    print("put logs for %s success " % logstore_name)
    time.sleep(60)


# Execute an SQL statement to query log data. 
def get_logs():
    print("ready to query logs from logstore %s" % logstore_name)
    request = GetLogsRequest(project_name, logstore_name, from_time, to_time, query=query)
    response = client.get_logs(request)
    for log in response.get_logs():
        for k, v in log.contents.items():
            print("%s : %s" % (k, v))
        print("*********************")


if __name__ == '__main__':
    # Create a project. 
    create_project()
    # Create a Logstore. 
    create_logstore()
    # Create indexes. 
    create_index()
    # Write data to the Logstore. 
    put_logs()
    # Execute an SQL statement to query log data. 
    get_logs()

For more information about sample code, see Alibaba Cloud Log Service SDK for Python.

Response

In this example, the following result is returned:

ready to create project aliyun-test-project
create project aliyun-test-project success
ready to create logstore aliyun-test-logstore
create logstore aliyun-test-project success
ready to create index for aliyun-test-logstore
create index for aliyun-test-logstore success
ready to put logs for aliyun-test-logstore
put logs for aliyun-test-logstore success
ready to query logs from logstore aliyun-test-logstore
dev : test_put
id : 0
*********************
dev : test_put
id : 1
*********************
dev : test_put
id : 2
*********************
dev : test_put
id : 3
*********************
........