Update a Logtail configuration

Updated at:

Update a Logtail configuration by using the UpdateConfig operation of the Python SDK.

Prerequisites

The following prerequisites are met:

Parameters

def update_logtail_config(self, project_name, config_detail):

Request parameters

Parameter

Type

Required

Description

project_name

String

Yes

Project name: A project is a resource management unit in Log Service that provides the primary boundary for multi-user isolation and access control. For more information, see Manage a Project.

config_detail

LogtailConfig

Yes

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

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

SimpleFileConfigDetail parameters

Parameter

Type

Required

Description

logstoreName

String

Yes

Logstore name: A Logstore is a unit in Log Service for collecting, storing, and querying logs. For more information, see Manage a Logstore.

configName

String

Yes

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

The name must meet the following requirements:

  • Can contain only 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 the asterisk (*) wildcard.

localStorage

bool

No

Specifies whether to enable local caching.

  • True (default): Enables a 1 GB local cache for use when Logtail is offline.

  • False: Disables the local cache.

enableRawLog

bool

No

Specifies whether to upload raw logs.

  • True: Uploads raw logs.

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

topicFormat

String

No

The method for generating log topics.

  • none (default): No log topic is generated.

  • group_topic: Uses the machine group topic as the log topic.

  • File path regular expression: Extracts part of the log file path as the log topic. Example: /var/log/(.*).log.

For more information, see Log topics.

fileEncoding

String

No

The file encoding format. Valid values: utf8 and gbk.

maxDepth

int

No

The maximum depth of directories to monitor. Valid values: 0 to 1000. A value of 0 indicates that only the current directory is monitored.

preserve

bool

No

Specifies whether to time out a log file that is not updated within a specified period.

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

  • False: If the log file is not updated within 30 minutes, it times out and is no longer monitored.

preserveDepth

String

No

If preserve is False, specifies the maximum directory depth for the timeout setting. Valid values: 1 to 3.

filterKey

List

No

The field used to filter logs. A log is collected only when this field matches the filterRegex regular expression.

filterRegex

String

No

The regular expression that corresponds to filterKey. The filterRegex and filterKey arrays must have the same number of elements.

Response parameters

For more information about the response parameters, see Update 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 Log Service. Replace it with the endpoint of the region where your Project resides.
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 = "*_json.log"


# Update the configuration for collecting simple text logs.
def main():
    try:
        config_detail = SimpleFileConfigDetail(logstore, configName, logPath, filePattern)
        res = client.update_logtail_config(project, config_detail)
        res.log_print()
    except Exception as error:
        print(error)


if __name__ == '__main__':
    main()

Sample response

UpdateLogtailConfigResponse:
headers: {'Server': 'AliyunSLS', 'Content-Length': '0', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Fri, 22 Nov 2024 07:26:50 GMT', 'x-log-time': '1732260410', 'x-log-requestid': '6740323AFB9C5EFB9980****'}

Process finished with exit code 0

Related documents