python 多进程 logging:ConcurrentLogHandler
来源:互联网 发布:centos 6.3升级 内核 编辑:程序博客网 时间:2024/06/07 09:49
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=DEBUGhandlers=consoleHandler[logger_simpleExample]level=DEBUGhandlers=consoleHandlerqualname=simpleExamplepropagate=0[handler_consoleHandler]class=StreamHandlerlevel=DEBUGformatter=simpleFormatterargs=(sys.stdout,)[formatter_simpleFormatter]format=%(asctime)s - %(name)s - %(levelname)s - %(message)sdatefmt=
2) logging-config.yaml
version: 1formatters: simple: format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'handlers: console: class: logging.StreamHandler level: DEBUG formatter: simple stream: ext://sys.stdoutloggers: simpleExample: level: DEBUG handlers: [console] propagate: noroot: 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.configimport cloghandlerimport yaml############################################################ create logger# 使用代码创建loggerlogger = logging.getLogger('simple_example')logger.setLevel(logging.DEBUG)# create console handler and set level to debugch = logging.StreamHandler()ch.setLevel(logging.DEBUG)# create formatterformatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')# add formatter to chch.setFormatter(formatter)# add ch to loggerlogger.addHandler(ch)# 'application' codelogger.debug('debug message')logger.info('info message')logger.warn('warn message')logger.error('error message')logger.critical('critical message')############################################################ basicConfiglogging.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 filef = 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 filelogging.config.fileConfig("logging-config.ini")log = logging.getLogger("simpleExample")log.info("==INI== Here is a very exciting log message")############################################################ using inline code configlogging.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。
0 0
- python 多进程 logging:ConcurrentLogHandler
- python-Logging之进程安全的ConcurrentLogHandler
- python类库32[多进程之Pool+Logging]
- python的日志logging模块性能以及多进程
- logging TimedRotatingFileHandler不支持多进程
- Python 中 logging 日志模块在多进程环境下的使用
- 使用python logging处理多机多进程写同一个日志文件
- python logging 模块 程序结束后并没有终止进程
- Python logging
- python logging
- python logging
- python logging
- Python Logging
- 【python】-logging
- python logging
- python logging
- python logging
- python logging
- OC2
- Java Marker Interface
- mac安装Homebrew
- symfony2目录简介
- web使用jq实现复制功能
- python 多进程 logging:ConcurrentLogHandler
- Java队列Queue、双端队列Deque
- C++11 多线程编程 原子变量
- nyoj745
- Android总结 - Service
- ImageLoader 官网
- jsonp方式解决跨域共享问题
- Android ToggleButton控件的用法
- 一位资深程序员大牛给予Java初学者的学习建议