Call the CreateConfig API to create a Logtail configuration.
Prerequisites
Simple Log Service is activated. For more information, see Activate Simple Log Service.
Simple Log Service SDK for Python is initialized. For more information, see Initialize Simple Log Service SDK for Python.
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: This example uses the |
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:
|
logPath | String | Yes | The absolute path of the log file. |
filePattern | String | Yes | The log file name. Wildcard characters |
localStorage | bool | No | Specifies whether to enable local cache.
|
enableRawLog | bool | No | Specifies whether to upload raw logs. Valid values:
|
topicFormat | String | No | The method used to generate a topic. Valid values:
For more information, see Log topics. |
fileEncoding | String | No | The File Encoding format of the log. Valid values: |
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:
|
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 0References
The following APIs are available to manage Logtail configurations:
For more sample code, see Alibaba Cloud Simple Log Service SDK for Python on GitHub.