Configure a logger

更新时间:
复制 MD 格式

Alibaba Cloud SDK V1.0 for Go lets you configure a logger on an SDK client to record logs of each API call.

Logger configuration

Parameters

Parameter

Description

level

The log prefix. Set this to a log level such as info, debug, or warn. Default value: info.

channel

The log channel. Default value: AlibabaCloud.

out

The log output destination. The value must be an object that implements the io.writer interface.

templete

The template that controls the log output format. Default value: `{time} {channel}: "{method} {uri} HTTP/{version}" {code} {cost} {hostname}`. For more information, see the Variables that can be configured in the template section of this topic.

Sample code

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.

Sample log

[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

Variables that can be configured in the template

Variable

Description

{channel}

The log channel.

{host}

The request host.

{time}

The timestamp in ISO 8601 format, displayed in GMT.

{method}

The request method.

{uri}

The request URI.

{version}

The protocol version.

{target}

The request target (path and query).

{hostname}

The hostname of the request target.

{code}

The response status code, if available.

{error}

The error message, if any. Omitted when no error occurs.

{req_headers}

The request headers.

{res_headers}

The response headers.

{pid}

The process ID.

{cost}

The request duration.

{start_time}

The request start time.

{res_body}

The response body.