python 多进程 logging:ConcurrentLogHandler
python 多进程 logging:ConcurrentLogHandler
python的logging模块RotatingFileHandler仅仅是线程安全的,如果多进程多线程使用,推荐 ConcurrentLogHandler. 安装之:
# Using ConcurrentLogHandler:
# wget https://pypi.python.org/packages/fd/e5/0dc4f256bcc6484d454006b02f33263b20f762a433741b29d53875e0d763/ConcurrentLogHandler-0.9.1.tar.gz#md5=9609ecc4c269ac43f0837d89f12554c3 # cd ConcurrentLogHandler-0.9.1 # python2.7 setup.py install
Linux下建一个目录,下面的文件都放到这个目录中:
1) logging-config.ini
[loggers] keys=root,simpleExample [handlers] keys=consoleHandler [formatters] keys=simpleFormatter [logger_root] level=DEBUG handlers=consoleHandler [logger_simpleExample] level=DEBUG handlers=consoleHandler qualname=simpleExample propagate=0 [handler_consoleHandler] class=StreamHandler level=DEBUG formatter=simpleFormatter args=(sys.stdout,) [formatter_simpleFormatter] format=%(asctime)s - %(name)s - %(levelname)s - %(message)s datefmt=
2) logging-config.yaml
version: 1 formatters: simple: format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s' handlers: console: class: logging.StreamHandler level: DEBUG formatter: simple stream: ext://sys.stdout loggers: simpleExample: level: DEBUG handlers: [console] propagate: no root: level: DEBUG handlers: [console]
3) testlogging.py
#!/usr/bin/python2.7 #-*- coding: UTF-8 -*- # # Using ConcurrentLogHandler: # wget https://pypi.python.org/packages/fd/e5/0dc4f256bcc6484d454006b02f33263b20f762a433741b29d53875e0d763/ConcurrentLogHandler-0.9.1.tar.gz#md5=9609ecc4c269ac43f0837d89f12554c3 # cd ConcurrentLogHandler-0.9.1 # python2.7 setup.py install ########################################################### import logging, logging.config import cloghandler import yaml ########################################################### # create logger # 使用代码创建logger logger = logging.getLogger('simple_example') logger.setLevel(logging.DEBUG) # create console handler and set level to debug ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) # create formatter formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') # add formatter to ch ch.setFormatter(formatter) # add ch to logger logger.addHandler(ch) # 'application' code logger.debug('debug message') logger.info('info message') logger.warn('warn message') logger.error('error message') logger.critical('critical message') ########################################################### # basicConfig logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') logging.warning('is when this event was logged.') ########################################################### # using yaml config file f = open("logging-config.yaml") dictcfg = yaml.load(f) f.close() logging.config.dictConfig(dictcfg) #logging.config.fileConfig("logging.config") log = logging.getLogger("root") log.info("==YAML== Here is a very exciting log message") ########################################################### # using ini config file logging.config.fileConfig("logging-config.ini") log = logging.getLogger("simpleExample") log.info("==INI== Here is a very exciting log message") ########################################################### # using inline code config logging.config.dictConfig({ 'version': 1, 'disable_existing_loggers': True, 'formatters': { 'verbose': { 'format': "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s", 'datefmt': "%Y-%m-%d %H:%M:%S", }, 'simple': { 'format': '%(levelname)s %(message)s', }, }, 'handlers': { 'null': { 'level': 'DEBUG', 'class': 'logging.NullHandler', }, 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'verbose', }, 'file': { 'level': 'DEBUG', 'class': 'cloghandler.ConcurrentRotatingFileHandler', 'maxBytes': 1024 * 1024 * 10, # 当达到10MB时分割日志 'backupCount': 10, # 最多保留10份文件 'delay': True, # If delay is true, file opening is deferred until the first call to emit 'filename': 'sample-site.log', 'formatter': 'verbose', }, 'file2': { 'level': 'DEBUG', 'class': 'cloghandler.ConcurrentRotatingFileHandler', 'maxBytes': 1024 * 1024 * 10, # 当达到10MB时分割日志 'backupCount': 10, # 最多保留10份文件 'delay': True, # If delay is true, file opening is deferred until the first call to emit 'filename': 'sample-site2.log', 'formatter': 'verbose', }, }, 'loggers': { '': { 'handlers': ['file'], 'level': 'INFO', }, 'root': { 'handlers': ['console'], 'level': 'INFO', 'propagate': 0, }, 'root2': { 'handlers': ['console'], 'level': 'INFO', 'propagate': 1, }, }, }) logger = logging.getLogger("root") logger.info("==== Here is a very exciting log message") logger = logging.getLogger("root2") logger.info("==== Here is a very exciting log message2")
至于喜欢使用哪种配置(ini, yaml还是代码)看自己喜欢了。我建议是yaml。
python 多进程 logging:ConcurrentLogHandler的更多相关文章
- Python 中 logging 日志模块在多进程环境下的使用
因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,Python 中 logging 日志模块在多进程环境下的使用 使用 Pytho ...
- Python多进程multiprocessing使用示例
mutilprocess简介 像线程一样管理进程,这个是mutilprocess的核心,他与threading很是相像,对多核CPU的利用率会比threading好的多. import multipr ...
- Python多进程池 multiprocessing Pool
1. 背景 由于需要写python程序, 定时.大量发送htttp请求,并对结果进行处理. 参考其他代码有进程池,记录一下. 2. 多进程 vs 多线程 c++程序中,单个模块通常是单进程,会启动几十 ...
- Python 多进程教程
Python2.6版本中新添了multiprocessing模块.它最初由Jesse Noller和Richard Oudkerk定义在PEP 371中.就像你能通过threading模块衍生线程一样 ...
- Python多进程库multiprocessing创建进程以及进程池Pool类的使用
问题起因最近要将一个文本分割成好几个topic,每个topic设计一个regressor,各regressor是相互独立的,最后汇总所有topic的regressor得到总得预测结果.没错!类似bag ...
- Python之logging模块
一.引言 之前在写一些小程序的时候想把日志内容打到文件中,所以就自己写了一个logger.py的程序,如下: #!/usr/bin/python # -*- coding=utf-8 -*- impo ...
- Python多进程编程
转自:Python多进程编程 阅读目录 1. Process 2. Lock 3. Semaphore 4. Event 5. Queue 6. Pipe 7. Pool 序. multiproces ...
- Python多进程(1)——subprocess与Popen()
Python多进程方面涉及的模块主要包括: subprocess:可以在当前程序中执行其他程序或命令: mmap:提供一种基于内存的进程间通信机制: multiprocessing:提供支持多处理器技 ...
- Python多进程使用
[Python之旅]第六篇(六):Python多进程使用 香飘叶子 2016-05-10 10:57:50 浏览190 评论0 python 多进程 多进程通信 摘要: 关于进程与线程的对比, ...
随机推荐
- NOIP提高组2010 乌龟棋
小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家控制一个乌龟棋子从起点出发走到终点. 乌 ...
- Polya计数
Let it Bead Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5365 Accepted: 3585 Descr ...
- python3 条件判断,循环,三元表达式
一. 条件判断 条件判断的关键字if elif else,具体规则如下: if condition_1: statement_block_1 elif condition_2: statement_b ...
- 浅谈java中内置的观察者模式与动态代理的实现
一.关于观察者模式 1.将观察者与被观察者分离开来,当被观察者发生变化时,将通知所有观察者,观察者会根据这些变化做出对应的处理. 2.jdk里已经提供对应的Observer接口(观察者接口)与Obse ...
- sklearn.model_selection 的 train_test_split作用
train_test_split函数用于将数据划分为训练数据和测试数据. train_test_split是交叉验证中常用的函数,功能是从样本中随机的按比例选取train_data和test_data ...
- 《java技术》第二次作业
(一)学习总结 1.什么是构造方法?什么是构造方法的重载? 1)没有返回值,名字与类名相同,当新对象被创建的时候,构造函数会被调用,要想构造函数,必须声明对象并对其初始化.每一个类都有构造函数,如果没 ...
- JSON概述
错误理解: 一直以为JSON就是对象,拥有跟js对象类似的特征:{key:value}形式, 以至于在自己的思维定式中就出现了一种很可怕的情景:居然不知道怎么去解释习以为常的json是 ...
- SSH构造struts2项目
第一在pom.xml导入相应的包 (网上有很多导入多个包的教程,我缩减到一个了) <project xmlns="http://maven.apache.org/POM/4.0.0&q ...
- 一看你就懂,超详细java中的ClassLoader详解
本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布 ClassLoader翻译过来就是类加载器,普通的Java开发者其实用到的不多,但对于某些框架开发者来说却非常常见.理解ClassL ...
- ORA-01207: file is more recent than control file - old control file的处理方法
1. 连接数据库 sqlplus / as sysdba2. 启动数据库,此时会报标题中的错误startup 3.备份创建控制文件的脚本语句,并从中拷贝出相关的NORESETLOGS模式的创建控制文件 ...