Function Compute integrates with Simple Log Service. After you configure logging for a function, Function Compute automatically collects logs and delivers them to a specified Logstore in a project. You can view individual request logs, instance logs, and function logs in the Function Compute console. You can also use the log analysis features in the Simple Log Service console to run custom queries.
Background
Simple Log Service is a one-stop service for log data that provides a full range of capabilities, including log collection, storage, querying, analysis, visualization, and alerting. To store function logs in Simple Log Service, you must configure a project and a Logstore for your function and grant the function the necessary permissions to access the service. After the configuration is complete, Function Compute sends function logs to the specified Logstore.
Billing
Billing modes
Function Compute does not charge extra fees for the logging feature. However, Simple Log Service charges for log delivery and storage.
If a Logstore uses pay-by-feature billing, Simple Log Service charges for storage, read traffic, requests, data processing, and data shipping after Function Compute sends logs to it. For more information, see Pay-by-feature billing items.
If a Logstore uses pay-by-data-volume billing, Simple Log Service charges based on factors such as the volume of raw data written after Function Compute sends logs to it. For more information, see Pay-by-data-volume billing items.
To change the billing mode of a Logstore, see Manage a Logstore.
Stop billing
If you need to stop billing, first disable the logging feature in the Function Compute console. Then, go to the Simple Log Service console to delete the Logstore or delete the project.
This irreversible action permanently deletes all existing data and stops Function Compute from delivering future logs to Simple Log Service. Back up any important log data before you proceed.
Enable logging for a function
Console
Prerequisites
Function Compute
Optional: Simple Log Service
Procedure
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.
On the function details page, click the Configuration tab. Click Modify next to Advanced Settings. In the Advanced Settings panel, find the Logs section, configure the parameters, and then click Deploy.
NoteThe logging feature is enabled by default when you create a function. The system automatically selects a Log Service Project that starts with
serverless-<region_id>and a default Logstore.The console automatically enables Log Rotation Rules, Request-level Metrics, and Instance-level Metrics, and creates all the required indexes for querying logs.
Parameter
Required
Description
Logging
Yes
Enables or disables the logging feature. Valid values:
Enable: When enabled, Function Compute sends logs to your project. You can then query and retrieve logs in the Function Compute console or the Simple Log Service console to track and troubleshoot issues.
Disable: When disabled, Function Compute does not persist function logs, making it difficult to track and troubleshoot issues.
Configuration Mode
Yes
Select Automatic Configuration or Custom Configuration. If you select Custom Configuration, the Log Service Project and Logstore parameters are required.
Log Service Project
Yes
The Simple Log Service project where function invocation logs are stored.
Logstore
Yes
The Logstore within the specified project where function invocation logs are stored.
Log Rotation Rules
No
Enables or disables the log segmentation rule. Valid values:
Enable: When enabled, Function Compute splits logs into segments based on the specified rule and writes each segment as a separate log entry to Simple Log Service.
The default log segmentation rule is
^.{0,2}\d{4}-\d{2}-\d{2}. This rule matches dates in thexxxx-xx-xxformat, wherexrepresents a digit. The rule splits logs based on whether a line starts with a date. For example, if a log line begins with 2023-10-10, it is treated as the first line of a new log entry. This line and all subsequent lines that do not start with a date are combined into a single log entry written to Simple Log Service.Disable: If disabled, Function Compute splits logs by the newline character
\nby default.
Request-level Metrics
No
Enables or disables request-level metrics. We strongly recommend that you enable this feature.
Valid values:
Enable: When enabled, metrics for each function invocation are sent to the selected Logstore. These metrics include execution duration, memory usage, execution errors, cold start occurrences, and the duration of each cold start phase. Function Compute uses these metrics to display a list of requests in the invocation logs. You can also use these metrics to filter requests, such as filtering for all failed requests or all requests that triggered a cold start.
Disable: You cannot view detailed information about request executions.
Instance-level Metrics
No
Enables or disables instance-level metrics. Valid values:
Enable: When enabled, metric data from function instances is sent to the selected Logstore. You can view instance-level performance metrics, such as CPU, memory, and network I/O usage. Function Compute provides instance-level observability based on this data, providing an end-to-end path for monitoring and troubleshooting function instances. For more information, see Instance-level metrics.
Disable: You cannot view detailed information about instance metrics.
Serverless Devs
Prerequisites
Procedure
Create a code directory with the following structure.
. ├── code │ └── index.js └── s.yamlThe following is an example of an s.yaml file.
In this example, the
logConfigfield is used to configure the logging feature. You must modify the Logstore and project values to match an existing project and Logstore in your account. You can also setlogConfig: auto, and Function Compute will automatically specify a Logstore and project for you.edition: 3.0.0 name: hello-world-app access: "default" vars: # Global variables region: "cn-hangzhou" resources: hello_world: component: fc3 # Component name props: region: ${vars.region} functionName: "start-nodejs-****" description: 'hello world by serverless devs' runtime: "nodejs16" code: ./code handler: index.handler memorySize: 128 timeout: 30 logConfig: # Modify the logstore and project to match your actual resources. You can also use logConfig:auto to have the system automatically create a project and Logstore. enableInstanceMetrics: true # Enable instance-level metrics. enableRequestMetrics: true # Enable request-level metrics. logBeginRule: DefaultRegex # The log segmentation rule. logstore: 'your-logstore' # Specify the Logstore name (must be created in advance). project: 'your-project' # Specify the project name (must be created in advance).Run the following command to deploy the application.
s deploy -yAfter the deployment succeeds, log on to the Function Compute console to verify that the function is deployed with the logging feature enabled.
Disable logging
After you disable this feature, Function Compute no longer collects or stores function execution logs, which can hinder troubleshooting and performance analysis. Proceed with caution.
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.
On the function details page, click the Configuration tab. Click Modify next to Advanced Settings. In the Advanced Settings panel, find the Logs section, turn off the Logging switch, and then click Deploy.
View invocation logs
On the function details page, click the Logs tab to query the invocation records of the current function.
On the function details page, click the Version Management or Alias Management tab. Select the version or alias that you want to query, and then click the Logs tab on its details page to view the corresponding logs.
You can view function invocation logs on the Invocation Requests tab or the Search by Keyword tab. The differences are described below:
Invocation Requests
This tab lists information such as the function invocation end time, request ID, result, instance ID, and the function's version or alias.
Click a Request ID to view the request and log details.
Click an Instance ID to view the resource usage of the instance, such as CPU utilization, memory usage, and network traffic.
You can click Request Log in the Actions column to view log details, or click Advanced Logs in the Actions column to go to the Simple Log Service console to query and analyze logs.
Search by Keyword
The list displays information such as the function invocation time, log content, and the function's version/alias. You can click Log Context in the Actions column to find the context logs for a log entry, or click Logstore in the Actions column to go to the Simple Log Service console to query and analyze logs.
You can enter keywords in the search box to find the log information you need. For example, to search for logs that contain the text
hello world, enterhello worldin the search box. The search results will include all log lines containing that keyword. For more information about the query syntax, see Query and search syntax.
Related documents
You can also configure logging for a function by using an SDK or making an API call. For more information, see CreateFunction.