Python 02 解析配置文件和日志库的使用
2014-02-12 16:47
736 查看
[log] path = /tmp/td_pay.log quiet = False debug_info = 1 [pay] app_id = 223 app_key = c01f [database] host = 1192.168.1.101 db_name = test user = test passwd = no
解析配置文件
import os import ConfigParser import logging import sys config_path = "/config.cfg" config_path = os.path.abspath(os.path.expanduser(config_path)) if not os.path.exists(config_path): print "Please Configure Your config.cfg " sys.exit(0) config = ConfigParser.RawConfigParser() config.read(config_path) log_file = os.path.abspath(os.path.expanduser(config.get('log', 'path'))) log_debug_info = config.getboolean('log', 'debug_info') log_quiet = config.getboolean('log', 'quiet') db_host = config.get('database', 'host') db_name = config.get('database', 'db_name') db_user = config.get('database', 'user') db_passwd = config.get('database', 'passwd')
日志库的使用
import os import logging import sys def log_init(log_file, log_debug_info, quiet=False): logger = logging.getLogger(__name__) if log_debug_info: logger.setLevel(logging.DEBUG) else: logger.setLevel(logging.INFO) hdlr = logging.FileHandler(log_file) formatter = logging.Formatter("%(process)s %(asctime)s %(pathname)s:%(lineno)d %(levelname)s %(message)s") hdlr.setFormatter(formatter) logger.addHandler(hdlr) if not quiet: hdlr = logging.StreamHandler() hdlr.setFormatter(formatter) logger.addHandler(hdlr) return logger LOG = log_init(log_file, log_debug_info, log_quiet) LOG.warn("Not This ss, ff is %s", ss) LOG.debug("charge success") LOG.error("Error %d: %s" % (e.args[0], e.args[1]))
相关文章推荐
- Python中使用ConfigParser解析ini配置文件实例
- 【Python】配置文件解析模块ConfigParser使用实例
- python 日志模块(不使用配置文件)
- Python logging模块使用配置文件记录日志
- Python3 使用配置文件创建日志
- Python中使用ConfigParser解析ini配置文件实例
- python使用ConfigParse解析配置文件
- Python配置文件解析模块ConfigParser使用实例
- python ConfigParser模块 配置文件解析
- Drools入门-----------环境搭建,分析Helloworld Drools5.0的xls文件转drl文件提升解析效率 使用BRMS的Tomcat6.0配置
- 单例设计模式中使用dom4j来完成(数据库配置文件)xml的解析,并完成数据库的连接
- Python解析配置文件模块:ConfigPhaser
- java中使用log4j日志文件,读取配置文件的方式
- 使用python脚本过滤出服务器日志文件中所有访问过robots.txt文件的主机
- Log4j日志的使用及配置文件(lp)
- python Flask框架的中配置文件加载的五中方法使用
- 02_JNI中Java代码调用C代码,Android中使用log库打印日志,javah命令的使用,Android.mk文件的编写,交叉编译
- Python_使用ElementTree解析xml文件
- 如何使用Python3读取配置文件(ini)
- 使用Python解析MNIST数据集(IDX文件格式)