すべてのプロダクト
Search
ドキュメントセンター

Object Storage Service:ログ記録の有効化

最終更新日:Nov 14, 2024

トラブルシューティングを容易にするために、OSS SDK for Pythonにはログ記録機能があります。 デフォルトでは、ログ記録は無効になっています。

説明

ログ記録は、Python V2.6.X以降のOSS SDKで提供されます。

背景情報

OSS SDK for Pythonが提供するログ記録機能を使用して、OSSでの操作のログを収集できます。 ログ情報は、コンピューターに保存されているログファイルに記録されます。

  • ログ形式: <time><name><level><threadId><message>

  • 降順のログレベル: CRITICAL、ERROR、WARNING、INFO、DEBUG、およびNOTSET

    説明

    ログレベルを指定すると、ローカルログファイルには、指定したレベル以上のログのみが記録されます。 たとえば、ログレベルをINFOに設定した場合、ログファイルには、レベルがCRITICAL、ERROR、WARNING、およびINFOのログが記録されます。

使用上の注意

  • このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。 OSSと同じリージョンにある他のAlibaba CloudサービスからOSSにアクセスする場合は、内部エンドポイントを使用します。 OSSリージョンとエンドポイントの詳細については、「リージョン、エンドポイント、オープンポート」をご参照ください。

  • このトピックでは、OSSエンドポイントを使用してOSSClientインスタンスを作成します。 カスタムドメイン名またはSTS (Security Token Service) を使用してOSSClientインスタンスを作成する場合は、「初期化」をご参照ください。

ログ記録の有効化

次のサンプルコードは、ログ記録を有効にする方法の例を示しています。

# -*- 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.ProviderAuthV4(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. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# 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')

ログの例:

説明

より詳細な情報を提供するログを取得するには、ログレベルを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'
    }
}

ログ情報は、バケットが初期化されると、バケット内のすべてのオブジェクトがトラバースされることを示します。 要求されたオブジェクトが存在しない場合、NoSuchKeyエラーが発生します。