O Alibaba Cloud SDK V1.0 for Go permite configurar um logger no cliente do SDK para registrar logs de cada chamada de API.
Configuração do logger
Parâmetros
|
Parâmetro |
Descrição |
|
level |
Prefixo do log. Defina como um nível de log, como info, debug ou warn. Valor padrão: info. |
|
channel |
Canal do log. Valor padrão: AlibabaCloud. |
|
out |
Destino da saída do log. O valor deve ser um objeto que implemente a interface io.writer. |
|
templete |
Modelo que controla o formato de saída do log. Valor padrão: |
Código de exemplo
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.
Log de exemplo
[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
Variáveis configuráveis no modelo
|
Variável |
Descrição |
|
{channel} |
Canal do log. |
|
{host} |
Host da solicitação. |
|
{time} |
Timestamp no formato ISO 8601, exibido em GMT. |
|
{method} |
Método da solicitação. |
|
{uri} |
URI da solicitação. |
|
{version} |
Versão do protocolo. |
|
{target} |
Alvo da solicitação (caminho e query). |
|
{hostname} |
Nome do host do alvo da solicitação. |
|
{code} |
Código de status da resposta, se disponível. |
|
{error} |
Mensagem de erro, se houver. Omitida quando não há erros. |
|
{req_headers} |
Cabeçalhos da solicitação. |
|
{res_headers} |
Cabeçalhos da resposta. |
|
{pid} |
ID do processo. |
|
{cost} |
Duração da solicitação. |
|
{start_time} |
Horário de início da solicitação. |
|
{res_body} |
Corpo da resposta. |