A logger is used to record logs of each API call and is configured by using an SDK client in Alibaba Cloud SDK V1.0 for Go. This topic describes how to configure a logger.
Logger configuration
Parameters
Parameter | Description |
level | The prefix of the log. We recommend that you set the value to a log level, such as info, debug, and warn. Default value: info. |
channel | The data source of the log. Default value: AlibabaCloud. |
out | The output path of the log. The value must be an object that can implement the io.writer interface. |
templete | The log template that is used to configure the log content. 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.comVariables that can be configured in the template
Variable | Description |
{channel} | The data source of the log. |
{host} | The host that sends the request. |
{time} | The time that follows the ISO 8601 standard and is displayed in GMT. |
{method} | The request method |
{uri} | The request URI. |
{version} | The protocol version. |
{target} | The request address, which is composed of the path parameter and the query parameter in the URI. |
{hostname} | The name of the host that sends the request. |
{code} | The status code of the response. If no status codes are returned, this variable is not displayed. |
{error} | The error message. If no error messages are returned, this variable is not displayed. |
{req_headers} | The request headers |
{res_headers} | The response headers. |
{pid} | The process ID. |
{cost} | The duration of the request. |
{start_time} | The start time of the request. |
{res_body} | The response body. |