python 如何對logging日志封裝
作者:做夢的人(小姐姐)
出處:https://www.cnblogs.com/chongyou/
因為最近在做平臺,發(fā)現(xiàn)有同事,使用django封裝了日志模塊,看樣子很簡單,準(zhǔn)備自己單獨(dú)做了一個日志封裝模板,對于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("")
# 設(shè)置輸出的等級
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)
# 設(shè)置輸出格式
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日志封裝的詳細(xì)內(nèi)容,更多關(guān)于python logging日志封裝的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
關(guān)于Django使用 django-celery-beat動態(tài)添加定時任務(wù)的方法
本文給大家介紹Django使用 django-celery-beat動態(tài)添加定時任務(wù)的方法,安裝對應(yīng)的是celery版本,文中給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-10-10
Python+Requests+PyTest+Excel+Allure?接口自動化測試實(shí)戰(zhàn)
本文主要介紹了Python+Requests+PyTest+Excel+Allure?接口自動化測試實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
使用 Visual Studio Code(VSCode)搭建簡單的Python+Djan
這篇文章主要介紹了使用 Visual Studio Code(VSCode)搭建簡單的Python+Django開發(fā)環(huán)境的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
Python機(jī)器學(xué)習(xí)k-近鄰算法(K Nearest Neighbor)實(shí)例詳解
這篇文章主要介紹了Python機(jī)器學(xué)習(xí)k-近鄰算法(K Nearest Neighbor),結(jié)合實(shí)例形式分析了k-近鄰算法的原理、操作步驟、相關(guān)實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2018-06-06
python SVM 線性分類模型的實(shí)現(xiàn)
這篇文章主要介紹了python SVM 線性分類模型的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
python機(jī)器學(xué)習(xí)之KNN分類算法
這篇文章主要為大家詳細(xì)介紹了python機(jī)器學(xué)習(xí)之KNN分類算法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08
Python PIL庫讀取設(shè)置圖像的像素內(nèi)容方法示例
這篇文章主要為大家介紹了使用Python PIL庫Image模塊中的getpixel和putpixel方法讀取設(shè)置圖像的像素內(nèi)容實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
python讀取excel進(jìn)行遍歷/xlrd模塊操作
這篇文章主要介紹了python讀取excel進(jìn)行遍歷/xlrd模塊操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07

