Create a Logtail configuration
Call the CreateConfig operation to create a Logtail configuration.
Prerequisites
-
Simple Log Service is activated. For more information, see Activate Simple Log Service.
-
The Simple Log Service SDK for Python is initialized. For more information, see Initialize Simple Log Service SDK for Python.
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 This example uses the |
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:
|
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.
|
enableRawLog | bool | No | Specifies whether to upload raw logs. Valid values:
|
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:
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: |
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:
|
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 0References
The following operations are available to manage Logtail configurations: