All Products
Search
Document Center

Alibaba Cloud SDK:Use the logging feature

Last Updated:Jul 01, 2024

This topic describes how to specify the output format and storage path of logs.

Configure a logger

import os

from aliyunsdkcore.client import AcsClient

import sys

client = AcsClient(
    os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),  # Obtain the AccessKey ID from an environment variable. We recommend that you use the AccessKey ID of a Resource Access Management (RAM) user.
    os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),  # Obtain the AccessKey secret from an environment variable. We recommend that you use the AccessKey secret of the RAM user.
    '<region_id>"'
)

# Export logs in the console or a terminal.
client.set_stream_logger(
    stream=sys.stderr,  # The output object of logs.
    format_string='%(thread)d %(asctime)s %(name)s %(levelname)s %(message)s'  # The output format of logs.
)

# Export the logs in the default format to a file.
client.set_file_logger(
    path='<file_path>'  # The path of the log file.
)