1. 程式人生 > 實用技巧 >python中的日誌模組(一)

python中的日誌模組(一)

#日誌類
import os
import logging

current_path = os.path.dirname(__file__)
log_path = os.path.join(current_path, '../log/testpython.log')

class logUtil:
def __init__(self,logfile_path=log_path):
self.logfile_path=log_path
self.logger = logging.getLogger('__log_util__')
self.logger.setLevel(level=logging.INFO)

file_log = logging.FileHandler(log_path) # 闖將一個檔案日誌物件
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_log.setFormatter(formatter)
self.logger.addHandler(file_log)


def info(self,message):
self.logger.info(message)

def error(self,errmessage):

self.logger.error(errmessage)

log_info=logUtil(log_path)

if __name__ == '__main__':
log=logUtil(log_path)
log.error('系統異常')
log.info('登入成功')


#類封裝
import os
import xlrd
from pritices_daily import case_infos
from pritices_daily.config_utils import conf_test
from pritices_daily.log_util import log_info

class ReadExcel:
def __init__(self,excel_path):
self.excel_path=excel_path

def read_excel_data_convert_info_01(self):
# 1,讀取資料存到列表中:[[用例編號,用例名稱,所在模組...],[....],[......]]
workbook = xlrd.open_workbook(self.excel_path) # 開啟工作簿
try:
log_info.info('開啟工作簿')
sheet = workbook.sheet_by_index(0) # 開啟第一個表
log_info.info('開啟第一個工作表')
all_case_info1 = []
for i in range(1, sheet.nrows):
case_info = []
for j in range(0, sheet.ncols):
case_info.append(sheet.cell_value(i, j))
all_case_info1.append(case_info)
except Exception as e:
log_info.error('系統錯誤')
return all_case_info1

# 2,做成類形成
def read_excel_data_convert_info_02(self):
workbook = xlrd.open_workbook(self.excel_path) # 開啟工作簿
sheet = workbook.sheet_by_index(0) # 開啟第一個表
all_case_info2 = []
for i in range(1, sheet.nrows):
case_id = sheet.cell_value(i, 0)
case_name = sheet.cell_value(i, 1)
case_module = sheet.cell_value(i, 2)
case_pri = sheet.cell_value(i, 3)
case_step = sheet.cell_value(i, 4)
case_result = sheet.cell_value(i, 5)
CaseInfo = case_infos.CaseInfo(case_id, case_name, case_module, case_pri, case_step, case_result)
all_case_info2.append(CaseInfo)
return all_case_info2[0].case_name

if __name__ == '__main__':
current_path = os.path.dirname(__file__)
expath = conf_test.read_ini
excel_path = os.path.join(current_path, conf_test.read_ini)
rd01=ReadExcel(excel_path)
print(rd01.read_excel_data_convert_info_01())
print(rd01.read_excel_data_convert_info_02())