Create a Logstore

Updated at:

Create a Logstore in a project by calling create_logstore. Configure parameters such as the Logstore name, data retention period, shard count, storage tiers, and Logstore type.

Prerequisites

Before you begin, ensure that you have:

Usage notes

Three parameters are permanent once set — plan carefully before calling create_logstore:

  • Logstore name: Must be globally unique within a project. Cannot be changed after creation. Allowed characters: lowercase letters, digits, hyphens (-), and underscores (_). Must start with a lowercase letter and end with a lowercase letter or digit. Length: 3 to 63 characters.

  • Logstore type: The mode parameter (standard or query) cannot be changed after creation.

  • Telemetry type: The telemetry_type parameter cannot be changed after creation. When set to Metrics, only a subset of parameters takes effect.

Method signature

def create_logstore(self, project_name, logstore_name,
                    ttl=30,
                    shard_count=2,
                    enable_tracking=False,
                    append_meta=True,
                    auto_split=True,
                    max_split_shard=64,
                    preserve_storage=False,
                    encrypt_conf=None,
                    telemetry_type='',
                    hot_ttl=-1,
                    mode=None,
                    infrequent_access_ttl=-1):

Parameters

Parameter

Type

Required

Default

Description

project_name

str

Yes

-

The project name. Projects isolate resources and control access. For more information, see Manage a project.

logstore_name

str

Yes

-

The Logstore name. Must be globally unique within the project. See Usage notes for naming rules.

ttl

int

No

30

The data retention period, in days. Valid values: 1 to 3650. Set to 3650 for permanent storage. This value equals the sum of hot_ttl, infrequent_access_ttl, and the Archive storage retention period.

shard_count

int

No

2

The initial number of shards. Valid values: 1 to 10. For more information, see MD5 value range.

enable_tracking

bool

No

False

Specifies whether to enable web tracking. When set to True, anonymous write access from the Internet is allowed without authentication, which may result in dirty data. For more information, see Use the web tracking feature to collect logs.

append_meta

bool

No

True

Specifies whether to record public IP addresses. When set to True, Simple Log Service appends __client_ip__ (the client public IP address) and __receive_time__ (a Unix timestamp of when the log was received) to the Tag field of collected logs.

auto_split

bool

No

True

Specifies whether to enable automatic sharding. For more information, see Shard capacity and Automatic sharding.

max_split_shard

int

No

64

The maximum number of shards after automatic splitting. Maximum value: 256. Takes effect only when auto_split is set to True.

encrypt_conf

dict

No

None

The encryption configuration. Includes enable, encrypt_type, and user_cmk_info. For more information, see EncryptConf and Data encryption.

telemetry_type

str

No

'' (None)

The telemetry data type. Valid values: None (default, log data) and Metrics (metric data). Cannot be changed after creation. When set to Metrics, only logstore_name, ttl, shard_count, auto_split, max_split_shard, and append_meta take effect.

hot_ttl

int

No

-1

The data retention period in the hot storage tier, in days. Minimum value: 7. Cannot exceed ttl. Set to -1 to keep all data in hot storage. For more information, see Configure intelligent tiered storage.

mode

str

No

None

The Logstore type. Cannot be changed after creation. Use standard (default) for full log analysis, real-time monitoring, and interactive queries. Use query for large datasets with long retention periods where SQL analysis is not required — index traffic costs are approximately 50% lower. See Logstore types for a detailed comparison.

infrequent_access_ttl

int

No

-1

The data retention period in the Infrequent Access (IA) storage tier, in days. Data must remain in IA storage for at least 30 days before it can move to Archive storage. For more information, see Configure intelligent tiered storage.

Logstore types

Type

Value

Features

Cost

Best for

Standard

standard

Full log analysis, real-time monitoring, interactive analysis

Standard index traffic fee

Real-time monitoring, interactive analysis, building observability systems

Query

query

High-performance queries only. Does not support SQL SELECT statements.

~50% lower index traffic fee

Large datasets, long retention periods (weeks or months), no analysis required

For a detailed comparison, see Appendix: logstore comparison by type.

Storage tier movement rules

Data moves between tiers based on the retention periods set for each tier:

Movement

Minimum retention before move

Hot storage to IA storage

7 days in hot storage

IA storage to Archive storage

30 days in IA storage

Hot storage to Archive storage

30 days in hot storage

Response

For more information, see the CreateLogStore API reference.

Examples

Both examples read credentials from environment variables and call create_logstore with different configuration levels.

Create a Logstore with default settings

Creates a Standard Logstore with the default configuration: 30-day retention, 2 shards, and automatic sharding enabled.

import os
from aliyun.log import LogClient

# Read credentials 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', '')

# Simple Log Service endpoint
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Initialize the client
client = LogClient(endpoint, access_key_id, access_key_secret)

# Project and Logstore names
project_name = "slsproject-1"
logstore_name = "logstore-1"

def main():
    res = client.create_logstore(project_name, logstore_name)
    res.log_print()

if __name__ == '__main__':
    main()

Sample response:

CreateLogStoreResponse:
headers: {'Server': 'AliyunSLS', 'Content-Length': '0', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Thu, 21 Nov 2024 08:20:24 GMT', 'x-log-time': '1732177224', 'x-log-requestid': '673EED4852EDC9797889****'}

Process finished with exit code 0

Create a Logstore with custom configuration

Creates a Standard Logstore with a 90-day retention period, 4 shards, intelligent tiered storage (30 days in hot storage), and automatic sharding enabled up to 128 shards.

import os
from aliyun.log import LogClient

access_key_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
access_key_secret = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')

endpoint = "cn-hangzhou.log.aliyuncs.com"
client = LogClient(endpoint, access_key_id, access_key_secret)

project_name = "slsproject-1"
logstore_name = "logstore-2"

def main():
    res = client.create_logstore(
        project_name,
        logstore_name,
        ttl=90,                  # Retain data for 90 days total
        shard_count=4,           # Start with 4 shards
        auto_split=True,         # Enable automatic sharding
        max_split_shard=128,     # Allow up to 128 shards after auto-split
        hot_ttl=30,              # Keep data in hot storage for 30 days
        mode='standard',         # Standard Logstore with full analysis
        append_meta=True,        # Record client IP and receive time
        enable_tracking=False    # Disable anonymous web tracking
    )
    res.log_print()

if __name__ == '__main__':
    main()

References