To facilitate troubleshooting, OSS SDK for Python provides the log recording feature. By default, log recording is disabled.

Note To enable log recording for OSS SDK for Python, use OSS SDK V2.6.x and later.

The log recording feature provided by OSS SDK for Python can be used to collect log information of operations in OSS. The log information is recorded in log files stored in your computer.

  • Log format: <time><name><level><threadId><message>
  • Log levels in ascending order: CRITICAL, ERROR, WARNING, INFO, DEBUG, and NOTSET
    Note After a log level is specified, a local log file records only logs whose levels are equal to or higher than the specified level. For example, if you set the log level to INFO, the log file records logs whose levels are CRITICAL, ERROR, WARNING, and INFO.

Enable log recording

The following code provides an example on how to enable log recording for OSS SDK for Python:

# -*- coding: utf-8 -*-

import os
import logging
import oss2
from itertools import islice
# Download log information to a local log file, and store the log file in the specified local path. 
# By default, if you specify the name of a local file such as examplelogfile.log without specifying the local path, the local file is saved to the local path of the project to which the sample program belongs. 
log_file_path = "D:\\localpath\\examplelogfile.log"

# Enable log recording. 
oss2.set_file_logger(log_file_path, 'oss2', logging.INFO)
# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'yourEndpoint', 'examplebucket')

# Traverse objects and directories. 
for b in islice(oss2.ObjectIterator(bucket), 10):
    print(b.key)
# Obtain the metadata of the object. 
# Specify the full path of the object. Example: exampledir/exampleobject.txt. The full path of the object cannot contain bucket names. 
object_meta = bucket.get_object_meta('exampledir/exampleobject.txt')

An example of log information

Note To obtain more log details, you can change the log level to DEBUG.
2018-11-20 16:37:46,437 oss2.api [INFO] 26716 : Exception: {
    'status': 404, 
    'x-oss-request-id': '5BF3C7DA236B3A201CE64679', 
    'details': {
        'HostId': 'examplebucket.oss-cn-shenzhen.aliyuncs.com', 
        'Message': 'The specified key does not exist.', 
        'Code': 'NoSuchKey', 
        'RequestId': '5BF3C7DA236B3A201CE64679', 
        'Key': 'exampledir/exampleobject.txt'
    }
}

The log information shows that all objects in a bucket are traversed if the bucket is initialized. If the requested object does not exist, an error occurs. The error information is NoSuchKey.