Configure the logging feature

更新时间:
复制 MD 格式

Function Compute integrates with Simple Log Service. After you configure logging for a function, Function Compute automatically collects logs and delivers them to a Logstore in a specified project. You can view per-request logs, instance logs, and function logs in the Function Compute console. You can also use the log analysis capabilities of the Simple Log Service console to run custom log queries.

Background information

Simple Log Service is an end-to-end service for log data. It provides full-stack capabilities for log collection, storage, querying, analysis, visualization, and alerting. To store function logs in Simple Log Service, configure a project and Logstore in your function and grant the function permission to access Simple Log Service. After configuration, function logs are written to the specified Logstore.

Billing notes

Billing method

Function Compute does not charge extra fees for enabling the logging feature. However, Simple Log Service charges for logs delivered to it.

  • If the Logstore uses the pay-by-feature billing method, Simple Log Service charges based on storage space, read traffic, number of requests, data transformation, and data shipping after Function Compute delivers logs to it. For more information, see Billing items for the pay-by-feature billing method.

  • If the Logstore uses the pay-by-ingested-data billing method, Simple Log Service charges based on raw data ingested after Function Compute delivers logs to it. For more information, see Billing items for the pay-by-ingested-data billing method.

To change the Logstore billing method, see Manage Logstores.

Stop billing

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.

Important

Deleting a Simple Log Service project or Logstore is irreversible. After deletion, logs can no longer be delivered to Simple Log Service, and existing data is permanently deleted. Back up important log data before deleting.

Enable logging for a function

Configure logging using the Function Compute console

Prerequisites

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, select the Configuration tab, click Advanced Settings to the right of Advanced Settings, locate the Logs option in the Advanced Settings panel, complete the log configuration by performing the following steps, and then click Deploy.

    Note
    • When you create a function, the logging feature is enabled by default, and the system automatically selects the Log Service Project that starts with serverless-<region_id> and the default Logstore.

    • The console automatically enables Log Rotation Rules, Request-level Metrics, and Instance-level Metrics, and creates all required indexes for log queries.

    Configuration item

    Required

    Description

    Logging

    Yes

    Specifies whether to enable Simple Log Service. Valid values:

    • Enable: After you enable this feature, Function Compute imports logs into your log project. You can query and retrieve logs in the Function Compute console and the Simple Log Service console to troubleshoot and identify issues.

    • Disable: Logging is disabled. Function logs cannot be persisted, and you cannot trace or locate issues.

    Configuration Mode

    Yes

    Select Automatic Configuration or Custom Configuration. If you select Custom Configuration, you must specify a Log Service Project and Logstore.

    Log Service Project

    Yes

    Specifies the target project for storing function invocation logs.

    Logstore

    Yes

    Specifies the target Logstore for storing function invocation logs.

    Log Rotation Rules

    No

    Specifies whether to enable log segmentation rules. Valid values:

    • Enable: When enabled, Function Compute will split logs according to log segmentation rules into multiple log segments and write them individually to Simple Log Service.

      The default rule is ^.{0,2}\d{4}-\d{2}-\d{2}, which matches dates in the format xxxx-xx-xx, where x is a digit. Logs are split 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 block. This line and subsequent lines without a leading date are written as one log entry.

    • Disable: Log segmentation is disabled. Logs are split using the default delimiter \n .

    Request-level Metrics

    No

    Specifies whether to import request metric logs into the Logstore. We strongly recommend enabling this feature.

    Valid values:

    • Enable: Metrics for each function invocation—such as execution time, memory usage, error status, cold start occurrence, and cold start step durations—are delivered to the Logstore you selected during logging configuration. The Function Compute invocation logs display a request list based on these metrics. You can also filter requests by metrics, such as all error requests or requests with cold starts.

    • Disable: You cannot view detailed request execution information.

    Instance-level Metrics

    No

    Specifies whether to enable instance-level metrics. Valid values:

    • Enable: When enabled, metric data from function instances is delivered to the Logstore you selected during log configuration. You can view performance metrics at the function instance level, such as CPU, memory, and network I/O usage. Function Compute uses instance-level metric data to provide observability at the instance granularity, giving you an end-to-end monitoring and troubleshooting path for function instances. For more information, see Instance-Level Metrics.

    • Disable: You cannot view detailed instance metrics.

Configure logging using Serverless Devs

Prerequisites

Install and configure Serverless Devs

Procedure

  1. Create a code directory with the following structure.

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

    In the example, the logConfig field configures logging. Replace logstore and project with an existing Logstore and project in your account. Alternatively, set logConfig: auto to let Function Compute automatically assign a project and Logstore.

    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: # Replace logstore and project with actual values, or use logConfig: auto to auto-create project and Logstore
            enableInstanceMetrics: true  # Enable instance-level metrics
            enableRequestMetrics:  true  # Enable request-level metrics 
            logBeginRule: DefaultRegex   # Log segmentation rule
            logstore: 'your-logstore'    # Specify Logstore name (must exist)
            project: 'your-project'      # Specify project name (must exist)
  2. Run the following command to deploy the application.

    sudo s deploy -y

    After successful deployment, log on to the Function Compute console to verify that the function is deployed and logging is enabled.

Disable logging

Note

After you disable logging, function execution logs are no longer collected or stored. This may affect troubleshooting and performance analysis. Proceed with caution.

  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, select the Configuration tab, click Modify on the right of Advanced Settings, in the Advanced Settings panel, find the Logs option, set the log feature toggle to Off, and click Deploy.

View invocation logs

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

Note

On the function details page, click the Version Management or Alias Management tab. Select a function version or alias, then click the Logs tab on the version or alias details page to view its logs.

You can view function invocation logs on either the Invocation Requests tab or the Search by Keyword tab. Differences:

  • Invocation Requests

    This tab lists function invocation end times, request IDs, invocation results, instance IDs, and function versions or aliases.

    • Click a Request ID to view request and log details.

    • Click an Instance ID to view instance metrics, such as CPU usage, memory usage, and network traffic.

    • In the Actions column, click Request Logs to view log details, or click Advanced Logs to go to the Simple Log Service console for log queries and analysis.

  • Search by Keyword

    This tab lists function invocation times, log content, and function versions or aliases. In the Actions column, click Log Context to view surrounding log entries, or click Logstore to go to the Simple Log Service console for log queries and analysis.

    Enter a keyword in the search box to find specific logs. For example, to find logs containing the text hello world, enter hello world in the search box. The results include all log lines containing this keyword. For more information about query syntax, see Query syntax and features.

References

You can also configure logging for a function by calling the SDK or API. For more information, see Create a function.