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)文章
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是基于網(wǎng)頁(yè)的用于交互計(jì)算的應(yīng)用程序,下面這篇文章主要給大家介紹了關(guān)于jupyter?notebook內(nèi)核配置的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02
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ù),利用Python中的內(nèi)置函數(shù)去實(shí)現(xiàn)直接排序,需要的朋友可以參考下2023-04-04
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í)別方法匯總
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

