網站首頁 編程語言 正文
前言:
如果使用進到的日志文件方法:logging.FileHandler
,會導致日志信息全部存放在一個日志文件中,不利于后面對日志文件的使用。
下面分享常見的兩種分文件存儲日志的方法。delay = True
參數避免了出現多進程中讀取日志權限的問題
TimedRotatingFileHandler 根據時間創建日志文件
TimedRotatingFileHandler(filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False, atTime=None)
atTime 與 when參數之間的關系
RotatingFileHander 根據日志文件大小創建日志文件
RotatingFileHandler(filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=False)
分文件時,PermissionError異常處理
異常信息:
--- Logging error --- Traceback (most recent call last): '省略部分信息' PermissionError: [WinError 32] 另一個程序正在使用此文件,進程無法訪問。
解決方法:
設置 delay=True
使用第三方庫 concurrent_log_handler.ConcurrentRotatingFileHandler
代碼實現: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字節,保留10個文件 # handler = handlers.RotatingFileHandler(log_file, maxBytes=10, backupCount=10, # encoding='utf-8', delay=True) # 按文件大小分文件存儲,每個文件10字節,保留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
原文鏈接:https://blog.csdn.net/qq_17328759/article/details/125793689
相關推薦
- 2022-04-18 python?numpy中對ndarry按照index增刪改查_python
- 2023-04-01 pytorch常用函數之torch.randn()解讀_python
- 2022-03-28 Python垃圾回收及Linux?Fork_python
- 2022-05-23 ELK與Grafana聯合打造可視化監控來分析nginx日志_nginx
- 2024-02-17 開發中SpringBoot項目jar包過大的解決辦法
- 2023-02-14 Cython處理C字符串的示例詳解_python
- 2022-09-24 基于Pytorch實現邏輯回歸_python
- 2022-07-02 python等間距取值方式_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支