Call the CreateSavedSearch API to create a saved search.
Prerequisites
Simple Log Service is activated.
Simple Log Service SDK for Python is initialized.
Usage notes
In this example, the public endpoint for the Singapore region is used: ap-southeast-1.log.aliyuncs.com
.
If you want to access Simple Log Service by using other Alibaba Cloud services that reside in the same region as your project, you can use the private endpoint: ap-southeast-1-intranet.log.aliyuncs.com
.
For more information about the mappings between the endpoints and regions that are supported by Simple Log Service, see Endpoints.
Parameter description
def create_savedsearch(self, project, detail):
Request parameters
Parameter | Type | Required | Description |
project | 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 a project. |
detail | String | Yes | A JSON string containing the configuration information of the saved search. For example:
|
Description of the detail parameter:
Name | Type | Required | Description |
logstore | String | Yes | The name of the logstore. The logstore in Simple Log Service is used to collect, store, and query logs. See Manage a logstore. |
savedsearchName | String | Yes | The unique name of the saved search in the project, with a character length ranging from 3 to 63. |
searchQuery | String | Yes | The query statement or the query and analytic statements. For more information, see Overview of log query and analysis. |
topic | String | No | The topic of logs. The default value is an empty string. For more information, see Log topics. |
Response parameters
For information about the response parameters, see CreateSavedSearch.
Sample code
from aliyun.log import LogClient
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 = "proj-test-1"
detail = {
"filterjson": "{\"filterJson\":{\"children\":[]},\"filterQuery\":\"\"}",
"logstore": "logstore-1",
"savedsearchName": "savedsearch-1",
"searchQuery": "* | SELECT status, count(*) AS PV GROUP BY status",
"topic": ""
}
def main():
# Create a saved search.
res = client.create_savedsearch(project, detail)
res.log_print()
if __name__ == '__main__':
main()
Sample response
header: {'Server': 'AliyunSLS', 'Content-Length': '0', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Tue, 15 Oct 2024 07:30:19 GMT', 'x-log-time': '1728977419', 'x-log-requestid': '670E1A0B12508244EEF3****'}
Process finished with exit code 0
References
For more information about managing saved search APIs, see the following:
For more code samples, see aliyun-log-python-sdk.