All Products
Search
Document Center

Function Compute:Log management

Last Updated:Aug 08, 2024

This topic describes how to display logs in the Go runtime environment.

Display logs

To display logs, you can use the context.GetLogger() method, the methods in the log library or fmt library, or the methods that are used to write logs to standard output (stdout) or standard error message (stderr) in the log library.

Use the context.GetLogger() method to display logs

If you use this method to display logs, the displayed logs contain information such as the log level, request ID, time, file name, and line number. The following sample code provides an example:

package main

import (
    "context"

    "github.com/aliyun/fc-runtime-go-sdk/fc"
    "github.com/aliyun/fc-runtime-go-sdk/fccontext"
)

func HandleRequest(ctx context.Context) (string, error) {
    fctx, _ := fccontext.FromContext(ctx)

    fctx.GetLogger().Debug("this is Debug log")
    fctx.GetLogger().Debugf("Hi, %s\n", "this is Debugf log")

    fctx.GetLogger().Info("this is Info log")
    fctx.GetLogger().Infof("Hi, %s\n", "this is Infof log")

    fctx.GetLogger().Warn("this is Warn log")
    fctx.GetLogger().Warnf("Hi, %s\n", "this is Warnf log")

    fctx.GetLogger().Error("this is Error log")
    fctx.GetLogger().Errorf("Hi, %s\n", "this is Errorf log")

    return "Hello world", nil
}

func main() {
    fc.Start(HandleRequest)
}

The following logs are displayed:

FC Invoke Start RequestId: 1e9a87a5-fe0f-4904-a6f4-1d2728514129
2023-09-06T04:28:41.79Z d32c01bc-4397-4f52-a9ca-e374c28f96c1 [DEBUG] main.go:16: this is Debug log
2023-09-06T04:28:41.79Z d32c01bc-4397-4f52-a9ca-e374c28f96c1 [DEBUG] main.go:17: Hi, this is Debugf log
2023-09-06T04:28:41.79Z d32c01bc-4397-4f52-a9ca-e374c28f96c1 [INFO] main.go:19: this is Info log
2023-09-06T04:28:41.79Z d32c01bc-4397-4f52-a9ca-e374c28f96c1 [INFO] main.go:20: Hi, this is Infof log
2023-09-06T04:28:41.79Z d32c01bc-4397-4f52-a9ca-e374c28f96c1 [WARN] main.go:22: this is Warn log
2023-09-06T04:28:41.79Z d32c01bc-4397-4f52-a9ca-e374c28f96c1 [WARN] main.go:23: Hi, this is Warnf log
2023-09-06T04:28:41.791Z d32c01bc-4397-4f52-a9ca-e374c28f96c1 [ERROR] main.go:25: this is Error log
2023-09-06T04:28:41.791Z d32c01bc-4397-4f52-a9ca-e374c28f96c1 [ERROR] main.go:26: Hi, this is Errorf log
FC Invoke End RequestId: 1e9a87a5-fe0f-4904-a6f4-1d2728514129

Use a method in the log library to display logs

The logs displayed by using this method contain the date and time information. The following sample code provides an example:

package main

import (
    "log"

    "github.com/aliyun/fc-runtime-go-sdk/fc"
)

func HandleRequest() (string, error) {
    log.Println("hello world")
    return "hello world", nil
}

func main() {
    fc.Start(HandleRequest)
}

The following logs are displayed:

FC Invoke Start RequestId: a15f8f85-9ed3-47c9-a61c-75678db61831
2022/02/17 04:29:02.228870 hello world
FC Invoke End RequestId: a15f8f85-9ed3-47c9-a61c-75678db61831

Execution summary

The Go runtime environment records the Start and End lines and the execution summary for each function invocation. The following table describes the parameters in the execution summary.

Parameter

Description

Request ID

The unique ID of the invocation request.

Code Check Value

The verification code of the code package used for invoking the function.

Function Execution Time

The amount of time that the function handler actually takes to run.

Function Billing Time

The amount of time in which you are charged for the function invocation.

Function Memory

The memory allocated for the function.

Actual Used Memory

The maximum amount of memory that is actually used by the function.