Updated on 2024-03-26 GMT+08:00

Log Initialization (SDK for Python)

Function

You can enable the SDK log function to record log information generated during API calling into log files for subsequent data analysis or fault location. The procedure is as follows:

  1. Find file log.conf in the OBS Python SDK (obtained from GitHub). The content format is as follows:
    [LOGCONF]
    
    #Configure log file dir
    LogFileDir 			= ./logs
    
    #Configure log file name
    LogFileName 		= eSDK-OBS-PYTHON.log
    
    #Configure log file size, unit:MB
    LogFileSize     	= 30
    
    #Configure max log file numbers
    LogFileNumber   	= 5
    
    #Configure log level for log file (DEBUG | INFO | WARNING | ERROR)
    LogFileLevel    	= INFO
    
    #Configure whether to print log to console (Yes:1 No:0)
    PrintLogToConsole   = 0
    
    #Configure log level for console (DEBUG | INFO | WARNING | ERROR)
    PrintLogLevel		= WARNING
  2. Modify parameters in the log.conf file as needed.
  3. Call ObsClient.initLog to enable the logging function.
  • The logging function is disabled by default. You need to enable it manually.
  • For details about SDK logs, see Log Analysis.
  • You can change the log file permissions in the system based on your actual needs.

The log module of the OBS Python SDK is thread secure but not process secure. If ObsClient is used in multi-process scenarios, you must configure an independent log path for each instance of ObsClient to prevent conflicts when multiple processes write logs concurrently.

Method

obsClient.initLog(
    log_config='*** Your Log Configuration Parameters ***',
    log_name='*** Your Log Name ***'
)

Constructor Parameter Description

Parameter

Type

Mandatory (Yes/No)

Description

log_config

LogConf

Yes

Log configuration parameter

log_name

str

No

Log name

Table 1 LogConf

Parameter

Type

Mandatory (Yes/No)

Description

config_file

str

Yes

Path to the log configuration file

sec

str

No

Section name in the log configuration file. The default value is LOGCONF.

Code Examples

# Import the module.
from obs import ObsClient

# Obtain an AK and SK pair using environment variables or import the AK and SK pair in other ways. Using hard coding may result in leakage.
# Obtain an AK and SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
ak = os.getenv("AccessKeyID")
sk = os.getenv("SecretAccessKey")
# (Optional) If you use a temporary AK and SK pair and a security token to access OBS, obtain them from environment variables.
security_token = os.getenv("SecurityToken")
# Set server to the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
server = "https://obs.ap-southeast-1.myhuaweicloud.com" 

# Create an obsClient instance.
# If you use a temporary AK and SK pair and a security token to access OBS, you must specify security_token when creating an instance.
obsClient = ObsClient(access_key_id=ak, secret_access_key=sk, server=server)

# Import the log module.
from obs import LogConf

# Specify the path to the log configuration file and initialize logs of ObsClient.
obsClient.initLog(LogConf('./log.conf'), '*** Your Log Name ***')

# Use ObsClient to access OBS.

# Disable ObsClient logging.
obsClient.close()