国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

Python使用logging結(jié)合decorator模式實(shí)現(xiàn)優(yōu)化日志輸出的方法

2020-01-04 17:32:27
字體:
供稿:網(wǎng)友
這篇文章主要介紹了Python使用logging結(jié)合decorator模式實(shí)現(xiàn)優(yōu)化日志輸出的方法,實(shí)例分析了Python使用logging模塊操作日志的相關(guān)技巧,需要的朋友可以參考下
 

本文實(shí)例講述了Python使用logging結(jié)合decorator模式實(shí)現(xiàn)優(yōu)化日志輸出的方法。分享給大家供大家參考,具體如下:

python內(nèi)置的loging模塊非常簡(jiǎn)便易用, 很適合程序運(yùn)行日志的輸出。

而結(jié)合python的裝飾器模式,則可實(shí)現(xiàn)簡(jiǎn)明實(shí)用的代碼。測(cè)試代碼如下所示:

#! /usr/bin/env python2.7# -*- encoding: utf-8 -*-import logginglogging.basicConfig(format='[%(asctime)s] %(message)s', level=logging.INFO)def time_recorder(func):  """裝飾器, 用在func方法執(zhí)行前后, 增加運(yùn)行信息"""  def wrapper():    logging.info("Begin to execute function: %s" % func.__name__)    func()    logging.info("Finish executing function: %s" % func.__name__)  return wrapper@time_recorderdef first_func():  print "I'm first_function. I'm doing something..."@time_recorderdef second_func():  print "I'm second_function. I'm doing something..."if __name__ == "__main__":  first_func()  second_func()

運(yùn)行并得到輸出:

[2014-04-01 18:02:13,724] Begin to execute function: first_funcI'm first_function. I'm doing something...[2014-04-01 18:02:13,725] Finish executing function: first_func[2014-04-01 18:02:13,725] Begin to execute function: second_funcI'm second_function. I'm doing something...[2014-04-01 18:02:13,725] Finish executing function: second_func
 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 天气| 沂源县| 安阳县| 肥东县| 凭祥市| 怀来县| 团风县| 丹东市| 福鼎市| 阿坝县| 达尔| 荃湾区| 河东区| 桐乡市| 宜昌市| 商都县| 萍乡市| 项城市| 德保县| 晋城| 且末县| 梁山县| 常山县| 伊宁县| 瑞安市| 商都县| 调兵山市| 保康县| 横峰县| 泌阳县| 齐齐哈尔市| 静海县| 剑河县| 舟曲县| 谷城县| 措美县| 普洱| 集安市| 高要市| 涿鹿县| 安康市|