python 日志

logging

python自带的记录日志模块--logging日志级别大小关系为:CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET,当然也可以自己定义日志级别。

import logging
logging.basicConfig(level=logging.DEBUG
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    filename='/tmp/test.log',
                    filemode='a')
logging.debug('this is debug message')
logging.info('this is info message')
logging.warning('this is warning message')
logging.error('this is error message')
logging.critical('this is critical message')

[root@python35 ~]# cat /tmp/test.log 
2016-05-05 10:20:08,915 loguser DEBUG: logger test
Thu, 05 May 2016 18:29:08 logger.py[line:7] DEBUG this is debug message
Thu, 05 May 2016 18:29:08 logger.py[line:8] INFO this is info message
Thu, 05 May 2016 18:29:08 logger.py[line:9] WARNING this is warning message
Thu, 05 May 2016 18:29:08 logger.py[line:10] ERROR this is error message
Thu, 05 May 2016 18:29:08 logger.py[line:11] CRITICAL this is critical message

日志会记录大于最低级别的日志。如上例中,

level=logging.DEBUG 改为 level=logging.INFO
则不会显示这条日志:

Thu, 05 May 2016 18:29:08 logger.py[line:7] DEBUG this is debug message

当规全局定了日志最低级别时,局部设定日志级别不能低于全局,否则使用全局的日志的级别。

 

logging.basicConfig函数各参数含义:

level: 设置日志级别,默认为logging.WARNING
format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所示:
  %(levelno)s: 打印日志级别的数值
  %(levelname)s: 打印日志级别名称
  %(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]
  %(filename)s: 打印当前执行程序名
  %(funcName)s: 打印日志的当前函数
  %(lineno)d: 打印日志的当前行号
  %(asctime)s: 打印日志的时间
  %(thread)d: 打印线程ID
  %(threadName)s: 打印线程名称
  %(process)d: 打印进程ID
  %(message)s: 打印日志信息
filename: 指定日志文件位置
filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a'
datefmt: 指定时间格式,同time.strftime()
stream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略

 而实际使用,我们可能会按以下方式使用:

def logger(logname,loglevel,logger):    #指定保存日志文件路劲,日志级别,以及调用文件
    #创建个logger
    logger=logging.getLogger(logger)
    logger=setLevel(logging.DEBUG)    #全局级别的日志,局部日志不能低于全局级别
    #创建一个handler,用于写入日志文件,定义不同的日志级别
    fh=logging.FileHandler(logname)   #logname='/tmp/test.log'
    fh.setLevel(logging.DEBUG)
    #再创建一个handler,用于输出到控制台,定义不同的日志级别
    ch=logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    #定义handler的输出格式
    #formatter=logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    formatter=format_dict[int(loglevel)]
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)
    #把handler和ch,fh关联起来
    logger.addHandler(fh)
    logger.addHandler(ch)

configparser

配置文件格式如下
config.ini

[user_info]   [=> section]
username=test
passwd=123

[db_config]   [=> section]
host=1.1.1.1
port=3306

获取配置文件的值

>>> import configparser
>>> config = configparser.ConfigParser()
>>> config.read("config.ini")
['config.ini']
>>> s = config.sections()   #获取所有的section
>>> print(s)
['user_info', 'db_config']
>>> p = config.options("user_info")  #获取user_info的key
>>> print(p)
['username', 'passwd']
>>> item = config.items("user_info") #获取user_info下所有的item值
>>> print(item)
[('username', 'test'), ('passwd', '123')]
>>> u = config.get("user_info", "username")
>>> print(u)
test

修改配置文件的值

>>> config.remove_section('user_info', 'username') #删除user_info下的键值队
>>> config.remove_section('user_info')   #删除user_info节点时,会删除下面所有的键值队
>>> config.write(open('config.ini','w+'))   #把配置文件保存到config.ini中
>>> sec=config.has_section("user_info")   #判断db_config节点是否存在
>>> print(sec)
False
>>> config.add_section("user_config")  #添加一个section
>>> config.set("user_config", "k1", "123")  #在节点下面添加一个键值队
>>> config.write(open('config.ini','w+'))

 

posted on 2016-06-09 14:19  逸秋  阅读(82)  评论(0)    收藏  举报

导航