0x04 Python logger 支持多进程日志按大小分割
支持多进程日志按大小分割
由于python内置模块logging.handlers.RotatingFileHandler是不支持多进程下日志分割的,存在进程间竞争同一文件。在网上找到了支持多进程的日志分割方案。
参考 中文博客
参考 英文mrfh
多进程日志大小分割handler配置实例
- 安装mrfh模块
pip install mrfh - 由于mrfh用到了fcntl模块,只有linux才有所以在win下是不可用的
- 下面是一个日志配置的实例,在file_size_rotate_hd中就用到了mrfh提供的handler。
LOGGER_CONFIG_DICT = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'detailed_fmt': {
'class': 'logging.Formatter',
'format': '%(asctime)s %(created)s %(levelname)-6s %(name)-15s %(processName)s:%(threadName)s %(message)s'
# human-readable timestamp levelname logger_name processname threadname message
},
'simple_fmt': {
'class': 'logging.Formatter',
'format': '%(asctime)s %(levelname)-8s %(name)-15s %(message)s'
# human-readable levelname logger_name message
},
'portal_fmt': {
'class': 'logging.Formatter',
# 'datefmt': '%Y-%m-%d %H:%M:%S,uuu', # 实际默认格式就是这个
'format': '%(asctime)s %(levelname)-8s %(name)-15s %(processName)-10s %(message)s'
},
'system_fmt': {
'class': 'logging.Formatter',
'format': '%(asctime)s %(name)-15s %(levelname)-8s %(processName)-10s %(message)s'
},
},
'handlers': {
'file_size_rotate_hd': {
#'class': 'logging.handlers.RotatingFileHandler',
'class': 'mrfh.MultiprocessRotatingFileHandler', # 这里就是用到mrfh了。
'filename': './logs/portal.log',
'mode': 'a',
'maxBytes': 1024 * 1024 * 500,
'backupCount': 50,
'formatter': 'portal_fmt'
},
'errors_hd': {
'class': 'logging.FileHandler',
'filename': './logs/errors.log',
'formatter': 'detailed_fmt',
'level': 'ERROR' # 只会错误40及以上的日志
},
'system_hd': {
'class': 'logging.FileHandler',
'filename': './logs/system.log',
'formatter': 'simple_fmt'
}
},
'loggers': {
'selfservices': {
'level': 'INFO',
'propagate': False,
'handlers': ['file_size_rotate_hd', 'errors_hd']
# 'handlers': ['time_rotate_file_hd', 'file_size_rotate_hd', 'errors_hd']
},
},
'root': {
'level': 'DEBUG',
'handlers': ['system_hd', 'errors_hd']
},
}
0x04 Python logger 支持多进程日志按大小分割的更多相关文章
- python logger 动态设置日志名
代码: import logging logger = logging.getLogger('') logger.setLevel(level=logging.INFO) def setLogName ...
- Python入门之logging日志模块以及多进程日志
本篇文章主要对 python logging 的介绍加深理解.更主要是 讨论在多进程环境下如何使用logging 来输出日志, 如何安全地切分日志文件. 1. logging日志模块介绍 python ...
- 【踩坑记录】记录一次使用Python logging库多进程打印日志的填坑过程
背景: 项目使用Python自带的logging库来打印日志 项目部署在一台Centos7的机器上 项目采用gunicorn多进程部署 过程: 1.LOG日志代码封装: 采用logging库,并设置w ...
- python 多线程和多进程
多线程与多进程 知识预览 一 进程与线程的概念 二 threading模块 三 multiprocessing模块 四 协程 五 IO模型 回到顶部 一 进程与线程的概念 1.1 进程 考虑一个场景: ...
- 笔记-python-module-logging.循环日志、多进程日志
笔记-python-module-logging.循环日志.多进程日志 1. logging循环日志 循环日志分为按大小切分和按时间切分,对应实现类如下. 1.1. RotatingFil ...
- Python中使用多进程来实现并行处理的方法小结
进程和线程是计算机软件领域里很重要的概念,进程和线程有区别,也有着密切的联系,先来辨析一下这两个概念: 1.定义 进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和 ...
- 用 Python 写一个多进程兼容的 TimedRotatingFileHandler
我前面有篇文章已经详细介绍了一下 Python 的日志模块.Python 提供了非常多的可以运用在各种不同场景的 Log Handler. TimedRotatingFileHandler 是 Pyt ...
- Python logger模块
1 logging模块简介 logging模块是Python内置的标准模块,主要用于输出运行日志,可以设置输出日志的等级.日志保存路径.日志文件回滚等:相比print,具备如下优点: 可以通过设置不同 ...
- Python模块学习 ---- logging 日志记录
许多应用程序中都会有日志模块,用于记录系统在运行过程中的一些关键信息,以便于对系统的运行状况进行跟踪.在.NET平台中,有非常著名的第三方开源日志组件log4net,c++中,有人们熟悉的log4cp ...
随机推荐
- SQL-select常用语句
1.全套装备 select [select选项] 字段列表[字段别名]/* from 数据源[where 条件子句] [group by条件子句] [having 子句] [order by 子句] ...
- Excel-图表制作
柱形图:通常使用柱形图比较数据间的数量关系 折线图:使用折线图来反映数据间的趋势关系 饼图:使用饼图来表示数据间的分配关系 对图表进行刷选 再图表设计下的快速布局可以选择各种样式 迷你图 对某列单 ...
- 003VlookUp的使用
在Excel中,Vlookup这个函数还是挺有用的 我最近在一个场景中使用到VlookUp函数,使用场景是 我们将学生名单导入到学业上报系统的时候,发现Excel中有 79条数据但是导入成功的提示是说 ...
- A1083 List Grades (25 分)
Given a list of N student records with name, ID and grade. You are supposed to sort the records with ...
- [LeetCode] 525. Contiguous Array 相连的数组
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...
- oracle--AWR,ADDM,ASH区别
一,ASH (Active Session History) ASH以V$SESSION为基础,每秒采样一次,记录活动会话等待的事件.不活动的会话不会采样,采样工作由新引入的后台进程MMNL来完成. ...
- java byte[]和base64互相转换
1.方式一 import java.io.UnsupportedEncodingException; import java.util.Base64; // byte[]转base64 Strin ...
- swagger案例Swagger案例
pom <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework ...
- [最新].NET Core ORM 开源项目一览,持续更新
截至2019-05-08共收集27个 .NET Core ORM 开源项目,38个 .NET ORM 开源项目. .NET Core ORM 开源项目收集地址:https://github.com/o ...
- MYSQL插入千万数据的工具类
建表语句 CREATE TABLE `test_id` ( `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键自增非空', `name` varchar( ...