国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 登封市| 井研县| 唐河县| 苗栗市| 临夏市| 鲜城| 正阳县| 健康| 夏河县| 甘德县| 策勒县| 寿光市| 太仓市| 肇东市| 舒兰市| 伊宁市| 龙川县| 安平县| 安多县| 德化县| 通化县| 楚雄市| 兴隆县| 阜康市| 静安区| 麻江县| 黄陵县| 朝阳区| 宜城市| 涞水县| 防城港市| 怀柔区| 广安市| 五指山市| 旌德县| 西宁市| 澄江县| 克拉玛依市| 德令哈市| 平定县| 隆子县|