Tüm Ürünler
Search
Belge Merkezi

Alibaba Cloud SDK:Günlük kaydediciyi yapılandırın

Son Güncelleme:Oct 07, 2026

Go için Alibaba Cloud SDK V1.0, her API çağrısının günlüklerini kaydetmek üzere bir SDK istemcisinde günlük kaydedici yapılandırmanıza olanak tanır.

Günlük kaydedici yapılandırması

Parametreler


Parameter


Description


level


Günlük öneki. Bu parametreyi info, debug veya warn gibi bir günlük düzeyine ayarlayın. Varsayılan değer: info.


channel


Günlük kanalı. Varsayılan değer: AlibabaCloud.


out


Günlük çıkış hedefi. Değer, io.writer arayüzünü uygulayan bir nesne olmalıdır.


templete


Günlük çıkış biçimini kontrol eden şablon. Varsayılan değer: {time} {channel}: "{method} {uri} HTTP/{version}" {code} {cost} {hostname}. Daha fazla bilgi için bu konudaki Şablonda yapılandırılabilecek değişkenler bölümüne bakın.

Örnek kod

import (
	"os"

	"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
	"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
	ecs "github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
)

func main() {
	config := sdk.NewConfig()
	// Use the AccessKey ID and AccessKey secret of the Resource Access Management (RAM) user.
	credential := credentials.NewAccessKeyCredential(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
	client, err := ecs.NewClientWithOptions("cn-hangzhou", config, credential)
	if err != nil {
		panic(err)
	}

	// Configure a logger in the SDK client. If you call this operation, the logging feature is enabled by default.
	// level: the log level. Default value: info.
	// channel: the data source of the log. Default value: AlibabaCloud.
	// out: the output path of the log. The value is an object that can implement the io.writer interface.
	// template: the log template. If you do not specify this parameter, the default value `{time} {channel}: "{method} {uri} HTTP/{version}" {code} {cost} {hostname}` is used.
	defaultLoggerTemplate := `{time} {channel}: "{method} {uri} HTTP/{version}" {code} {cost} {host}`
	client.SetLogger("info", "AlibabaCloud", os.Stdout, defaultLoggerTemplate)

	request := ecs.CreateDescribeRegionsRequest()
	client.DescribeRegions(request)
} 
client.OpenLogger()            // Enable the logging feature. If a logger does not exist in the SDK client, a default logger is created and configured.
client.SetTemplate(templete)   // Configure the log template. If a logger does not exist in the SDK client, a default logger is created and configured.

Örnek günlük

[INFO]client.go:574: 2024-06-17 17:01:25 AlibabaCloud: "POST /?AccessKeyId=LTAI****************&Action=DescribeRegions&Format=JSON&RegionId=cn-hangzhou&Signature=bC773M84gqLqk93PO8vd%2Bmx%2FAmM%3D&SignatureMethod=HMAC-SHA1&SignatureNonce=9f8f6dd196e9663a343150a81de0986f&SignatureType=&SignatureVersion=1.0&Timestamp=2024-06-17T09%3A01%3A21Z&Version=2014-05-26 HTTP/1.1" 200 4.1605408s ecs-cn-hangzhou.aliyuncs.com

Şablonda yapılandırılabilecek değişkenler


Variable


Description


{channel}


Günlük kanalı.


{host}


İstek sunucusu.


{time}


GMT olarak görüntülenen ISO 8601 biçimindeki zaman damgası.


{method}


İstek yöntemi.


{uri}


İstek URI'si.


{version}


Protokol sürümü.


{target}


İstek hedefi (yol ve sorgu).


{hostname}


İstek hedefinin ana bilgisayar adı.


{code}


Varsa yanıt durum kodu.


{error}


Varsa hata iletisi. Hata oluşmadığında atlanır.


{req_headers}


İstek üst bilgileri.


{res_headers}


Yanıt üst bilgileri.


{pid}


İşlem kimliği.


{cost}


İstek süresi.


{start_time}


İstek başlangıç zamanı.


{res_body}


Yanıt gövdesi.