在使用SDK调用云产品API或资源时,您可以通过调用add_stream_log_handler()方法或配置enable_stream_logger参数,在命令行控制台查看操作日志。

通过client创建命令行控制台日志

本教程以ECS产品为例。完成以下步骤,您可以通过使用Alibaba Cloud SDK for Python创建的ECS client,调用add_stream_log_handler()方法创建命令行控制台日志,并查看其调用API返回结果的日志信息。

  1. 运行如下代码,创建一个ECS client。
    from alibabacloud import get_client
    client = get_client('ecs', 
                        access_key_id=access_key_id,
                        access_key_secret=access_key_secret,
                        region_id='cn-hangzhou')
  2. 运行如下代码,创建命令行控制台日志。
    client.add_stream_log_handler()
  3. 运行如下代码,调用describe_instances接口,并获取结果。
    ret = client.describe_instances()
    运行成功后,命令行控制台输出类似如下日志信息:
    4628 2019-09-09 17:35:51,090 alibabacloud-1871451597120 DEBUG Request received. Product:Ecs QueryParams: {}
    4628 2019-09-09 17:35:51,091 alibabacloud-1871451597120 DEBUG Endpoint Resolved. Endpoint:ecs-cn-hangzhou.aliyuncs.com
    4628 2019-09-09 17:35:51,213 alibabacloud-1871451597120 DEBUG Response received. Product:Ecs Response-body: {"PageNumber":1,"TotalCount":1,"PageSize":10,"RequestId":"63185B79-716B-4D53-AB15-0298B15F77DB","Instances":{"Instance":[{"ImageId":"ubuntu_16_04_64_20G_alibase_20190513.vhd","VlanId":"","EipAddress":{"IpAddress":"","AllocationId":"","InternetChargeType":""},"ZoneId":"cn-hangzhou-h","IoOptimized":true,"SerialNumber":"90794d88-6de6-4428-8633-871f0534206c","Cpu":2,"Memory":8192,"DeviceAvailable":true,"SecurityGroupIds":{"SecurityGroupId":["sg-bp1hcjozmtbp1th4xu31"]},"SaleCycle":"","AutoReleaseTime":"","OSType":"linux","ResourceGroupId":"rg-acfm4m2o2xk6p6q","OSName":"Ubuntu  16.04 64位","InstanceNetworkType":"vpc","HostName":"iZbp1cvlbciwj4kubf5mg2Z","CreationTime":"2019-06-12T05:36Z","EcsCapacityReservationAttr":{"CapacityReservationPreference":"none","CapacityReservationId":""},"RegionId":"cn-hangzhou","DeletionProtection":true,"OperationLocks":{"LockReason":[]},"ExpiredTime":"2099-12-31T15:59Z","InnerIpAddress":{"IpAddress":[]},"InstanceTypeFamily":"ecs.g5","InstanceId":"i-bp1cvlbciwj4kubf5mg2","NetworkInterfaces":{"NetworkInterface":[{"MacAddress":"00:16:3e:0a:31:71","PrimaryIpAddress":"172.16.110.182","NetworkInterfaceId":"eni-bp14w7zx6ruc1jstf1t5"}]},"InternetMaxBandwidthIn":1000,"CreditSpecification":"","InternetChargeType":"PayByTraffic","SpotStrategy":"NoSpot","StoppedMode":"StopCharging","InternetMaxBandwidthOut":5,"VpcAttributes":{"NatIpAddress":"","PrivateIpAddress":{"IpAddress":["172.16.110.182"]},"VSwitchId":"vsw-bp1uha67et911wzchvsbo","VpcId":"vpc-bp1bbzn3qlnnoeryke4bv"},"SpotPriceLimit":0.0,"StartTime":"2019-08-22T06:40Z","InstanceName":"web1","Description":"Python 测试机器","OSNameEn":"Ubuntu  16.04 64 bit","PublicIpAddress":{"IpAddress":[]},"InstanceType":"ecs.g5.large","Status":"Stopped","Recyclable":false,"ClusterId":"","GPUSpec":"","InstanceChargeType":"PostPaid","GPUAmount":0,"DedicatedHostAttribute":{"DedicatedHostId":"","DedicatedHostName":""},"DedicatedInstanceAttribute":{"Affinity":"","Tenancy":""},"DeploymentSetId":""}]}}

通过resource创建命令行控制台日志

本教程以ECS产品为例。完成以下步骤,您可以在初始化ECS resource时,通过enable_stream_logger参数配置命令行控制台日志,并查看其调用API返回结果的日志信息。

  1. 运行如下代码,创建一个配置了命令行控制台日志的ECS resource。
    from alibabacloud import get_resource
    
    resource = get_resource('ecs', 
                            access_key_id=access_key_id,
                            access_key_secret=access_key_secret,
                            region_id='cn-hangzhou', 
                            enable_stream_logger=True)

    命令行控制台日志必选配置参数说明如下:

    enable_stream_logger
    表示是否开启命令行控制台日志。True表示开启,默认为None。
  2. 运行如下代码,获取所有实例的详细信息。
    list(resource.instances.all())
    运行成功后,命令行控制台输出类似如下日志信息:
    7504 2019-09-09 17:40:58,052 alibabacloud-2264238042024 DEBUG Request received. Product:Ecs QueryParams: {'PageNumber': 1}
    7504 2019-09-09 17:40:58,053 alibabacloud-2264238042024 DEBUG Endpoint Resolved. Endpoint:ecs-cn-hangzhou.aliyuncs.com
    7504 2019-09-09 17:40:58,238 alibabacloud-2264238042024 DEBUG Response received. Product:Ecs Response-body: {"PageNumber":1,"TotalCount":1,"PageSize":10,"RequestId":"8FCCFA1E-5BEE-4552-BB17-0CD0EE95C281","Instances":{"Instance":[{"ImageId":"ubuntu_16_04_64_20G_alibase_20190513.vhd","VlanId":"","EipAddress":{"IpAddress":"","AllocationId":"","InternetChargeType":""},"ZoneId":"cn-hangzhou-h","IoOptimized":true,"SerialNumber":"90794d88-6de6-4428-8633-871f0534206c","Cpu":2,"Memory":8192,"DeviceAvailable":true,"SecurityGroupIds":{"SecurityGroupId":["sg-bp1hcjozmtbp1th4xu31"]},"SaleCycle":"","AutoReleaseTime":"","OSType":"linux","ResourceGroupId":"rg-acfm4m2o2xk6p6q","OSName":"Ubuntu  16.04 64位","InstanceNetworkType":"vpc","HostName":"iZbp1cvlbciwj4kubf5mg2Z","CreationTime":"2019-06-12T05:36Z","EcsCapacityReservationAttr":{"CapacityReservationPreference":"none","CapacityReservationId":""},"RegionId":"cn-hangzhou","DeletionProtection":true,"OperationLocks":{"LockReason":[]},"ExpiredTime":"2099-12-31T15:59Z","InnerIpAddress":{"IpAddress":[]},"InstanceTypeFamily":"ecs.g5","InstanceId":"i-bp1cvlbciwj4kubf5mg2","NetworkInterfaces":{"NetworkInterface":[{"MacAddress":"00:16:3e:0a:31:71","PrimaryIpAddress":"172.16.110.182","NetworkInterfaceId":"eni-bp14w7zx6ruc1jstf1t5"}]},"InternetMaxBandwidthIn":1000,"CreditSpecification":"","InternetChargeType":"PayByTraffic","SpotStrategy":"NoSpot","StoppedMode":"StopCharging","InternetMaxBandwidthOut":5,"VpcAttributes":{"NatIpAddress":"","PrivateIpAddress":{"IpAddress":["172.16.110.182"]},"VSwitchId":"vsw-bp1uha67et911wzchvsbo","VpcId":"vpc-bp1bbzn3qlnnoeryke4bv"},"SpotPriceLimit":0.0,"StartTime":"2019-08-22T06:40Z","InstanceName":"web1","Description":"Python 测试机器","OSNameEn":"Ubuntu  16.04 64 bit","PublicIpAddress":{"IpAddress":[]},"InstanceType":"ecs.g5.large","Status":"Stopped","Recyclable":false,"ClusterId":"","GPUSpec":"","InstanceChargeType":"PostPaid","GPUAmount":0,"DedicatedHostAttribute":{"DedicatedHostId":"","DedicatedHostName":""},"DedicatedInstanceAttribute":{"Affinity":"","Tenancy":""},"DeploymentSetId":""}]}}

命令行控制台日志配置

命令行控制台日志可选配置参数如下:

参数 描述
stream_log_level 日志级别。例如:logging.INFO,表示描述信息,从粗粒度上描述了应用的运行过程。
stream_log_name 日志名称。
stream 流。stream=None,默认输出到终端。
stream_format_string 日志信息输出格式。