You can store function invocation logs in Log Service, and then perform operations such as code debugging, fault analysis, and data analysis based on the logs. This topic describes how to configure the logging feature in the Function Compute console. This topic also describes how to view function invocation logs.

Background information

Log Service is a one-stop log data management service developed by Alibaba Cloud. Before you use Log Service to store function invocation logs, you must specify a Log Service project and a Log Service Logstore for your service in Function Compute and authorize the service to access Log Service. Function invocation logs are stored in the specified Logstore. All function invocation logs that are generated for a service in the same region are stored in the same Logstore.

Billing

When you configure the logging feature, Function Compute may automatically create a Log Service project and a Log Service Logstore that are charged based on the pay-as-you-go billing method. For more information about billing, see Billable items.

Prerequisites

Configure the logging feature

  1. Log on to the Function Compute console.
  2. In the left-side navigation pane, click Services and Functions.
  3. In the top navigation bar, select the region where the service resides.
  4. On the Services page, find the desired service. Click Configure in the Actions column.
  5. In the Log Settings section, configure the parameters that are described in the following table and click Save.
    Log Settings section
    Note
    • If you have enabled the logging feature when you create the service, the Log Service Project parameter is automatically set to the Log Service project that is automatically created by Function Compute and starts with aliyun-fc-cn-<region_id>. The Logstore parameter is automatically set to the default Log Service Logstore under the specified Log Service project. If a Log Service project has been automatically created by Function Compute in the current region before, the system uses this Log Service project and does not create another project.
    • If the logging feature is disabled when you create the service, you must manually configure the Log Service Project and Logstore parameters.
    Parameter Required Description
    Logging Yes Specifies whether to enable the logging feature. Valid values:
    • Enable: enables the logging feature. After the logging feature is enabled, you can view function invocation logs. The function invocation logs are stored in Log Service for queries.
      Note To enable the logging feature, you must select a role that is authorized to access Log Service in the Role Settings section. For more information about roles, see Grant Function Compute permissions to access other Alibaba Cloud services.
    • Disable: disables the logging feature.
    Log Service Project Yes Specifies the Log Service project that stores function invocation logs.
    Logstore Yes Specifies the Log Service Logstore that stores function invocation logs.
    Log Rotation Rules No Specifies whether to split function invocation logs. Valid values:
    • Enable: If you select this option, Function Compute splits function invocation logs and stores the generated log blocks in Log Service. Function Compute splits function invocation logs by checking whether the log lines start with a date. For example, the log line that starts with 2021-10-10 is considered as the first line of a log block. The first line and the subsequent consecutive lines that do not start with a date in the log block are written to Log Service as a whole.
    • Disable: If you select this option, Function Compute does not split function invocation logs.
    Request-level Metrics No Specifies whether to enable the Insights feature of Function Compute for the service. If you enable the Insights feature, you can view the invocation metrics. Valid values:
    • Enable: If you select this option, invocation metrics for functions in the service are collected and delivered to the specified Logstore. You can use service logs and analysis logs to create a custom monitoring dashboard, which is a Grafana dashboard. For more information, see Create Grafana dashboards based on request-level metrics.
    • Disable: If you select this option, the Insights feature of Function Compute is disabled for the service. In this case, you cannot view the invocation metrics.
    Instance-level Metrics No Specifies whether to enable the collection of instance-level metrics. Valid values:
    • Enable: If you select this option, instance-level metrics are collected. Instance-level metrics are performance metrics of instances. The instances are monitored, and their performance metrics are collected in real time. The metrics are displayed to provide you with end-to-end monitoring and troubleshooting paths for the instances. For more information, see Instance-level metrics.
    • Disable: If you select this option, instance-level metrics are not collected.
Note You can also click Enable on the Logs tab on the details page of a function to enable the logging feature. Function Compute automatically creates a Log Service project that starts with aliyun-fc-cn-<region_id> and a default Logstore under the Log Service project. If a Log Service project has been automatically created by Function Compute in the current region before, the system uses this Log Service project and does not create another project.

View function invocation logs

To view the invocation logs of a function, click the Logs tab on the details page of the function.

Note You can change the version or alias of the function in the Version or Alias drop-down list in the upper part of the page to query the invocation logs of the specific version or alias.
You can view the function invocation logs on the Call Request List tab or the Search by Keyword tab. The following part describes the differences of the information displayed on the two tabs:
  • Call Request List tab
    The function invocation time, request ID, invocation result, instance ID, and function version and alias are displayed on the Call Request List tab. The following figure shows an example.Call Request List tab
    • Click the request ID of an invocation record to view the details of the request and logs.
    • Click the instance ID of an invocation record to view the resource usage of the instance, such as CPU utilization, memory usage, and network traffic.
    • Click View Logs in the Actions column that corresponds to an invocation record to view the log details. Click Advanced Logs in the Actions column to go to the Log Service console to query and analyze logs.
  • Search by Keyword tab
    The function invocation time, log content, and function version and alias are displayed on the Search by Keyword tab. The following figure shows an example. Click Log Context in the Actions column that corresponds to an invocation record to query the context of a log entry. Click Logstore in the Actions column to go to the Log Service console to query and analyze logs. Search by Keyword tab
    You can enter a keyword in the search box to query logs based on your requirements. For example, if you want to search for logs that contain hello world, you can enter hello world in the search box. All log entries that contain the specified keywords are returned. The following figure shows an example. For more information about the syntax of querying logs by keyword, see Search syntax. Search result

References

In addition to the Function Compute and Log Service consoles, you can also use Serverless Devs to configure the logging feature and query logs. For more information, see the following topics: