python?logging模塊的分文件存放詳析
前言:
如果使用進到的日志文件方法:logging.FileHandler,會導致日志信息全部存放在一個日志文件中,不利于后面對日志文件的使用。
下面分享常見的兩種分文件存儲日志的方法。delay = True 參數(shù)避免了出現(xiàn)多進程中讀取日志權(quán)限的問題
TimedRotatingFileHandler 根據(jù)時間創(chuàng)建日志文件
TimedRotatingFileHandler(filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False, atTime=None)

atTime 與 when參數(shù)之間的關(guān)系

RotatingFileHander 根據(jù)日志文件大小創(chuàng)建日志文件
RotatingFileHandler(filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=False)

分文件時,PermissionError異常處理
異常信息:
--- Logging error --- Traceback (most recent call last): '省略部分信息' PermissionError: [WinError 32] 另一個程序正在使用此文件,進程無法訪問。
解決方法:
設(shè)置 delay=True使用第三方庫 concurrent_log_handler.ConcurrentRotatingFileHandler
代碼實現(xiàn):customer_log.py
import logging
from logging import handlers
from concurrent_log_handler import ConcurrentRotatingFileHandler
def set_basic_logger():
path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
log_path = path + '/Log/'
log_file = log_path + 'mockSystem.log'
err_file = log_path + 'mockSystemErr.log'
# 定制輸出格式
formatter = logging.Formatter(
'[%(asctime)s] %(filename)s -> %(funcName)s line:%(lineno)d [%(levelname)s] : %(message)s')
# # 所有日志在一個文件中存儲
# handler = logging.FileHandler(log_file, encoding='utf-8', mode='a+')
# 按天分文件存儲,保存最近30天的日志
handler = handlers.TimedRotatingFileHandler(log_file, when='d', interval=1, backupCount=30, encoding='utf-8', delay=True)
# 按文件大小分文件存儲,每個文件10字節(jié),保留10個文件
# handler = handlers.RotatingFileHandler(log_file, maxBytes=10, backupCount=10,
# encoding='utf-8', delay=True)
# 按文件大小分文件存儲,每個文件10字節(jié),保留10個文件
# handler = ConcurrentRotatingFileHandler(log_file, maxBytes=10, backupCount=10)
handler.setLevel(logging.INFO)
handler.setFormatter(formatter)
# err_handler = ConcurrentRotatingFileHandler(err_file, encoding='utf-8', mode='a+') # 輸出到err_log文件
err_handler = handlers.TimedRotatingFileHandler(err_file, when='d', interval=1, backupCount=30,
encoding='utf-8', delay=True)
# err_handler = handlers.RotatingFileHandler(err_file, maxBytes=10, backupCount=10,
# encoding='utf-8', delay=True)
# err_handler = ConcurrentRotatingFileHandler(err_file, maxBytes=10, backupCount=10)
err_handler.setLevel(logging.WARNING)
err_handler.setFormatter(formatter)
logging.basicConfig(
level=logging.DEBUG,
format='[%(asctime)s] %(filename)s -> %(funcName)s line:%(lineno)d [%(levelname)s] : %(message)s',
handlers=[handler, err_handler]
)在項目主程序中使用時:main.py
from customer_log imoprt set_basic_logger import mu set_basic_logger() mu.show_cur_info()
在項目其他模塊使用時:mu.py
import logging def show_cur_info(): msg = 'dddddd' print(msg) logging.info(msg
到此這篇關(guān)于python logging模塊的分文件存放詳析的文章就介紹到這了,更多相關(guān)python logging模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用TensorBoard進行超參數(shù)優(yōu)化的實現(xiàn)
這篇文章主要介紹了使用TensorBoard進行超參數(shù)優(yōu)化的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07
關(guān)于Python dict存中文字符dumps()的問題
這篇文章主要介紹了關(guān)于Python dict存中文字符dumps()的問題,本文給大家分享問題及解決方案,給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-10-10

