全部产品
Search
文档中心

Function Compute:Manajemen Log

更新时间:Jun 24, 2025

Topik ini menjelaskan cara menampilkan log dalam lingkungan runtime Go.

Tampilkan log

Untuk menampilkan log, Anda dapat menggunakan metode context.GetLogger(), metode dalam pustaka log atau fmt, atau metode yang digunakan untuk menulis log ke output standar (stdout) atau pesan kesalahan standar (stderr) dalam pustaka log.

Use the context.GetLogger() method to display logs

Jika Anda menggunakan metode ini untuk menampilkan log, log yang ditampilkan mencakup informasi seperti tingkat log, ID permintaan, waktu, nama file, dan nomor baris. Contoh kode berikut memberikan ilustrasi:

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

Log berikut akan ditampilkan:

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

Log yang ditampilkan menggunakan metode ini mencakup informasi tanggal dan waktu. Contoh kode berikut memberikan ilustrasi:

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

Log berikut akan ditampilkan:

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

Lingkungan runtime Go mencatat baris Mulai dan Akhir serta ringkasan eksekusi untuk setiap pemanggilan fungsi. Tabel berikut menjelaskan parameter dalam ringkasan eksekusi.

Parameter

Deskripsi

ID Permintaan

ID unik dari permintaan pemanggilan.

Nilai Periksa Kode

Kode verifikasi dari paket kode yang digunakan untuk memanggil fungsi.

Waktu Eksekusi Fungsi

Jumlah waktu yang sebenarnya dihabiskan oleh penanganan fungsi untuk berjalan.

Waktu Penagihan Fungsi

Jumlah waktu di mana Anda dikenakan biaya untuk pemanggilan fungsi.

Memori Fungsi

Memori yang dialokasikan untuk fungsi.

Memori Digunakan Sebenarnya

Jumlah maksimum memori yang sebenarnya digunakan oleh fungsi.