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

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

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

源代碼:

# 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)建并打開一個新文件
  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)容寫入時編碼格式指定
 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可以看做是一個記錄日志的人,對于記錄的每個日志,他需要有一套規(guī)則,比如記錄的格式(formatter),
# 等級(level)等等,這個規(guī)則就是handler。使用logger.addHandler(handler)添加多個規(guī)則,
# 就可以讓一個logger記錄多個日志。
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實現(xiàn)log日志的示例代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python爬取熱搜制作詞云

    python爬取熱搜制作詞云

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

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

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

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

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

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

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

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

    今天小編就為大家分享一篇Python DataFrame 設(shè)置輸出不顯示index(索引)值的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    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ù)去實現(xiàn)直接排序,需要的朋友可以參考下
    2023-04-04
  • pyinstaller?pathex參數(shù)引發(fā)打包no?module?name異常

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

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

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

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

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

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

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

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

最新評論

涟源市| 大足县| 大名县| 玉门市| 石嘴山市| 福安市| 黔西县| 贺兰县| 青浦区| 宿迁市| 合肥市| 平阳县| 辛集市| 阿克苏市| 忻州市| 浦江县| 灯塔市| 明溪县| 建德市| 罗甸县| 深圳市| 台前县| 文登市| 南乐县| 哈密市| 耒阳市| 三门峡市| 商河县| 罗甸县| 维西| 杂多县| 湖南省| 祁门县| 东方市| 张家港市| 道孚县| 安泽县| 湛江市| 元阳县| 桃园市| 鞍山市|