#!/usr/bin/python
# -*- coding: UTF-8 -*-
import time
import logging
import traceback
# logging.basicConfig(level=logging.INFO, filename="{}_log.txt".format(__file__[:-3]), filemode="w", format='%(asctime)s-%(filename)s[line:%(lineno)d]-%(levelname)s: %(message)s')
def init_logging(the_level=logging.INFO, the_filename="{}_log.txt".format(__file__[:-3]), the_filemode="a", the_format='%(asctime)s-%(filename)s[line:%(lineno)d]-%(levelname)s: %(message)s'):
logging.basicConfig(level = the_level, filename= the_filename, filemode=the_filemode, format=the_format)
def record_time(fun_name):
"""記錄程序執(zhí)行時間工具"""
def wrapper(fun):
def inner(*fun_params):
run_flag = False # 標記作業(yè)是否正常執(zhí)行
start_time = time.time()
logging.info("程序運行時間:{} 執(zhí)行函數(shù):{} ".format(time.strftime("%Y%m%d %H:%M:%S", time.localtime(start_time)), fun_name))
try:
fun(*fun_params)
run_flag = True
finally:
run_result = "成功" if run_flag else "失敗"
logging.info("執(zhí)行函數(shù){}: {}".format(fun_name, run_result) )
# 統(tǒng)計單個作業(yè)運行時長
end_time = time.time()
run_time = end_time - start_time
logging.info("開始時間:{} 結(jié)束時間:{} 耗時:{:.0f} 秒".format(time.strftime("%Y%m%d %H:%M:%S", time.localtime(start_time)),
time.strftime("%Y%m%d %H:%M:%S", time.localtime(end_time)), run_time))
return inner
return wrapper
@record_time("extract_data")
def test_fun():
for i in range(5):
logging.info("this is info message")
time.sleep(1)
if i == 1:
1/0
if __name__ == '__main__':
try:
init_logging()
test_fun()
except Exception as e:
logging.error("\033[35m bug message:\n{}\n".format(traceback.format_exc()))
python_記錄程序運行時間的工具
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 2017/6/14奇點閱讀K4營打卡第13天 K4營即將結(jié)束,結(jié)營分要求20分,每天打卡總分14分,向公號投稿已經(jīng)...