在使用SDK调用云产品API或资源时,您可以通过调用add_stream_log_handler()方法或配置enable_stream_loggerfile_logger_path参数,使用文件记录操作日志。

通过client创建文件日志

本教程以ECS产品为例。完成以下步骤,您可以通过使用Alibaba Cloud SDK for Python创建的ECS client,调用add_rotating_file_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_rotating_file_log_handler(path="ecs.log")
    说明 path为必需参数,表示设置文件路径。
    运行成功后,在当前目录下生成了一个名为ecs.log的文件。
  3. 运行如下代码,调用describe_instances接口,并获取结果。
    ret = client.describe_instances()
  4. 可选: 打开ecs.log日志文件,查看日志信息。
    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_file_loggerfile_logger_path配置文件日志,并查看其调用API返回结果的日志信息。

  1. 运行如下代码,创建一个配置文件日志的ECS resource。
    from alibabacloud import get_resource
    
    ecs_resource = get_resource('ecs', 
                                access_key_id=access_key_id,
                                access_key_secret=access_key_secret,
                                region_id='cn-hangzhou', 
                                enable_file_logger=True,
                                file_logger_path="ecs.log")

    文件日志必选配置参数说明如下:

    enable_file_logger
    表示是否开启文件日志。True表示开启,默认为None。
    file_logger_path
    设置logger的文件路径。若不配置,报错如下信息:
    alibabacloud.exceptions.ClientException: The params file_logger_path is needed. If you want add file logger handler.
    运行成功后,在当前目录下生成了一个名为ecs.log的文件。
  2. 运行如下代码,获取所有实例的详细信息。
    list(ecs_resource.instances.all())
  3. 可选: 打开ecs.log日志文件,查看日志信息。
    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":""}]}}

文件日志配置

文件日志可选配置参数如下:

参数 描述
file_log_level 日志级别。例如:logging.INFO,表示描述信息,从粗粒度上描述了应用的运行过程。
file_log_name 日志名称。
file_logger_path 文件日志位置。
file_max_bytes 文件最大字节数,默认10485760字节。
file_backup_count 最多保存的文件个数,默认5个文件。
file_logger_format_string 日志信息输出格式。
说明 Alibaba Cloud SDK for Python通过使用logging模块下的RotatingFileHandler文件记录日志。RotatingFileHandler文件最大字节为10M,备份文件为5个。因此,使用默认的日志文件配置前,请确保磁盘至少有60M可用空间。