All Products
Search
Document Center

Function Compute:Log

Last Updated:Apr 01, 2026

Waktu proses Go Function Compute mendukung beberapa metode pencetakan log. Setiap metode menghasilkan format output berbeda—pilih sesuai kebutuhan debugging dan observabilitas Anda.

MetodeOutput mencakupPaling cocok untuk
context.GetLogger()Tingkat log, ID permintaan, timestamp, nama file, nomor barisDebugging terstruktur dengan konteks lengkap
paket logHanya timestampOutput sederhana dengan pengaturan minimal
paket fmt atau library yang menulis ke stdout/stderrBervariasi tergantung libraryFormat log kustom

Cetak log

Gunakan context.GetLogger()

context.GetLogger() menghasilkan baris log terstruktur yang mencakup tingkat log, ID permintaan, timestamp, nama file, dan nomor baris, sehingga memudahkan korelasi log dengan pemanggilan fungsi tertentu.

Tingkat log yang didukung: Debug/Debugf, Info/Infof, Warn/Warnf, Error/Errorf.

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)
}

Setiap baris log mengikuti format berikut: {timestamp} {requestId} [{LEVEL}] {filename}:{linenum}: {message}. Batas pemanggilan juga dicatat:

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

Gunakan paket log

Paket standar Go log menulis ke stdout dan menambahkan timestamp di awal setiap baris.

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-75678db61831

Rangkuman eksekusi

Untuk setiap pemanggilan, waktu proses Go mencatat baris Start, baris End, dan rangkuman eksekusi.

fc-go-execution-info
ParameterDeskripsi
Request IDID unik dari permintaan pemanggilan.
Code Check ValueKode verifikasi dari paket kode yang digunakan untuk memanggil fungsi.
Function Execution TimeWaktu yang benar-benar dibutuhkan oleh penanganan fungsi untuk berjalan.
Function Billing TimeDurasi waktu yang dikenai biaya saat memanggil fungsi.
Function MemoryJumlah memori yang dialokasikan untuk fungsi.
Actual Used MemoryJumlah maksimum memori yang digunakan selama pemanggilan.