The Go runtime in Function Compute supports multiple logging methods, each producing output with a different set of fields. Choose the method that matches what you need when debugging function invocations.
Choose a logging method
| Method | Output includes | Best for |
|---|---|---|
context.GetLogger() | Log level, request ID, timestamp, file name, line number | Production debugging — correlate logs to a specific invocation |
log library (standard) | Timestamp | General-purpose logging |
fmt library / stdout / stderr | Raw message only | Simple output or custom log formatting |
Write logs with context.GetLogger()
This method tags every log line with the log level, request ID, timestamp, source file, and line number, making it the most useful method for tracing issues across invocations.
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)
}Each log line follows the format <timestamp> <requestId> [<LEVEL>] <file>:<line>: <message>. The runtime wraps each invocation with FC Invoke Start and FC Invoke End lines:
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-1d2728514129Write logs with the log library
Go's standard log library prepends a timestamp to each line.
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)
}Output:
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-75678db61831Execution summary
At the end of each invocation, the Go runtime appends an execution summary. The summary fields are:
Request ID — The unique ID of the invocation request.
Code Check Value — The verification code of the code package used for the invocation.
Function Execution Time — The amount of time that the function handler actually takes to run.
Function Billing Time — The duration used to calculate the bill for the invocation.
Function Memory — The memory allocated to the function.
Actual Used Memory — The peak memory consumed during the invocation.