All Products
Search
Document Center

Function Compute:Configure the logging feature

Last Updated:Feb 18, 2024

Function Compute supports integration with Simple Log Service. After you configure the logging feature for a function, Function Compute automatically collects logs of the function and ships the logs to a specified Logstore in Simple Log Service. You can view logs based on function invocation requests, function instances, and functions in the Function Compute console. You can also use log analysis capabilities in the Simple Log Service console to retrieve logs as required.

Background information

Simple Log Service is a one-stop log data management service developed by Alibaba Cloud. To store function invocation logs in Simple Log Service, you must configure a log project and a Logstore in your function and grant the function permissions to access Simple Log Service. This way, function invocation logs are printed to the configured Logstore.

Billing rules

Function Compute does not charge fees for using the logging feature. However, Simple Log Service charges fees for logs that are shipped to the configured Logstore. For more information, see Billing overview.

Configure the logging feature

Use the Function Compute console

Before you start

Procedure

  1. Log on to the Function Compute console. In the left-side navigation pane, click Functions.

  2. In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.

  3. On the function details page, choose Configuration > Logs and click Modify. In the Logs panel, configure the following parameters and click Deploy.

    image

    Note
    • If you enable the logging feature when you create a function, the Function Compute console automatically creates a log project that starts with aliyun-fc-cn-<region_id> and a default Logstore in the log project. Only one of such log project can be created in each region. If the system detects that a log project has already been created in the region, the log project is directly used.

    • If you do not enable the logging feature when you create a function, you must manually select a log project and a Logstore when you update the function.

    • The system automatically enables Log Segmentation Rules, Request-level Metrics, and Instance-level Metrics, and creates all indexes that are required to query logs.

    Parameter

    Required

    Description

    Logging

    Yes

    Specifies whether to enable the logging feature. Valid values:

    • Enable: If you enable the logging feature, Function Compute imports logs to your log project. This way, you can query and retrieve logs in the Function Compute console or the Simple Log Service console to troubleshoot issues.

    • Disable: If you do not enable the logging feature, function logs are not persisted, which may make troubleshooting difficult.

    Configuration Mode

    Yes

    Valid values: Automatic Configuration and Custom Configuration. If you select Custom Configuration, you must specify the Log Service Project and Logstore parameters.

    Log Service Project

    Yes

    Specifies the Simple Log Service project that stores function invocation logs.

    Logstore

    Yes

    Specifies the Logstore that stores function invocation logs.

    Log Segmentation Rules

    No

    Specifies whether to split function invocation logs. Take note of the following items:

    • Enable: If you enable log segmentation, Function Compute splits logs into segments based on rules before writing the logs to Simple Log Service.

      The default log segmentation rule is ^.{0,2}\d{4}-\d{2}-\d{2}, which matches dates that conform to the xxxx-xx-xx format, where x represents numbers. Based on this rule, Function Compute splits logs based on whether the first lines of the logs contain a date. For example, if the first line of your log is 2023-10-10, the log is considered as the first line of a log and is written to Simple Log Service together with the subsequent logs that do not contain a date. All these logs are considered as one log by Simple Log Service.

    • Disable: If you disable log segmentation, Function Compute splits logs by using \n without using any special rule.

    Request-level Metrics

    No

    Specifies whether to import logs of request metrics to the Logstore. We recommend that you enable this feature.

    Take note of the following items:

    • Enable: If you enable this mode, the metrics for each invocation of a function, such as the execution time, memory usage, execution errors, cold start status, and the duration of each cold start step, are delivered to the selected Logstore. Function Compute lists requests in invocation logs based on these metrics. You can also filter requests based on the metrics, such as filtering all requests for which errors are reported or filtering requests in which cold starts occur.

    • Disabled: If you disable this mode, you cannot view the execution details of requests.

    Instance-level Metrics

    No

    Specifies whether to collect instance-level metrics. Take note of the following items:

    • Enable: If you enable this mode, metric data of function instances is shipped to the configured Logstore. This way, you can view the performance metrics of a function instance, such as its CPU, memory, and network I/O usages. Function Compute provides instance-level observability based on these metrics. This way, you can monitor function instances or troubleshoot issues in an end-to-end manner. For more information, see Instance-level metrics.

    • Disable: If you disable this mode, you cannot view the details of instance metrics.

Use Serverless Devs

Before you start

Procedure

  1. Create a code directory. The following sample code shows the directory structure:

    .
    ├── code
    │   └── index.js
    └── s.yaml

    The following sample code shows an s.yaml file.

    In this example, the logConfig section is used to configure the logging feature. You must specify the logstore and project fields based on your existing log project and Logstore. You can also directly set logConfig to auto. This way, Function Compute automatically creates a log project and a Logstore.

    edition: 3.0.0
    name: hello-world-app
    access: "default"
    vars: # The global variable.
      region: "cn-hangzhou"
    
    resources:
      hello_world:
        component: fc3 # The name of the component.
        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: # Specify the actually used project and Logstore. Alternatively, set this field to auto to allow the system to create a project and a Logstore for you.
            enableInstanceMetrics: true
            enableRequestMetrics:  true
            logBeginRule: DefaultRegex
            logstore: 'your-logstore'
            project: 'your-project'
  2. Run the following command to deploy the function:

    sudo s deploy -y

    After you run the code, you can log on to the Function Compute console to check whether the function is deployed and the logging feature is enabled.

View function invocation logs

On the function details page, click the Logs tab to query the invocation records of the function.

Note

On the function details page, you can also select a function version or alias from the Version or Alias drop-down list to go to the version or alias details page and then click the Logs tab to query logs.

You can view function invocation logs on the Invocation Requests tab or the Search by Keyword tab. The following items describe the differences between the information displayed on the two tabs:

  • Invocation Requests

    The function invocation time, request ID, invocation result, instance ID, and version or alias are displayed.

    • Click the request ID of an invocation record to view the request and log details.

    • 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 Simple Log Service console to query and analyze logs.

  • Search by Keyword

    The function invocation time, log content, and function version or alias are displayed. 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 Simple Log Service console to query and analyze logs.

    You can enter a keyword in the search box to query logs. For example, if you want to search for logs that contain text hello world, you can enter keyword hello world in the search box. The search results are all log lines that contain the keyword. For more information about the syntax, see Search syntax.