python logging模块的分文件存放详析
作者:hqx 时间:2023-04-02 20:27:32
前言:
如果使用进到的日志文件方法: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
标签:python,logging,模块,分文件,存放
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python super()函数的详解
2023-08-08 10:09:42
![](https://img.aspxhome.com/file/2023/5/107265_0s.png)
HTML5 Canvas 起步(3) - 颜色与渐变
2009-06-08 12:58:00
![](https://img.aspxhome.com/file/UploadPic/20096/8/3-colors-72s.jpg)
Flask框架学习笔记之表单基础介绍与表单提交方式
2023-02-05 09:25:44
![](https://img.aspxhome.com/file/2023/2/103542_0s.png)
mysql 5.7.18 winx64安装配置方法图文教程
2024-01-13 16:37:30
![](https://img.aspxhome.com/file/2023/8/80128_0s.png)
phpmyadmin显示utf8_general_ci中文乱码的问题终级篇
2024-04-30 09:57:56
![](https://img.aspxhome.com/file/2023/0/131850_0s.jpg)
tween.js缓动补间动画算法示例
2024-05-21 10:13:40
由prototype_1.3.1进入javascript殿堂-类的初探
2008-05-22 12:44:00
简要介绍SQL Server 2008新的事件处理系统
2009-12-22 08:15:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/210u51y4y-40s.jpg)
解决Microsoft VBScript 运行时错误 (0x800A0046) 没有权限的解决方案
2009-09-03 13:28:00
vue项目两种方式实现竖向表格的思路分析
2024-06-05 15:31:13
![](https://img.aspxhome.com/file/2023/1/123131_0s.png)
用asp程序读取网站的alexa世界排名
2008-11-23 20:43:00
python爬虫开发之使用Python爬虫库requests多线程抓取猫眼电影TOP100实例
2021-02-25 11:13:42
![](https://img.aspxhome.com/file/2023/9/99519_0s.png)
详解vue-cli 脚手架 安装
2024-05-09 10:40:06
![](https://img.aspxhome.com/file/2023/2/126642_0s.png)
mysql存储过程中使用游标的实例
2024-01-28 14:30:33
Vue3通过ref操作Dom元素及hooks的使用方法
2024-04-27 16:07:32
![](https://img.aspxhome.com/file/2023/4/133004_0s.png)
解决idea git切换多个分支后maven不生效的问题
2023-06-28 17:48:13
![](https://img.aspxhome.com/file/2023/7/132257_0s.png)
深入理解Python3 内置函数大全
2022-06-27 22:11:36
![](https://img.aspxhome.com/file/2023/1/87591_0s.png)
解析python 类方法、对象方法、静态方法
2022-10-08 04:47:57
sql自动增长标识导致导入数据问题的解决方法
2023-07-04 04:39:22
Python中的面向对象编程详解(上)
2021-10-12 14:33:45