python的log打印工程化模塊設置

在工程啓動模塊中添加一下代碼

from logging.handlers import RotatingFileHandler

logging.getLogger(None).setLevel(logging.INFO)
#################################################################################################
#定義一個StreamHandler,將INFO級別或更高的日誌信息打印到標準錯誤,並將其添加到當前的日誌處理對象#
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
console.setFormatter(formatter)
logging.getLogger(None).addHandler(console)
#################################################################################################
#定義一個StreamHandler,將INFO級別或更高的日誌信息打印到日誌文件,並將其添加到當前的日誌處理對象#
if not os.path.exists("./logs"):
    os.makedirs("./logs")
rthandler = RotatingFileHandler(filename="./logs/servicelog.log", mode='a', maxBytes=10*1024*1024, backupCount=10)
rthandler.setLevel(logging.INFO)
rtformatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
rthandler.setFormatter(rtformatter)
logging.getLogger(None).addHandler(rthandler)
#################################################################################################

代碼個模塊使用方法:

  import logging

logging.info("query")

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章