All Products
Search
Document Center

Object Storage Service:Enable log recording

Last Updated:Sep 20, 2023

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

Note

Log recording is provided in OSS SDK for Python V2.6.X and later.

Background information

You can use the log recording feature provided by OSS SDK for Python to collect logs of operations in OSS. Log information is recorded in log files stored on your computer.

  • Log format: <time><name><level><threadId><message>

  • Log levels in descending order: CRITICAL, ERROR, WARNING, INFO, DEBUG, and NOTSET

    Note

    After a log level is specified, the 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.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.

Enable log recording

The following sample code provides an example on how to enable log recording:

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

import os
import logging
import oss2
from itertools import islice
from oss2.credentials import EnvironmentVariableCredentialsProvider

# 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 project belongs. 
log_file_path = "D:\\localpath\\examplelogfile.log"

# Enable log recording. 
oss2.set_file_logger(log_file_path, 'oss2', logging.INFO)

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# Traverse objects and directories. 
for b in islice(oss2.ObjectIterator(bucket), 10):
    print(b.key)
# Query the metadata of the object. 
# Specify the full path of the object. Example: exampledir/exampleobject.txt. Do not include the bucket name in the full path. 
object_meta = bucket.get_object_meta('exampledir/exampleobject.txt')

Log example:

Note

To obtain logs that provide more detailed information, 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, the NoSuchKey error occurs.