最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實(shí)現(xiàn)log日志的示例代碼

 更新時(shí)間:2018年04月28日 08:44:47   作者:shengnan_only  
下面小編就為大家分享一篇python實(shí)現(xiàn)log日志的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

源代碼:

# coding=utf-8
import logging
import os
import time
LEVELS={'debug':logging.DEBUG,\
  'info':logging.INFO,\
  'warning':logging.WARNING,\
  'error':logging.ERROR,\
  'critical':logging.CRITICAL,}
  
logger=logging.getLogger()
level='default'
def createFile(filename):
 path=filename[0:filename.rfind('/')]
 if not os.path.isdir(path):
  os.makedirs(path)
 if not os.path.isfile(filename):
#創(chuàng)建并打開(kāi)一個(gè)新文件
  fd = open(filename,mode='w',encoding='utf-8')
  fd.close()
class MyLog:
 log_filename='E:/quality/it/pyrequest-master/log/itest.log'
 err_filename='E:/quality/it/pyrequest-master/log/err.log'
 dateformat='%Y-%m-%d %H:%M:%S'
 logger.setLevel(LEVELS.get(level,logging.NOTSET))
 createFile(log_filename)
 createFile(err_filename)
#注意文件內(nèi)容寫(xiě)入時(shí)編碼格式指定
 handler=logging.FileHandler(log_filename,encoding='utf-8')
 errhandler=logging.FileHandler(err_filename,encoding='utf-8')
 @staticmethod 
 #靜態(tài)方法
 def debug(log_message):
  setHandler('debug')
  logger.debug("[DEBUG "+getCurrentTime()+"]"+log_message)
  removerhandler('debug')
 @staticmethod
 def info(log_message):
  setHandler('info')
  logger.info("[INFO "+getCurrentTime()+"]"+log_message)
  removerhandler('info')
 
 @staticmethod
 def warning(log_message):
  setHandler('warning')
  logger.warning("[WARNING "+getCurrentTime()+"]"+log_message)
  removerhandler('warning')
 @staticmethod
 def error(log_message):
  setHandler('error')
  logger.error("[ERROR "+getCurrentTime()+"]"+log_message)
  removerhandler('error')
 @staticmethod
 def critical(log_message):
  setHandler('critical')
  logger.critical("[CRITICAL "+getCurrentTime()+"]"+log_message)
  removerhandler('critical')
# logger可以看做是一個(gè)記錄日志的人,對(duì)于記錄的每個(gè)日志,他需要有一套規(guī)則,比如記錄的格式(formatter),
# 等級(jí)(level)等等,這個(gè)規(guī)則就是handler。使用logger.addHandler(handler)添加多個(gè)規(guī)則,
# 就可以讓一個(gè)logger記錄多個(gè)日志。
def setHandler(level):
 if level=='error':
  logger.addHandler(MyLog.errhandler)
 #handler=logging.FileHandler(log_filename)
 #把logger添加上handler
 logger.addHandler(MyLog.handler)
def removerhandler(level):
 if level=='error':
  logger.removeHandler(MyLog.errhandler)
 logger.removeHandler(MyLog.handler)
def getCurrentTime():
 return time.strftime(MyLog.dateformat,time.localtime(time.time()))
if __name__=="__main__":
 MyLog.debug("This is debug message")
 MyLog.info("This is info message")
 MyLog.warning("This is warning message")
 MyLog.error("This is error message")
 MyLog.critical("This is critical message")
  

以上這篇python實(shí)現(xiàn)log日志的示例代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python爬取熱搜制作詞云

    python爬取熱搜制作詞云

    這篇文章主要介紹了python爬取百度熱搜制作詞云,首先爬取百度熱搜,至少間隔1小時(shí),存入文件,避免重復(fù)請(qǐng)求,如果本1小時(shí)有了不再請(qǐng)求,存入數(shù)據(jù)庫(kù),供詞云包使用,爬取熱搜,具體流程請(qǐng)需要的小伙伴參考下面文章內(nèi)容
    2021-12-12
  • Pytorch之Tensor和Numpy之間的轉(zhuǎn)換的實(shí)現(xiàn)方法

    Pytorch之Tensor和Numpy之間的轉(zhuǎn)換的實(shí)現(xiàn)方法

    這篇文章主要介紹了Pytorch之Tensor和Numpy之間的轉(zhuǎn)換的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • jupyter?notebook內(nèi)核配置的圖文教程

    jupyter?notebook內(nèi)核配置的圖文教程

    Jupyter?Notebook是基于網(wǎng)頁(yè)的用于交互計(jì)算的應(yīng)用程序,下面這篇文章主要給大家介紹了關(guān)于jupyter?notebook內(nèi)核配置的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • python實(shí)現(xiàn)360的字符顯示界面

    python實(shí)現(xiàn)360的字符顯示界面

    這篇文章主要介紹了python實(shí)現(xiàn)360的字符顯示界面示例,需要的朋友可以參考下
    2014-02-02
  • Python DataFrame 設(shè)置輸出不顯示index(索引)值的方法

    Python DataFrame 設(shè)置輸出不顯示index(索引)值的方法

    今天小編就為大家分享一篇Python DataFrame 設(shè)置輸出不顯示index(索引)值的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • 關(guān)于Python排序sort()函數(shù)和sorted()函數(shù)

    關(guān)于Python排序sort()函數(shù)和sorted()函數(shù)

    這篇文章主要介紹了關(guān)于Python排序sort()函數(shù)和sorted()函數(shù),利用Python中的內(nèi)置函數(shù)去實(shí)現(xiàn)直接排序,需要的朋友可以參考下
    2023-04-04
  • pyinstaller?pathex參數(shù)引發(fā)打包no?module?name異常

    pyinstaller?pathex參數(shù)引發(fā)打包no?module?name異常

    這篇文章主要為大家介紹了一個(gè)關(guān)于pyinstaller的?pathex?參數(shù)所引發(fā)的打包執(zhí)行報(bào)no?module?name的異常錯(cuò)誤解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • python實(shí)戰(zhàn)教程之OCR文字識(shí)別方法匯總

    python實(shí)戰(zhàn)教程之OCR文字識(shí)別方法匯總

    ocr是一種光學(xué)字符識(shí)別技術(shù),簡(jiǎn)單來(lái)說(shuō)它能夠識(shí)別出圖像中的文字并且將其給取出來(lái),下面這篇文章主要給大家介紹了關(guān)于python實(shí)戰(zhàn)教程之OCR文字識(shí)別方法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • Python日志處理模塊logging用法解析

    Python日志處理模塊logging用法解析

    這篇文章主要介紹了Python日志處理模塊logging用法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Python必備技能之debug調(diào)試教程詳解

    Python必備技能之debug調(diào)試教程詳解

    這篇文章主要為大家詳細(xì)介紹了Python初學(xué)者必須要學(xué)會(huì)的技能——在Python中進(jìn)行debug操作,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-03-03

最新評(píng)論

合山市| 扎兰屯市| 策勒县| 张家口市| 沁阳市| 临沧市| 萨嘎县| 宜黄县| 吴桥县| 电白县| 常州市| 石泉县| 剑河县| 泸西县| 锡林浩特市| 松江区| 会理县| 宁河县| 宁明县| 霍城县| 襄城县| 广平县| 英超| 阿荣旗| 高碑店市| 永年县| 宜兰市| 衢州市| 莫力| 辽中县| 郓城县| 景洪市| 桓台县| 凉城县| 文昌市| 沙洋县| 封开县| 三河市| 梅州市| 陈巴尔虎旗| 成武县|