All Products
Search
Document Center

Simple Log Service:Create a Logtail configuration

Last Updated:Feb 25, 2025

Call the CreateConfig API to create a Logtail configuration.

Prerequisites

Parameter description

def create_logtail_config(self, project_name, config_detail):

Request parameters

Parameter

Type

Required

Description

project_name

String

Yes

The name of the project. The project in Simple Log Service is used to isolate the resources of different users and control access to specific resources. For more information, see Manage a project.

config_detail

LogtailConfig

Yes

Information about the Logtail configuration. The following methods are supported: LogtailConfigGenerator, SeperatorFileConfigDetail, SimpleFileConfigDetail, FullRegFileConfigDetail, JsonFileConfigDetail, ApsaraFileConfigDetail, and SyslogConfigDetail.

This example uses the SimpleFileConfigDetail method to generate a configuration object for simple text log collection.

Description of the SimpleFileConfigDetail parameter

Name

Type

Required

Description

logstoreName

String

Yes

The name of the Logstore. The logstore in Simple Log Service is used to collect, store, and query logs. For more information, see Manage a logstore.

configName

String

Yes

The Logtail configuration name must be unique within a project and cannot be changed after creation. For more information, see What is Logtail?

The name must meet the following requirements:

  • Can contain lowercase letters, digits, hyphens (-), and underscores (_).

  • Must start and end with a lowercase letter or a digit.

  • Must be 2 to 128 characters in length.

logPath

String

Yes

The absolute path of the log file.

filePattern

String

Yes

The log file name. Wildcard characters * are supported.

localStorage

bool

No

Specifies whether to enable local cache.

  • True (default): Use 1 GB of local cache when Logtail is offline.

  • False: Do not enable local cache.

enableRawLog

bool

No

Specifies whether to upload raw logs. Valid values:

  • True: Upload raw logs.

  • False (default): Do not upload raw logs.

topicFormat

String

No

The method used to generate a topic. Valid values:

  • none (default): Do not generate a log topic.

  • group_topic: Use the topic of the machine group to which the Logtail configuration is applied as the log topic.

  • Regular expression that is used to match a log file path: A part of the log file path is used as the topic of collected logs. For example, /var/log/(.*).log.

For more information, see Log topics.

fileEncoding

String

No

The File Encoding format of the log. Valid values: utf8 and gbk.

maxDepth

int

No

Set the Maximum Directory Monitoring Depth for logs. Valid values: 0 to 1000. The value 0 indicates that only the specified log file directory is monitored.

preserve

bool

No

Specifies the timeout strategy.

Valid values:

  • True (default): Never timeout.

  • False: If a log file is not updated within 30 minutes, Logtail considers it timed out and stops monitoring it.

preserveDepth

String

No

The maximum directory depth for timeout. This parameter is required when preserve is set to False. Valid values: 1 to 3.

filterKey

List

No

Only logs with field values matching the regular expression specified by filterRegex are collected.

filterRegex

String

No

The regular expression that corresponds to filterKey.

filterRegex and filterKey must have the same number of elements.

Response parameters

For information about the response parameters, see CreateConfig.

Sample code

from aliyun.log import LogClient
from aliyun.log.logtail_config_detail import SimpleFileConfigDetail
import os

# In this example, the AccessKey ID and AccessKey secret are obtained 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', '')

# Specify a Simple Log Service endpoint.
endpoint = "ap-southeast-1.log.aliyuncs.com"

client = LogClient(endpoint, access_key_id, access_key_secret)


project = "slsproject-1"
logstore = "logstore-1"
configName = logstore + "-stt1-logtail"
logPath = "/data/logs"
filePattern = "*.log"


# Create a simple text log collection configuration
def main():
    try:
        config_detail = SimpleFileConfigDetail(logstore, configName, logPath, filePattern)
        res = client.create_logtail_config(project, config_detail)
        res.log_print()
    except Exception as error:
        print(error)


if __name__ == '__main__':
    main()

Sample response

CreateLogtailConfigResponse:
headers: {'Server': 'AliyunSLS', 'Content-Length': '0', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Fri, 22 Nov 2024 06:40:40 GMT', 'x-log-time': '1732257640', 'x-log-requestid': '67402768AA8C117B143D****'}

Process finished with exit code 0

References