You can use the following statements in the program to output the log:
fmt.Println
fmt.Sprintf
For example, you can query the output in the function log by running the following code.
package main
import (
"context"
"fmt"
"github.com/tencentyun/scf-go-lib/cloudfunction"
)
type DefineEvent struct {
// test event define
Key1 string `json:"key1"`
Key2 string `json:"key2"`
}
func hello(ctx context.Context, event DefineEvent) (string, error) {
fmt.Println("key1:", event.Key1)
fmt.Println("key2:", event.Key2)
return fmt.Sprintf("Hello %s!", event.Key1), nil
}
func main() {
// Make the handler available for Remote Procedure Call by Cloud Function
cloudfunction.Start(hello)
}
Currently, the function logs can be uploaded to Tencent Cloud SCF. You can complete the configuration as instructed in Log Delivery Configuration.
You can search function execution logs on the log query page of the SCF or CLS console. For more information on how to query logs, see Log Search Guide.
Note:Function logs uploaded to logset and log topic can be both queried with a function configured.
Currently, the content output by simple log print statements in the function code will be recorded in the SCF_Message
field when it is delivered to CLS. For descriptions of CLS fields, see Log Delivery Configuration.
Currently, SCF supports adding custom fields to logs that are uploaded to CLS. The custom fields allow you to output business fields and relevant data to logs, and track them using the log search feature of CLS.
Note:
- If you need to query the key value of a custom field such as
SCF_CustomKey: SCF
, add a key-value index to the log topic for SCF log delivery as instructed in Configuring Indexes.- To avoid function log query failures caused by misuse of the index configuration, the default destination topic for SCF log delivery (prefixed with
SCF_LogTopic_
) does not support modifying the index configuration. You need to set the destination topic to custom delivery first and then update the log topic's index configuration.- After the index configuration is modified for a log topic, it will take effect only for newly written data.
If a function outputs a single-line log in JSON, the log will be parsed and uploaded to CLS in the field:value
format. Only the first layer will be parsed, and the remaining nested structure will be recorded as values.
Run the following codes and check results:
package main
import (
"context"
"fmt"
"github.com/tencentyun/scf-go-lib/cloudfunction"
)
type DefineEvent struct {
Key1 string `json:"key1"`
Key2 string `json:"key2"`
}
func hello(ctx context.Context, event DefineEvent) (string, error) {
m := map[string]string{"key1": "test value 1", "key2": "test value 2"}
data, _ := json.Marshal(m)
fmt.Println(string(data))
return fmt.Sprintf("hello world"), nil
}
func main() {
cloudfunction.Start(hello)
}
After using the above code to perform a test, you can run the following statement to search in Function Management > Log Query > Advanced Search:
Search for logs
After the test is written to CLS, you can find the key1
field in the log query as shown below:
Was this page helpful?