All Products
Search
Document Center

Simple Log Service:Initialize the SLS Python SDK

Last Updated:Jun 03, 2026

LogClient is the SLS Python SDK client for managing projects, Logstores, writing logs, and reading logs. Initialize a LogClient instance to start sending requests, and modify the default configurations as needed.

Prerequisites

Initialize a LogClient

API

class LogClient(object):
    """ Construct the LogClient with endpoint, accessKeyId, accessKey.

    :type endpoint: string
    :param endpoint: log service host name, for example, ch-hangzhou.log.aliyuncs.com or https://cn-beijing.log.aliyuncs.com

    :type accessKeyId: string
    :param accessKeyId: aliyun accessKeyId

    :type accessKey: string
    :param accessKey: aliyun accessKey
    """

    __version__ = API_VERSION
    Version = __version__

    def __init__(self, endpoint, accessKeyId=None, accessKey=None, securityToken=None, source=None,
                 auth_version=AUTH_VERSION_1, region='', credentials_provider=None):

Request parameters

Variable

Type

Required

Description

Example

endpoint

String

Yes

The SLS endpoint. Obtain one from the Prerequisites section.

cn-hangzhou.log.aliyuncs.com

accessKeyId

String

Yes

  • If you use an AccessKey pair, this is the AccessKey ID of an Alibaba Cloud account or a RAM user, used to identify the caller. Configure access credentials.

    Warning

    An Alibaba Cloud account AccessKey pair has full permissions on all resources. If leaked, your system faces high risk. Use a RAM user AccessKey pair with minimum required permissions.

  • If you use an STS token, this is the `AccessKeyId` in `Credentials` returned by AssumeRole.

LTAI****************

accessKey

String

Yes

  • If you use an AccessKey pair, this is the AccessKey secret of an Alibaba Cloud account or a RAM user, used to verify the AccessKey ID. Configure access credentials.

  • If you use an STS token, this is the `AccessKeySecret` in `Credentials` returned by AssumeRole.

yourAccessKeySecret

securityToken

String

No

****************

source

String

No

Log source identifier. Defaults to the local server IP address if you leave this parameter empty or do not specify it.

10.105.214.**

auth_version

String

No

  • The authentication protocol version. Valid values: AUTH_VERSION_1 and AUTH_VERSION_4. Default value: AUTH_VERSION_1.

  • AUTH_VERSION_4 is more secure. If you use it, you must also specify `region`.

AUTH_VERSION_1

region

String

No

The region of the project. Supported regions.

cn-hangzhou

Examples

Initialize using an AccessKey pair (V4 signature)

# Import the sls package.
from aliyun.log import *
from aliyun.log.auth import AUTH_VERSION_4

# To obtain the AccessKey pair from environment variables, import the os library.
import os

# Obtain the AccessKey ID and AccessKey secret from environment variables.
access_key_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
access_key_secret = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')

# The endpoint of Simple Log Service.
endpoint = "yourEndpoint"

# Create a LogClient instance. Use V4 signature and specify the region based on your requirements. This example uses the China (Hangzhou) region.
client = LogClient(endpoint, access_key_id, access_key_secret, auth_version=AUTH_VERSION_4, region='cn-hangzhou')

Initialize using an AccessKey pair (V1 signature)

# Import the sls package.
from aliyun.log import *

# To obtain the AccessKey pair from environment variables, import the os library.
import os

# Obtain the AccessKey ID and AccessKey secret from environment variables.
access_key_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
access_key_secret = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')

# The endpoint of Simple Log Service.
endpoint = "yourEndpoint"

# Create a LogClient instance.
client = LogClient(endpoint, access_key_id, access_key_secret)

Initialize with STS

# Import the sls package.
from aliyun.log import *

# To obtain credentials from environment variables, import the os library.
import os

# Obtain the AccessKey ID, AccessKey secret, and STS token from environment variables.
access_key_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
access_key_secret = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
securityToken = os.environ.get('ALIBABA_CLOUD_STS_TOKEN')
# The endpoint of Simple Log Service.
endpoint = "yourEndpoint"

# Create a LogClient instance.
client = LogClient(endpoint, access_key_id, access_key_secret, securityToken)

What's next