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.
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.
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:
Log on to the Log Service console.
Click Create Project.
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.
After a project is created, create a Logstore and set the Logstore name.
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.
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.
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.
To download and configure the fcli tool, see fcli.
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:
~ fcli shell
Welcome to the Function Compute world. Have fun!
>>> mkl -p test-log-project -s test-log-store
Note: you have to pay at least 0.04 RMB/day for the log store resource. For the detail billing info please refer to:
Do you want to create the log store? [y/n]
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.
>>> grant serviceName
Please input the role name: #Name the role created in this step.
Please input the policy name: #Name the permission created in this step.
Permission grant scenarios:
1. Allow FC write function logs to your log store. #Select a permission.
2. Allow FC copy code from your OSS location.
Please input your choice [1-2]:
Please input the log project: test-log-project
Please input the log store: test-log-store
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.
# In the service directory
# By default, the most recent 1000 log entries generated in a day are output.
>>> logs myFunction
# A maximum of 5000 log entries generated in a minute can be output.
>>> logs -d 60 -c 5000 myFunction
# Output the last 100 lines of log entries.
>>> logs -t -c 100 myFunction
# Output log entries from 2018-01-22T18:00:00Z to 2018-01-22T19:00:00Z.
>>> logs -s 2018-01-22T18:00:00Z -e 2018-01-22T19:00:00Z myFunction