Function Compute integrates with Simple Log Service. After you configure logging for a function, Function Compute automatically collects logs and sends them to a specified Logstore. You can view per-request, instance, or function logs in the Function Compute. You can also use the log analysis features in the Simple Log Service to run custom queries.
Background information
To store function logs in Simple Log Service, you must configure a project and Logstore for the function's service and grant that service the required permissions. Function Compute writes logs to the specified Logstore. All logs from functions within the same service and region are delivered to the same Logstore.
Billing
Function Compute does not charge additional fees for configuring the logging feature. Logs are delivered to Simple Log Service, and you are charged by Simple Log Service. For more information, see Billing overview.
Configuration
Function Compute console
Prerequisites
-
Function Compute
-
Optional: Simple Log Service (SLS)
Procedure
Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
In the top navigation bar, select a region. On the Services page, find the desired service and click Configure in the Actions column.
-
In the Log Settings section of the service details page, configure the following parameters and click Save.
Note-
If you enable the logging feature when you create a service, the Function Compute console automatically creates a project that is prefixed with
aliyun-fc-cn-<region_id>and a default Logstore within that project. Only one such project is created per region. If an auto-created project already exists in the region, Function Compute uses it. -
If you do not enable the logging feature when you create the service, you must manually select a custom project and Logstore when you update the service.
-
The console automatically enables the log segmentation rule, request-level metrics, and instance-level metrics, and creates all indexes required to query logs.
Parameter
Required
Description
Logging
Yes
Specifies whether to enable the logging feature. Valid values:
-
Enable: When enabled, Function Compute sends logs to your project. You can then query logs in the Function Compute console or the Simple Log Service console to trace and troubleshoot issues.
NoteWhen you enable the logging feature, you must select a role that has permissions to access Simple Log Service in the Role Configuration section on the current page. For more information about service roles, see Grant Function Compute permissions to access other Alibaba Cloud services.
-
Disable: When disabled, Function Compute does not persist function logs, which makes tracing and troubleshooting difficult.
Configuration mode
Yes
Select Automatic Configuration or Custom Configuration. If you select Custom Configuration, the project and Logstore parameters are required.
Project
Yes
The destination project for function invocation logs.
Logstore
Yes
The destination Logstore for function invocation logs.
Log segmentation rule
No
Specifies whether to enable a log segmentation rule. Valid values:
-
Enable: When enabled, Function Compute splits logs into segments based on the specified rule and writes them to Simple Log Service one by one.
The default rule is
^.{0,2}\d{4}-\d{2}-\d{2}, which matches dates in thexxxx-xx-xxformat, wherexrepresents a digit. This rule splits logs based on whether a line starts with a date. For example, if a log line starts with 2023-10-10, it is treated as the first line of a new log entry. Function Compute then combines this line and all subsequent lines that do not start with a date into a single log entry and sends it to Simple Log Service. -
Disable: If disabled, Function Compute splits logs by the newline character (
\n) by default.
Request-level metrics
No
Specifies whether to send request-level metrics to the Logstore. We strongly recommend enabling this feature.
Valid values:
-
Enable: When enabled, Function Compute sends metrics for each function invocation to your specified Logstore. These metrics include duration, memory usage, errors, cold start status, and the duration of each cold start phase. Function Compute uses these metrics to display a request list in the invocation logs. You can also use these metrics to filter requests, such as filtering for all failed requests or all requests with a cold start.
-
Disable: If disabled, you cannot view detailed information about request executions.
Instance-level metrics
No
Specifies whether to enable instance-level metrics. Valid values:
-
Enable: When enabled, Function Compute sends metric data for function instances to your specified Logstore. You can view performance metrics for each function instance, such as CPU usage, memory usage, and network I/O. Function Compute uses this data to provide instance-level observability and an end-to-end path for monitoring and troubleshooting function instances. For more information, see Instance-level metrics.
-
Disable: If disabled, you cannot view detailed instance metrics.
-
Serverless Devs
Prerequisites
Procedure
-
Create a code directory with the following structure.
. ├── code │ └── index.js └── s.yamlThe
logConfigfield in the following example is used to configure logging. Replace the values ofprojectandlogstorewith your existing ones. You can also setlogConfig: autoto allow Function Compute to automatically create a project and a Logstore.edition: 1.0.0 name: hello-world-app access: "default" vars: region: "cn-hangzhou" service: name: "hello-world-service" description: 'hello world by serverless devs' logConfig: # Replace the values of project and logstore with your actual ones. You can also set logConfig to auto to have a project and Logstore automatically created. project: 'your-project' logstore: 'your-logstore' enableRequestMetrics: true enableInstanceMetrics: true role: 'acs:ram::<accountID>:role/aliyunfcdefaultrole' services: helloworld: component: fc props: region: ${vars.region} service: ${vars.service} function: name: "hello-world" description: 'hello world by serverless devs' runtime: nodejs14 codeUri: ./code handler: index.handler memorySize: 128 timeout: 60 -
Run the following command to deploy the application.
sudo s deploy -yAfter the command runs successfully, log on to the Function Compute console to verify that the function was deployed and the logging feature was enabled.
View invocation logs
On the function details page, click the Logs tab to view invocation logs for the current function.
You can switch between versions or aliases at the top of the page to query logs for a specific version or alias.
You can view function invocation logs on both the Request List and Search by Keyword tabs. They differ as follows:
-
Request List
This tab lists information about function invocations, such as the invocation time, request ID, result, instance ID, and function version or alias. The list includes the following columns: Invocation End Time, Request ID, Result, Instance ID, Version/Alias, Duration, Memory Usage, and Actions. For requests that involve a cold start, Cold Start is indicated in the Duration column. The Actions column also provides a Trace link.
-
You can click a request ID to view request details and log details.
-
You can click an instance ID to view the instance's metrics, such as CPU usage, memory usage, and network traffic.
-
In the Actions column, click View Logs to view log details, or click Advanced Logs to navigate to the Simple Log Service console to query and analyze logs.
-
-
Search by Keyword
This tab displays information such as the invocation time, log content, version, and alias. You can click Log Context in the Actions column to view the context for that log entry. You can also click Logstore in the Actions column to navigate to the Simple Log Service console to query and analyze logs. The query results are displayed in a table with four columns: Time, Version/Alias, Content, and Actions. The Content column shows function invocation logs, including messages such as
FC Invoke Start RequestId,[INFO] hello worldoutput, and initialization information such asFunctionCompute python3 runtime inited.. The Actions column provides Log Context and Logstore links for further investigation.You can enter keywords in the search box to find specific log entries. For example, to search for logs that contain the text
hello world, enter the keywordhello worldin the search box. The search results will show all log lines that contain this keyword. For more information about the query syntax, see Query and search syntax.
References
You can also configure logging for a function by using an SDK or calling an API operation. For more information, see CreateService and LogConfig.