All Products
Search
Document Center

Simple Log Service:Enable the logging feature

Last Updated:Apr 02, 2024

This topic describes how to enable the logging feature in the Function Compute console. After you enable the logging feature, function execution logs are delivered to Simple Log Service.

Prerequisites

A service is created. For more information, see Create a service.

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 Services & Functions.

  2. In the top navigation bar, select a region. On the Services page, find the desired service and click Configure in the Actions column.

  3. In the Log Settings section of the Modify Service page, configure parameters and click Save.

    image

    Note
    • If you enable the logging feature when you create a service, the Function Compute console automatically creates a log project that is prefixed with aliyun-fc-cn-<region_id> and a default Logstore under the log project. Only one of such log project can be created in each region. If the system detects that such 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 the service, you must manually specify Simple Log Service Project and Logstore when you update the service.

    • 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

    Specify whether to enable the logging feature. Valid values:

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

      Note

      To enable the logging feature, you must use a role that is authorized to access Simple Log Service in the Role Settings section. For more information about service roles, see Grant Function Compute permissions to access other Alibaba Cloud services.

    • 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 Simple Log Service Project and Logstore.

    Simple Log Service Project

    Yes

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

    Logstore

    Yes

    Specify the Logstore that stores function invocation logs.

    Log Segmentation Rules

    No

    Specify whether to enable log segmentation. Valid values:

    • 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. x indicates digits. Function Compute splits logs based on the date condition. For example, a log that starts with 2023-10-10 is considered as the first line of a log entry and is written to Simple Log Service together with subsequent logs that do not contain a date. All these logs are considered as one log entry by Simple Log Service.

    • Disable: If you disable log segmentation, Function Compute splits logs by using \n by default.

    Request-level Metrics

    No

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

    Valid values:

    • 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 specified Logstore. Function Compute lists requests in invocation logs based on these metrics. You can also filter requests based on the metrics. For example, you can filter all requests for which errors are reported and requests in which cold starts occur.

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

    Instance-level Metrics

    No

    Specify whether to enable collection of instance-level metrics. Valid values:

    • Enable: If you enable this mode, metric data of function instances is shipped to the specified Logstore. This way, you can view the performance metrics of function instances, such as CPU, memory, and network I/O usage. Function Compute provides instance-level observability based on these metrics. 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 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 log project and Logstore. You can also set logConfig to auto to allow Function Compute automatically creates a log project and a Logstore for you.

    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: # Specify your project and Logstore. Alternatively, set this field to auto to allow the system to create a project and a Logstore for you.
          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
  2. Run the following command to deploy the application:

    sudo s deploy -y

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

What to do next

After function execution logs are delivered to Simple Log Service, you can query, analyze, download, ship, and transform the logs in the Simple Log Service console. You can also create alerts rules for the logs. For more information, see Common operations on logs of Alibaba Cloud services.