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

Print logs

To print logs, you can use the context.GetLogger() method, or use the methods in the log library or fmt library, or use other methods that write log libraries to stdout or stderr.

Use the context.GetLogger() method to print logs

If you use this method to print logs, the printed logs contain information such as the log level, request ID, time, file name, and line number. Sample code:

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 printed:

FC Invoke Start RequestId: 1e9a87a5-fe0f-4904-a6f4-1d2728514129
2022-04-20T04:28:41.79Z d32c01bc-4397-4f52-a9ca-e374c28f96c1 [DEBUG] main.go:16: this is Debug log
2022-04-20T04:28:41.79Z d32c01bc-4397-4f52-a9ca-e374c28f96c1 [DEBUG] main.go:17: Hi, this is Debugf log
2022-04-20T04:28:41.79Z d32c01bc-4397-4f52-a9ca-e374c28f96c1 [INFO] main.go:19: this is Info log
2022-04-20T04:28:41.79Z d32c01bc-4397-4f52-a9ca-e374c28f96c1 [INFO] main.go:20: Hi, this is Infof log
2022-04-20T04:28:41.79Z d32c01bc-4397-4f52-a9ca-e374c28f96c1 [WARN] main.go:22: this is Warn log
2022-04-20T04:28:41.79Z d32c01bc-4397-4f52-a9ca-e374c28f96c1 [WARN] main.go:23: Hi, this is Warnf log
2022-04-20T04:28:41.791Z d32c01bc-4397-4f52-a9ca-e374c28f96c1 [ERROR] main.go:25: this is Error log
2022-04-20T04: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 the log library to print logs

The logs printed by using this method contain the date and time information. Sample code:

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 printed:

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 records the Start and End lines and the execution summary for each function invocation.

fc-go-execution-info

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 that is used to invoke the function.
Function Execution Time The time that the function handler actually takes to run.
Function Billing Time The amount of time in which you are charged for invoking the function.
Function Memory The amount of memory allocated for the function.
Actual Used Memory The maximum amount of memory that is actually used by the function.