edit-icon download-icon

Function Compute logs

Last Updated: Aug 08, 2018

In Function Compute, function execution logs are stored in Log Service. You can use these logs for code debugging, fault analysis, data analysis, or other scenarios. To store function logs in Log Service, you must create a log project and Logstore for the service corresponding to the function, and grant service the permission to access Log Service. Function logs are collected to the Logstore that you create. The log data of all functions of the same service is collected to the same Logstore.

Contents

This topic describes how to configure a Logstore for a service and view logs by using the console or the command-line tool fcli. We recommend that you use the console to configure Logstores for services, and then use fcli to view the logs, because fcli displays logs in a more human-readable format.

Use Function Compute console

Configure Logstore for service in the console

Create log project and Logstore

Before configuring a Logstore, make sure that you have created a log project and a Logstore. If no log projects or Logstore are available, follow these steps:

  1. Log on to the Log Service console.

  2. Click Create Project.

  3. Set the project name and region. Note that project names in Log Service must be unique. The name and region metadata of the log project cannot be changed later.

  4. After a project is created, create a Logstore and set the Logstore name.

  5. Select the Logstore that you create. If a message indicating that index is not enabled for the Logstore is displayed, go to the upper-right corner and enable index.

    After the preceding steps, a log project and a Logstore have been created.

Configure log project and Logstore for Function Compute

To view function logs in Log Service, the following requirements must be met:

  • A log project and a Logstore have been configured for the function. The log data of all functions of the same service is collected to the same Logstore.

    You can configure a log project or Logstore either when creating or updating a service. For more information about how to create or update a service, see Basic operations.

  • The service in Function Compute has been granted the permission to access Log Service. For more information about service permissions, see Introduction.

View logs in the console

Create a default function for the service that has a log project and a Logstore, and output the hello world log entry. After your function is executed, the logs generated are output to the Logstore. You can view these logs in the Log Service console. Access the Logstore that you have configured and view the function execution logs.

Use fcli

To download and configure the fcli tool, see fcli.

Configure Logstore by using fcli

Create log project and Logstore

Run the fcli shell command to enter the interactive mode of fcli and run the mkl -p log-project -s log-store command to create a log project and a Logstore. For your actual scenario, change the test-log-project and test-log-store parameters to the required log project and Logstore names. An example is as follows:

  1. ~ fcli shell
  2. Welcome to the Function Compute world. Have fun!
  3. >>> mkl -p test-log-project -s test-log-store
  4. Note: you have to pay at least 0.04 RMB/day for the log store resource. For the detail billing info please refer to:
  5. https://www.aliyun.com/price/product#/sls/detail
  6. Do you want to create the log store? [y/n]
  7. y

Note: Log Service charges fees for reserving certain resources, which means that even if no log is generated, you must pay for the service. For more information, see Billing methods.

Configure log project and Logstore for Function Compute

  1. >>> grant serviceName
  2. Please input the role name: #Name the role created in this step.
  3. fc-logs-role
  4. Please input the policy name: #Name the permission created in this step.
  5. fc-logs-policy
  6. Permission grant scenarios:
  7. 1. Allow FC write function logs to your log store. #Select a permission.
  8. 2. Allow FC copy code from your OSS location.
  9. Please input your choice [1-2]:
  10. 1
  11. Please input the log project: test-log-project
  12. Please input the log store: test-log-store
  13. ...grant success

View logs using fcli

Run the logs command to view logs.

  • -c —count int: Sets the maximum number of rows of log entries to be returned. The default value is 1000.
  • -d —duration int: Indicates the time period measured from current time, in seconds, in which generated log entries are returned. The default value is from current time to 86400 seconds (that is, 24 hours).
  • -e —end string: Sets the time before which the log entries are generated. The format is UTC RFC3339. Example: 2017-01-01T01:02:03Z.
  • -s —start string: Sets the time from which log entries are generated. The format is UTC RFC3339. Example: 2017-01-01T01:02:03Z.
  • -t —tail: Outputs the most recent log entries in descending order beginning from line i.
  1. # In the service directory
  2. # By default, the most recent 1000 log entries generated in a day are output.
  3. >>> logs myFunction
  4. # A maximum of 5000 log entries generated in a minute can be output.
  5. >>> logs -d 60 -c 5000 myFunction
  6. # Output the last 100 lines of log entries.
  7. >>> logs -t -c 100 myFunction
  8. # Output log entries from 2018-01-22T18:00:00Z to 2018-01-22T19:00:00Z.
  9. >>> logs -s 2018-01-22T18:00:00Z -e 2018-01-22T19:00:00Z myFunction
Thank you! We've received your feedback.