配置python日志的简单助手moudle
logconfig的Python项目详细描述
配置python日志的简单助手moudle。
要求
兼容性
- Python2.6
- python 2.7版
- python 3.2
- Python3.3
- Python3.4
依赖性
- 皮亚姆
- logutils(如果使用python 2)
安装
pip install logconfig
概述
这个简单的库公开了几个用于配置标准库的logging模块的助手方法。一点也不奇怪。在hood之下logconfig使用logging.config加载各种配置格式。
除了配置加载之外,logconfig还提供了帮助程序,用于利用队列轻松转换已配置记录器的处理程序。
支持的配置格式
- json
- 山药
- 配置分析器
- python dict
快速启动
配置加载
importlogconfigimportlogging# Load config from JSON filelogconfig.from_json('path/to/file.json')# Load config from YAML filelogconfig.from_yaml('path/to/file.yml')# Load config from ConfigParser filelogconfig.from_yaml('path/to/file.cfg')# Load config from dictlogconfig.from_dict(config_dict)log=logging.getLogger()log.debug('Configuration loaded using logconfig')
队列利用率
importlogconfigimportlogginglogconfig.from_dict({'version':1,'disable_existing_loggers':False,'handlers':{'console':{'class':'logging.StreamHandler','level':'DEBUG'}},'loggers':{'mylogger':{'handlers':['console']}}})# Convert logger's handlers to utilize a queuequeue=logconfig.Queue(-1)listener=logconfig.QueueListener(queue)handler=logconfig.QueueHandler(queue)mylogger=logging.getLogger('mylogger')# You can also pass in the logger name instead of the actual logger.# logconfig.queuify_logger('mylogger', handler, listener)logconfig.queuify_logger(mylogger,handler,listener)assertisinstance(mylogger.handlers[0],logconfig.QueueHandler)# Start the listener.listener.start()# When finished, stop the listener.# This is optional, but not doing so may prevent some logs from being processed.listener.stop()
用法
使用logconfig可以轻松加载logging配置。有关配置logging的详细信息,请访问https://docs.python.org/library/logging.config.html。
importlogconfig
从json配置
使用json文件配置日志记录。
logconfig.from_json(filename)
示例json文件:
{"version":1,"disable_existing_loggers":false,"formatters":{"simple":{"format":"%(asctime)s. - %(name)s - %(levelname)s - %(message)s"}},"handlers":{"console":{"class":"logging.StreamHandler","level":"DEBUG","formatter":"simple","stream":"ext://sys.stdout"}},"root":{"level":"DEBUG","handlers":["console"]}}
来自yaml的配置
使用yaml文件配置日志记录。
logconfig.from_yaml(filename)
示例yaml文件:
version:1disable_existing_loggers:Falseformatters:simple:format:"%(asctime)s.-%(name)s-%(levelname)s-%(message)s"handlers:console:class:logging.StreamHandlerlevel:DEBUGformatter:simplestream:ext://sys.stdoutroot:level:DEBUGhandlers:[console]
来自configparser文件的配置
使用configparser兼容文件配置日志记录。
logconfig.from_file(filename)
cfg文件示例:
[loggers]keys=root[handlers]keys=console[formatters]keys=simple[logger_root]level=DEBUGhandlers=console[handler_console]class=StreamHandlerlevel=DEBUGformatter=simpleargs=(sys.stdout,)[formatter_simple]format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
来自dict
的配置使用python字典配置日志记录。
logconfig.from_dict(dct)
示例dict:
{'version':1,'disable_existing_loggers':False,'formatters':{'simple':{'format':'%(asctime)s. - %(name)s - %(levelname)s - %(message)s'}},'handlers':{'console':{'formatter':'simple','class':'logging.StreamHandler','level':'DEBUG','stream':'ext://sys.stdout'}},'root':{'handlers':['console'],'level':'DEBUG'}}
自动检测配置
无论出于什么原因,如果您不知道配置的源是什么(或者您只是觉得幸运),那么您可以尝试使用自动检测方法之一强制进行日志记录配置:
logconfig.from_filename(filename)logconfig.from_autodetect(filename_or_dict)try:logconfig.from_filename(filename)logconfig.from_autodetect(filename_or_dict)exceptlogconfig.LogConfigExceptionasex:# Unrecognized configuration argument.pass
这些方法将尝试将函数参数分派给正确的配置加载器,或者尝试失败。
环境变量配置
使用通过环境变量提供的文件名配置日志记录。
logconfig.from_env(variable_name)
注意:环境变量值将传递给from_filename()。