python 如何對logging日志封裝
作者:做夢的人(小姐姐)
出處:https://www.cnblogs.com/chongyou/
因為最近在做平臺,發(fā)現(xiàn)有同事,使用django封裝了日志模塊,看樣子很簡單,準備自己單獨做了一個日志封裝模板,對于python不熟練的我,封裝部分參考了多個博主的內(nèi)容,形成自己的日志模塊,內(nèi)容如下:
封裝部分
創(chuàng)建一個logutil2的py文件
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Author: zhangjun # @Date : 2018/7/26 9:20 # @Desc : Description import logging import logging.handlers import os import time class logs(object): def __init__(self): self.logger = logging.getLogger("") # 設置輸出的等級 LEVELS = {'NOSET': logging.NOTSET, 'DEBUG': logging.DEBUG, 'INFO': logging.INFO, 'WARNING': logging.WARNING, 'ERROR': logging.ERROR, 'CRITICAL': logging.CRITICAL} # 創(chuàng)建文件目錄 logs_dir="logs2" if os.path.exists(logs_dir) and os.path.isdir(logs_dir): pass else: os.mkdir(logs_dir) # 修改log保存位置 timestamp=time.strftime("%Y-%m-%d",time.localtime()) logfilename='%s.txt' % timestamp logfilepath=os.path.join(logs_dir,logfilename) rotatingFileHandler = logging.handlers.RotatingFileHandler(filename =logfilepath, maxBytes = 1024 * 1024 * 50, backupCount = 5) # 設置輸出格式 formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] %(message)s', '%Y-%m-%d %H:%M:%S') rotatingFileHandler.setFormatter(formatter) # 控制臺句柄 console = logging.StreamHandler() console.setLevel(logging.NOTSET) console.setFormatter(formatter) # 添加內(nèi)容到日志句柄中 self.logger.addHandler(rotatingFileHandler) self.logger.addHandler(console) self.logger.setLevel(logging.NOTSET) def info(self, message): self.logger.info(message) def debug(self, message): self.logger.debug(message) def warning(self, message): self.logger.warning(message) def error(self, message): self.logger.error(message)
2.調(diào)用模塊
創(chuàng)建另外一個py文件
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Author: zhangjun # @Date : 2018/7/26 9:21 # @Desc : Description import logging logger = logging.getLogger(__name__) import logutil2 if __name__ == '__main__': logger=logutil2.logs() logger.info("this is info") logger.debug("this is debug") logger.error("this is error") logger.warning("this is warning")
結(jié)果展示:
1.控制臺輸出
2.日志文件展示
創(chuàng)建目錄
日志文件的寫入
以上就是python 如何對logging日志封裝的詳細內(nèi)容,更多關(guān)于python logging日志封裝的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
關(guān)于Django使用 django-celery-beat動態(tài)添加定時任務的方法
本文給大家介紹Django使用 django-celery-beat動態(tài)添加定時任務的方法,安裝對應的是celery版本,文中給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-10-10Python+Requests+PyTest+Excel+Allure?接口自動化測試實戰(zhàn)
本文主要介紹了Python+Requests+PyTest+Excel+Allure?接口自動化測試實戰(zhàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-02-02使用 Visual Studio Code(VSCode)搭建簡單的Python+Djan
這篇文章主要介紹了使用 Visual Studio Code(VSCode)搭建簡單的Python+Django開發(fā)環(huán)境的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12Python機器學習k-近鄰算法(K Nearest Neighbor)實例詳解
這篇文章主要介紹了Python機器學習k-近鄰算法(K Nearest Neighbor),結(jié)合實例形式分析了k-近鄰算法的原理、操作步驟、相關(guān)實現(xiàn)與使用技巧,需要的朋友可以參考下2018-06-06Python PIL庫讀取設置圖像的像素內(nèi)容方法示例
這篇文章主要為大家介紹了使用Python PIL庫Image模塊中的getpixel和putpixel方法讀取設置圖像的像素內(nèi)容實例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01