Create a Logtail configuration

Updated at:

Call the CreateConfig operation to create a Logtail configuration.

Prerequisites

Parameters

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. See Manage projects.

config_detail

LogtailConfig

Yes

The details of the Logtail configuration. Supported methods include LogtailConfigGenerator, SeperatorFileConfigDetail, SimpleFileConfigDetail, FullRegFileConfigDetail, JsonFileConfigDetail, ApsaraFileConfigDetail, and SyslogConfigDetail.

This example uses the SimpleFileConfigDetail method to create a configuration for collecting simple text logs.

SimpleFileConfigDetail parameters

Parameter

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. See Manage Logstores.

configName

String

Yes

The Logtail configuration name must be unique within a project and cannot be changed after creation. For more information, see Logtail (legacy)

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 name of the log file. You can use a wildcard character (*).

localStorage

bool

No

Specifies whether to enable the local cache.

  • True (default): Logtail uses a 1 GB local cache when offline.

  • False: Disables the local cache.

enableRawLog

bool

No

Specifies whether to upload raw logs. Valid values:

  • True: Uploads raw logs.

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

topicFormat

String

Yes

The method to generate a log topic. This parameter must be used together with topicType. Valid values and the topicType value that each of them must be paired with:

  • none: Does not generate a log topic. Pair this value with topicType=none.

  • group_topic: Uses the topic of the machine group to which the configuration is applied. Pair this value with topicType=machine_group_topic.

  • File path regular expression: Uses a part of the log file path as the log topic. Example: /var/log/(.*).log. Pair this value with topicType=filepath.

  • Custom format: Uses a custom log topic format. Pair this value with topicType=custom.

If you do not specify topicFormat, the call fails and the following error message is returned: inputDetail.topicFormat is of error type or lacking. For more information, see Log topics.

fileEncoding

String

No

The file encoding of the log. Valid values: utf8 and gbk.

maxDepth

int

No

The maximum directory monitoring depth. Valid values: 0 to 1,000. A value of 0 limits monitoring to the current directory.

preserve

bool

No

The timeout policy for inactive log files. A log file that is not updated within a specific period times out. Valid values:

  • True (default): The log file never times out.

  • False: Logtail stops monitoring a log file if it is not updated for 30 minutes.

preserveDepth

String

No

The maximum directory depth to which the timeout policy applies. This parameter is required if you set preserve to False. Valid values: 1 to 3.

filterKey

List

No

The field keys to use for filtering logs. Logtail collects a log entry only if a field's value matches the corresponding regular expression specified in the filterRegex parameter.

filterRegex

String

No

The regular expression that corresponds to filterKey. The number of elements in filterRegex must be the same as the number of elements in filterKey.

Response parameters

For information about the response parameters, see Create a Logtail configuration.

Sample code

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

# This example obtains 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 for Simple Log Service.
endpoint = "cn-hangzhou.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 configuration for collecting logs from simple text files.
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